mirror of
https://github.com/pcvolkmer/onkostar-plugin-dnpm.git
synced 2025-07-03 01:32: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>
|
<version>${spring-version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-jpa</artifactId>
|
||||||
|
<version>1.2.0.RELEASE</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package DNPM.config;
|
package DNPM.config;
|
||||||
|
|
||||||
|
import DNPM.database.SettingsRepository;
|
||||||
import DNPM.services.*;
|
import DNPM.services.*;
|
||||||
import de.itc.onkostar.api.IOnkostarApi;
|
import de.itc.onkostar.api.IOnkostarApi;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
@ -15,6 +17,7 @@ import javax.sql.DataSource;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackages = "DNPM.analyzer")
|
@ComponentScan(basePackages = "DNPM.analyzer")
|
||||||
|
@EnableJpaRepositories(basePackages = "DNPM.database")
|
||||||
public class PluginConfiguration {
|
public class PluginConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -28,8 +31,17 @@ public class PluginConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TherapieplanServiceFactory therapieplanServiceFactory(final IOnkostarApi onkostarApi, final FormService formService) {
|
public SettingsService settingsService(final SettingsRepository settingsRepository) {
|
||||||
return new TherapieplanServiceFactory(onkostarApi, formService);
|
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 IOnkostarApi onkostarApi;
|
||||||
|
|
||||||
|
private final SettingsService settingsService;
|
||||||
|
|
||||||
private final FormService formService;
|
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.onkostarApi = onkostarApi;
|
||||||
|
this.settingsService = settingsService;
|
||||||
this.formService = formService;
|
this.formService = formService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TherapieplanService currentUsableInstance() {
|
public TherapieplanService currentUsableInstance() {
|
||||||
if (
|
if (settingsService.multipleMtbsInMtbEpisode()) {
|
||||||
null != onkostarApi.getGlobalSetting("mehrere_mtb_in_mtbepisode")
|
|
||||||
&& onkostarApi.getGlobalSetting("mehrere_mtb_in_mtbepisode").equals("true")
|
|
||||||
) {
|
|
||||||
return new MultipleMtbTherapieplanService();
|
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;
|
package DNPM.config;
|
||||||
|
|
||||||
import DNPM.services.FormService;
|
import DNPM.services.FormService;
|
||||||
|
import DNPM.services.SettingsService;
|
||||||
import DNPM.services.TherapieplanServiceFactory;
|
import DNPM.services.TherapieplanServiceFactory;
|
||||||
import de.itc.onkostar.api.IOnkostarApi;
|
import de.itc.onkostar.api.IOnkostarApi;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@ -17,6 +18,9 @@ public class PluginConfigurationTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private IOnkostarApi onkostarApi;
|
private IOnkostarApi onkostarApi;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private SettingsService settingsService;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private FormService formService;
|
private FormService formService;
|
||||||
|
|
||||||
@ -29,7 +33,7 @@ public class PluginConfigurationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testShouldReturnTherapieplanServiceFactory() {
|
void testShouldReturnTherapieplanServiceFactory() {
|
||||||
var actual = this.configuration.therapieplanServiceFactory(onkostarApi, formService);
|
var actual = this.configuration.therapieplanServiceFactory(onkostarApi, settingsService, formService);
|
||||||
assertThat(actual).isInstanceOf(TherapieplanServiceFactory.class);
|
assertThat(actual).isInstanceOf(TherapieplanServiceFactory.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package DNPM.config;
|
package DNPM.config;
|
||||||
|
|
||||||
import DNPM.services.DefaultTherapieplanService;
|
import DNPM.services.*;
|
||||||
import DNPM.services.FormService;
|
|
||||||
import DNPM.services.MultipleMtbTherapieplanService;
|
|
||||||
import DNPM.services.TherapieplanServiceFactory;
|
|
||||||
import de.itc.onkostar.api.IOnkostarApi;
|
import de.itc.onkostar.api.IOnkostarApi;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -12,8 +9,6 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
@ -25,32 +20,21 @@ public class TherapieplanServiceFactoryTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private FormService formService;
|
private FormService formService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private SettingsService settingsService;
|
||||||
|
|
||||||
private TherapieplanServiceFactory therapieplanServiceFactory;
|
private TherapieplanServiceFactory therapieplanServiceFactory;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
this.therapieplanServiceFactory = new TherapieplanServiceFactory(onkostarApi, formService);
|
this.therapieplanServiceFactory = new TherapieplanServiceFactory(onkostarApi, settingsService, formService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testShouldReturnDefaultTherapieplanServiceIfSettingIsFalse() {
|
void testShouldReturnDefaultTherapieplanServiceIfSettingIsFalse() {
|
||||||
doAnswer(invocationOnMock -> {
|
when(settingsService.multipleMtbsInMtbEpisode()).thenReturn(false);
|
||||||
var settingName = invocationOnMock.getArgument(0, String.class);
|
|
||||||
if (settingName.equals("mehrere_mtb_in_mtbepisode")) {
|
|
||||||
return "false";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}).when(onkostarApi).getGlobalSetting(anyString());
|
|
||||||
|
|
||||||
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
||||||
|
|
||||||
assertThat(actual).isInstanceOf(DefaultTherapieplanService.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testShouldReturnDefaultTherapieplanServiceIfNoSetting() {
|
|
||||||
when(onkostarApi.getGlobalSetting(anyString())).thenReturn(null);
|
|
||||||
|
|
||||||
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
||||||
|
|
||||||
assertThat(actual).isInstanceOf(DefaultTherapieplanService.class);
|
assertThat(actual).isInstanceOf(DefaultTherapieplanService.class);
|
||||||
@ -58,13 +42,7 @@ public class TherapieplanServiceFactoryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testShouldReturnMultipleMtbTherapieplanServiceIfSettingIsTrue() {
|
void testShouldReturnMultipleMtbTherapieplanServiceIfSettingIsTrue() {
|
||||||
doAnswer(invocationOnMock -> {
|
when(settingsService.multipleMtbsInMtbEpisode()).thenReturn(true);
|
||||||
var settingName = invocationOnMock.getArgument(0, String.class);
|
|
||||||
if (settingName.equals("mehrere_mtb_in_mtbepisode")) {
|
|
||||||
return "true";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}).when(onkostarApi).getGlobalSetting(anyString());
|
|
||||||
|
|
||||||
var actual = this.therapieplanServiceFactory.currentUsableInstance();
|
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