mirror of
https://github.com/pcvolkmer/onkostar-plugin-dnpm.git
synced 2025-07-02 01:02:55 +00:00
Issue #11: Service und Spring Data JPA Repository für Systemeinstellungen
This commit is contained in:
6
pom.xml
6
pom.xml
@ -47,6 +47,12 @@
|
||||
<version>${spring-version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>1.2.0.RELEASE</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
|
@ -1,10 +1,12 @@
|
||||
package DNPM.config;
|
||||
|
||||
import DNPM.database.SettingsRepository;
|
||||
import DNPM.services.*;
|
||||
import de.itc.onkostar.api.IOnkostarApi;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@ -15,6 +17,7 @@ import javax.sql.DataSource;
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "DNPM.analyzer")
|
||||
@EnableJpaRepositories(basePackages = "DNPM.database")
|
||||
public class PluginConfiguration {
|
||||
|
||||
@Bean
|
||||
@ -28,8 +31,17 @@ public class PluginConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TherapieplanServiceFactory therapieplanServiceFactory(final IOnkostarApi onkostarApi, final FormService formService) {
|
||||
return new TherapieplanServiceFactory(onkostarApi, formService);
|
||||
public SettingsService settingsService(final SettingsRepository settingsRepository) {
|
||||
return new SettingsService(settingsRepository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TherapieplanServiceFactory therapieplanServiceFactory(
|
||||
final IOnkostarApi onkostarApi,
|
||||
final SettingsService settingsService,
|
||||
final FormService formService
|
||||
) {
|
||||
return new TherapieplanServiceFactory(onkostarApi, settingsService, formService);
|
||||
}
|
||||
|
||||
}
|
||||
|
20
src/main/java/DNPM/database/ReadOnlyRepository.java
Normal file
20
src/main/java/DNPM/database/ReadOnlyRepository.java
Normal file
@ -0,0 +1,20 @@
|
||||
package DNPM.database;
|
||||
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Basis-Repository for ReadOnly Spring-Data-JPA Repositories
|
||||
* <p>Entity-Klassen müssen in Package <code>de.itc.db.dnpm</code> liegen
|
||||
* @param <T> Typ des Entities
|
||||
* @param <ID> Typ der ID
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {
|
||||
|
||||
List<T> findAll();
|
||||
|
||||
}
|
14
src/main/java/DNPM/database/SettingsRepository.java
Normal file
14
src/main/java/DNPM/database/SettingsRepository.java
Normal file
@ -0,0 +1,14 @@
|
||||
package DNPM.database;
|
||||
|
||||
import de.itc.db.dnpm.Setting;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* Spring Data JPA Repository zum Lesen von Einstellungen
|
||||
*/
|
||||
@Repository("dnpmSettingRepository")
|
||||
public interface SettingsRepository extends ReadOnlyRepository<Setting, Long> {
|
||||
|
||||
Setting findByName(String name);
|
||||
|
||||
}
|
47
src/main/java/DNPM/services/SettingsService.java
Normal file
47
src/main/java/DNPM/services/SettingsService.java
Normal file
@ -0,0 +1,47 @@
|
||||
package DNPM.services;
|
||||
|
||||
import DNPM.database.SettingsRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Implementiert den Dienst zur Ermittlung von Systemeinstellungen
|
||||
*/
|
||||
public class SettingsService {
|
||||
|
||||
private final SettingsRepository settingsRepository;
|
||||
|
||||
public SettingsService(final SettingsRepository settingsRepository) {
|
||||
this.settingsRepository = settingsRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Übergibt ein <code>Optional</code> für die Einstellung mit angegebenen Namen
|
||||
* @param name Name der Einstellung
|
||||
* @return Optional mit Wert der Einstellung oder ein leeres Optional, wenn Einstellung nicht gefunden
|
||||
*/
|
||||
public Optional<String> getSetting(String name) {
|
||||
var sid = settingsRepository.findByName(name);
|
||||
if (null == sid) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(sid.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Übergibt die SID als <code>Optional</code>
|
||||
* @return Optional mit Wert der SID
|
||||
*/
|
||||
public Optional<String> getSID() {
|
||||
return getSetting("SID");
|
||||
}
|
||||
|
||||
/**
|
||||
* Übergibt die Einstellung für <code>mehrere_mtb_in_mtbepisode</code>
|
||||
* @return Übergibt <code>true</code>, wenn <code>mehrere_mtb_in_mtbepisode</code> auf "Ja" gesetzt ist.
|
||||
*/
|
||||
public boolean multipleMtbsInMtbEpisode() {
|
||||
var setting = getSetting("SID");
|
||||
return setting.isPresent() && setting.get().equals("true");
|
||||
}
|
||||
}
|
@ -6,18 +6,22 @@ public class TherapieplanServiceFactory {
|
||||
|
||||
private final IOnkostarApi onkostarApi;
|
||||
|
||||
private final SettingsService settingsService;
|
||||
|
||||
private final FormService formService;
|
||||
|
||||
public TherapieplanServiceFactory(IOnkostarApi onkostarApi, FormService formService) {
|
||||
public TherapieplanServiceFactory(
|
||||
final IOnkostarApi onkostarApi,
|
||||
final SettingsService settingsService,
|
||||
final FormService formService
|
||||
) {
|
||||
this.onkostarApi = onkostarApi;
|
||||
this.settingsService = settingsService;
|
||||
this.formService = formService;
|
||||
}
|
||||
|
||||
public TherapieplanService currentUsableInstance() {
|
||||
if (
|
||||
null != onkostarApi.getGlobalSetting("mehrere_mtb_in_mtbepisode")
|
||||
&& onkostarApi.getGlobalSetting("mehrere_mtb_in_mtbepisode").equals("true")
|
||||
) {
|
||||
if (settingsService.multipleMtbsInMtbEpisode()) {
|
||||
return new MultipleMtbTherapieplanService();
|
||||
}
|
||||
|
||||
|
43
src/main/java/de/itc/db/dnpm/Setting.java
Normal file
43
src/main/java/de/itc/db/dnpm/Setting.java
Normal file
@ -0,0 +1,43 @@
|
||||
package de.itc.db.dnpm;
|
||||
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Immutable
|
||||
@Table(name = "einstellung")
|
||||
public class Setting {
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Column(name = "wert")
|
||||
private String value;
|
||||
|
||||
protected Setting() {
|
||||
// No content
|
||||
}
|
||||
|
||||
public Setting(Long id, String name, String value) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package DNPM.config;
|
||||
|
||||
import DNPM.services.FormService;
|
||||
import DNPM.services.SettingsService;
|
||||
import DNPM.services.TherapieplanServiceFactory;
|
||||
import de.itc.onkostar.api.IOnkostarApi;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -17,6 +18,9 @@ public class PluginConfigurationTest {
|
||||
@Mock
|
||||
private IOnkostarApi onkostarApi;
|
||||
|
||||
@Mock
|
||||
private SettingsService settingsService;
|
||||
|
||||
@Mock
|
||||
private FormService formService;
|
||||
|
||||
@ -29,7 +33,7 @@ public class PluginConfigurationTest {
|
||||
|
||||
@Test
|
||||
void testShouldReturnTherapieplanServiceFactory() {
|
||||
var actual = this.configuration.therapieplanServiceFactory(onkostarApi, formService);
|
||||
var actual = this.configuration.therapieplanServiceFactory(onkostarApi, settingsService, formService);
|
||||
assertThat(actual).isInstanceOf(TherapieplanServiceFactory.class);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,6 @@
|
||||
package DNPM.config;
|
||||
|
||||
import DNPM.services.DefaultTherapieplanService;
|
||||
import DNPM.services.FormService;
|
||||
import DNPM.services.MultipleMtbTherapieplanService;
|
||||
import DNPM.services.TherapieplanServiceFactory;
|
||||
import DNPM.services.*;
|
||||
import de.itc.onkostar.api.IOnkostarApi;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -12,8 +9,6 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@ -25,32 +20,21 @@ public class TherapieplanServiceFactoryTest {
|
||||
@Mock
|
||||
private FormService formService;
|
||||
|
||||
@Mock
|
||||
private SettingsService settingsService;
|
||||
|
||||
private TherapieplanServiceFactory therapieplanServiceFactory;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.therapieplanServiceFactory = new TherapieplanServiceFactory(onkostarApi, formService);
|
||||
this.therapieplanServiceFactory = new TherapieplanServiceFactory(onkostarApi, settingsService, formService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testShouldReturnDefaultTherapieplanServiceIfSettingIsFalse() {
|
||||
doAnswer(invocationOnMock -> {
|
||||
var settingName = invocationOnMock.getArgument(0, String.class);
|
||||
if (settingName.equals("mehrere_mtb_in_mtbepisode")) {
|
||||
return "false";
|
||||
}
|
||||
return null;
|
||||
}).when(onkostarApi).getGlobalSetting(anyString());
|
||||
|
||||
when(settingsService.multipleMtbsInMtbEpisode()).thenReturn(false);
|
||||
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
||||
|
||||
assertThat(actual).isInstanceOf(DefaultTherapieplanService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testShouldReturnDefaultTherapieplanServiceIfNoSetting() {
|
||||
when(onkostarApi.getGlobalSetting(anyString())).thenReturn(null);
|
||||
|
||||
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
||||
|
||||
assertThat(actual).isInstanceOf(DefaultTherapieplanService.class);
|
||||
@ -58,13 +42,7 @@ public class TherapieplanServiceFactoryTest {
|
||||
|
||||
@Test
|
||||
void testShouldReturnMultipleMtbTherapieplanServiceIfSettingIsTrue() {
|
||||
doAnswer(invocationOnMock -> {
|
||||
var settingName = invocationOnMock.getArgument(0, String.class);
|
||||
if (settingName.equals("mehrere_mtb_in_mtbepisode")) {
|
||||
return "true";
|
||||
}
|
||||
return null;
|
||||
}).when(onkostarApi).getGlobalSetting(anyString());
|
||||
when(settingsService.multipleMtbsInMtbEpisode()).thenReturn(true);
|
||||
|
||||
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
||||
|
||||
|
58
src/test/java/DNPM/services/SettingsServiceTest.java
Normal file
58
src/test/java/DNPM/services/SettingsServiceTest.java
Normal file
@ -0,0 +1,58 @@
|
||||
package DNPM.services;
|
||||
|
||||
import DNPM.database.SettingsRepository;
|
||||
import de.itc.db.dnpm.Setting;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class SettingsServiceTest {
|
||||
|
||||
@Mock
|
||||
private SettingsRepository settingsRepository;
|
||||
|
||||
private SettingsService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.service = new SettingsService(settingsRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnSID() {
|
||||
doAnswer(invocationOnMock -> {
|
||||
var name = invocationOnMock.getArgument(0, String.class);
|
||||
if (null != name && name.equals("SID")) {
|
||||
return new Setting(1L, "SID", "12345");
|
||||
}
|
||||
return null;
|
||||
}).when(settingsRepository).findByName(anyString());
|
||||
|
||||
var actual = service.getSID();
|
||||
assertThat(actual).isPresent();
|
||||
assertThat(actual.get()).isEqualTo("12345");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnSIDByName() {
|
||||
doAnswer(invocationOnMock -> {
|
||||
var name = invocationOnMock.getArgument(0, String.class);
|
||||
if (null != name && name.equals("SID")) {
|
||||
return new Setting(1L, "SID", "12345");
|
||||
}
|
||||
return null;
|
||||
}).when(settingsRepository).findByName(anyString());
|
||||
|
||||
var actual = service.getSetting("SID");
|
||||
assertThat(actual).isPresent();
|
||||
assertThat(actual.get()).isEqualTo("12345");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user