diff --git a/pom.xml b/pom.xml index 44e290cd1155875f546056050359a3954a0fe865..f2ab9d3049519f6a30c5911e54b1053bc205bdad 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,9 @@ <testcontainers-keycloak.version>3.2.0</testcontainers-keycloak.version> <keycloak-admin-client.version>23.0.6</keycloak-admin-client.version> <mongock.version>5.4.0</mongock.version> + <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version> + <mapstruct-processor.version>1.5.5.Final</mapstruct-processor.version> + </properties> <dependencies> @@ -70,6 +73,17 @@ <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> + <!-- tools --> + <dependency> + <groupId>org.mapstruct</groupId> + <artifactId>mapstruct</artifactId> + </dependency> + <dependency> + <groupId>org.mapstruct</groupId> + <artifactId>mapstruct-processor</artifactId> + <version>${mapstruct-processor.version}</version> + </dependency> + <!-- mongock --> <dependency> <groupId>io.mongock</groupId> @@ -222,6 +236,29 @@ </execution> </executions> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <annotationProcessorPaths> + <path> + <groupId>org.mapstruct</groupId> + <artifactId>mapstruct-processor</artifactId> + <version>${mapstruct-processor.version}</version> + </path> + <path> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + </path> + <path> + <groupId>org.projectlombok</groupId> + <artifactId>lombok-mapstruct-binding</artifactId> + <version>${lombok-mapstruct-binding.version}</version> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> </plugins> </build> diff --git a/src/main/java/de/ozgcloud/admin/AdministrationApplication.java b/src/main/java/de/ozgcloud/admin/AdministrationApplication.java index 563eea5b36d486c99187d35a08c61e6fb2e34bf1..b33cca756ff78962f1b54145707fa20ae95a0f6c 100644 --- a/src/main/java/de/ozgcloud/admin/AdministrationApplication.java +++ b/src/main/java/de/ozgcloud/admin/AdministrationApplication.java @@ -21,18 +21,18 @@ */ package de.ozgcloud.admin; -import io.mongock.runner.springboot.EnableMongock; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; +import io.mongock.runner.springboot.EnableMongock; + @SpringBootApplication @EnableConfigServer @EnableMongock @EnableMongoRepositories public class AdministrationApplication { - public static void main(String[] args) { SpringApplication.run(AdministrationApplication.class, args); } diff --git a/src/main/java/de/ozgcloud/admin/setting/AlfaSettingDTO.java b/src/main/java/de/ozgcloud/admin/setting/AlfaSettingDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..0e6e6fed08fee3d9a87e10a57461901bfea7f531 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/AlfaSettingDTO.java @@ -0,0 +1,14 @@ +package de.ozgcloud.admin.setting; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +class AlfaSettingDTO implements ApplicationSettingDTO { + @JsonProperty("ozgcloud.postfach.signatur") + @Builder.Default + private String signatur = ""; +} diff --git a/src/main/java/de/ozgcloud/admin/setting/ApplicationSettingDTO.java b/src/main/java/de/ozgcloud/admin/setting/ApplicationSettingDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..789f27b030e9b4e54ce96bdffb5107aa8345a684 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/ApplicationSettingDTO.java @@ -0,0 +1,4 @@ +package de.ozgcloud.admin.setting; + +interface ApplicationSettingDTO { +} diff --git a/src/main/java/de/ozgcloud/admin/settings/DataRestConfiguration.java b/src/main/java/de/ozgcloud/admin/setting/DataRestConfiguration.java similarity index 61% rename from src/main/java/de/ozgcloud/admin/settings/DataRestConfiguration.java rename to src/main/java/de/ozgcloud/admin/setting/DataRestConfiguration.java index 7c5d282338be512a5a0397f0950d30935e513dcf..c82d68e26f46afa10bd35a193fc9152dc62a0035 100644 --- a/src/main/java/de/ozgcloud/admin/settings/DataRestConfiguration.java +++ b/src/main/java/de/ozgcloud/admin/setting/DataRestConfiguration.java @@ -1,18 +1,20 @@ -package de.ozgcloud.admin.settings; +package de.ozgcloud.admin.setting; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; +import lombok.RequiredArgsConstructor; + @Configuration +@RequiredArgsConstructor public class DataRestConfiguration implements RepositoryRestConfigurer { - @Autowired - private SettingsValidator settingsValidator; + + private final SettingValidator settingValidator; @Override public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener v) { - v.addValidator("beforeSave", settingsValidator); - v.addValidator("beforeCreate", settingsValidator); + v.addValidator("beforeSave", settingValidator); + v.addValidator("beforeCreate", settingValidator); } } \ No newline at end of file diff --git a/src/main/java/de/ozgcloud/admin/settings/Settings.java b/src/main/java/de/ozgcloud/admin/setting/Setting.java similarity index 91% rename from src/main/java/de/ozgcloud/admin/settings/Settings.java rename to src/main/java/de/ozgcloud/admin/setting/Setting.java index 85c519591406cba2bd753b283e377aea4d8ab9ca..6bf42c624ded3481ee4d976352d8cece48fd6fee 100644 --- a/src/main/java/de/ozgcloud/admin/settings/Settings.java +++ b/src/main/java/de/ozgcloud/admin/setting/Setting.java @@ -19,7 +19,7 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.admin.settings; +package de.ozgcloud.admin.setting; import jakarta.validation.Valid; @@ -36,8 +36,8 @@ import lombok.extern.jackson.Jacksonized; @Builder @Getter @Jacksonized -@Document(Settings.COLLECTION_NAME) -public class Settings { +@Document(Setting.COLLECTION_NAME) +public class Setting { static final String COLLECTION_NAME = "settings"; @Id @@ -46,5 +46,5 @@ public class Settings { private String name; @Valid @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.EXTERNAL_PROPERTY, property = "name") - private SettingsBody settingsBody; + private SettingBody settingBody; } diff --git a/src/main/java/de/ozgcloud/admin/setting/SettingBody.java b/src/main/java/de/ozgcloud/admin/setting/SettingBody.java new file mode 100644 index 0000000000000000000000000000000000000000..d9aaa8b2b2a99ab518214bc3ade69ea78b9b2039 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/SettingBody.java @@ -0,0 +1,13 @@ +package de.ozgcloud.admin.setting; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonSubTypes.Type; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; + +@JsonSubTypes({ + @Type(value = PostfachSettingBody.class, name = "Postfach") +}) +public interface SettingBody { + +} diff --git a/src/main/java/de/ozgcloud/admin/settings/SettingsConstants.java b/src/main/java/de/ozgcloud/admin/setting/SettingConstants.java similarity index 94% rename from src/main/java/de/ozgcloud/admin/settings/SettingsConstants.java rename to src/main/java/de/ozgcloud/admin/setting/SettingConstants.java index d12906c039760748fd9904a2bc9a3ccdf9f66259..521f39ad36f7a9672133d265d17e07b7a8cec837 100644 --- a/src/main/java/de/ozgcloud/admin/settings/SettingsConstants.java +++ b/src/main/java/de/ozgcloud/admin/setting/SettingConstants.java @@ -19,13 +19,13 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.admin.settings; +package de.ozgcloud.admin.setting; import lombok.AccessLevel; import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class SettingsConstants { +public class SettingConstants { static final String REL = "settings"; diff --git a/src/main/java/de/ozgcloud/admin/setting/SettingEnvironmentRepository.java b/src/main/java/de/ozgcloud/admin/setting/SettingEnvironmentRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..0843cb38ab3701c0d46106324654d97af1afe889 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/SettingEnvironmentRepository.java @@ -0,0 +1,55 @@ +package de.ozgcloud.admin.setting; + +import java.util.Map; +import java.util.Optional; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.environment.PropertySource; +import org.springframework.cloud.config.server.environment.EnvironmentRepository; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Component +public class SettingEnvironmentRepository implements EnvironmentRepository { + + private final SettingService settingService; + + private final ObjectMapper objectMapper; + + private static final String ALFA = "Alfa"; + private static final String VORGANG_MANAGER = "OzgCloud_VorgangManager"; + + @Override + public Environment findOne(String application, String profile, String label) { + return buildEnvironment(application, findAnwendungSettingDTO(application)); + } + + Optional<ApplicationSettingDTO> findAnwendungSettingDTO(String application) { + switch (application) { + case ALFA: + return Optional.of(settingService.getAlfaSettingDTO()); + case VORGANG_MANAGER: + return Optional.of(settingService.getVorgangManagerSettingDTO()); + default: + return Optional.empty(); + } + } + + Environment buildEnvironment(String application, Optional<ApplicationSettingDTO> settingDTO) { + var environment = new Environment(application); + settingDTO.ifPresent(setting -> environment.add(transformToPropertySource(setting))); + return environment; + } + + PropertySource transformToPropertySource(ApplicationSettingDTO settingDTO) { + return new PropertySource(StringUtils.EMPTY, objectMapper.convertValue(settingDTO, new TypeReference<Map<String, Object>>() { + })); + } + +} diff --git a/src/main/java/de/ozgcloud/admin/setting/SettingMapper.java b/src/main/java/de/ozgcloud/admin/setting/SettingMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..f0f1ba6273a836a1a3fc94af1bd73a4fa3e2b1c0 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/SettingMapper.java @@ -0,0 +1,21 @@ +package de.ozgcloud.admin.setting; + +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.ReportingPolicy; + +import de.ozgcloud.admin.setting.postfach.Absender; +import de.ozgcloud.admin.setting.postfach.AbsenderVorgangManager; +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; + +@Mapper(unmappedTargetPolicy = ReportingPolicy.WARN) +interface SettingMapper { + + AlfaSettingDTO.AlfaSettingDTOBuilder mapAlfaPostfach(@MappingTarget AlfaSettingDTO.AlfaSettingDTOBuilder builder, + PostfachSettingBody postfachData); + + VorgangManagerSettingDTO.VorgangManagerSettingDTOBuilder mapVorgangManagerPostfach( + @MappingTarget VorgangManagerSettingDTO.VorgangManagerSettingDTOBuilder builder, PostfachSettingBody postfachData); + + AbsenderVorgangManager mapAbsenderToAbsenderVorgangManager(Absender absender); +} diff --git a/src/main/java/de/ozgcloud/admin/settings/SettingsRepository.java b/src/main/java/de/ozgcloud/admin/setting/SettingRepository.java similarity index 78% rename from src/main/java/de/ozgcloud/admin/settings/SettingsRepository.java rename to src/main/java/de/ozgcloud/admin/setting/SettingRepository.java index 722039cce0407f83ea7e4edb87943f705dbf4f6f..0094e0ecf2375606e486eea2ab7fc28e06ffb8f3 100644 --- a/src/main/java/de/ozgcloud/admin/settings/SettingsRepository.java +++ b/src/main/java/de/ozgcloud/admin/setting/SettingRepository.java @@ -19,12 +19,15 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.admin.settings; +package de.ozgcloud.admin.setting; + +import java.util.Optional; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; -@RepositoryRestResource(collectionResourceRel = SettingsConstants.REL, path = SettingsConstants.PATH) -interface SettingsRepository extends MongoRepository<Settings, String> { +@RepositoryRestResource(collectionResourceRel = SettingConstants.REL, path = SettingConstants.PATH) +interface SettingRepository extends MongoRepository<Setting, String> { + Optional<Setting> findOneByName(String name); } diff --git a/src/main/java/de/ozgcloud/admin/setting/SettingService.java b/src/main/java/de/ozgcloud/admin/setting/SettingService.java new file mode 100644 index 0000000000000000000000000000000000000000..dcc8eecae72acc320c4b22f01cbf699c1cf266db --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/SettingService.java @@ -0,0 +1,31 @@ +package de.ozgcloud.admin.setting; + +import org.springframework.stereotype.Service; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +class SettingService { + private static final String POSTFACH_SETTING_ITEM_NAME = "Postfach"; + + private final SettingRepository repository; + + private final SettingMapper mapper; + + public AlfaSettingDTO getAlfaSettingDTO() { + var postfachData = getSettingWithPostfachFromDb(); + return mapper.mapAlfaPostfach(AlfaSettingDTO.builder(), postfachData).build(); + } + + public VorgangManagerSettingDTO getVorgangManagerSettingDTO() { + var postfachData = getSettingWithPostfachFromDb(); + return mapper.mapVorgangManagerPostfach(VorgangManagerSettingDTO.builder(), postfachData).build(); + } + + PostfachSettingBody getSettingWithPostfachFromDb() { + var postfach = repository.findOneByName(POSTFACH_SETTING_ITEM_NAME); + return postfach.map(Setting::getSettingBody).map(PostfachSettingBody.class::cast).orElse(PostfachSettingBody.builder().build()); + } +} diff --git a/src/main/java/de/ozgcloud/admin/settings/SettingsValidator.java b/src/main/java/de/ozgcloud/admin/setting/SettingValidator.java similarity index 83% rename from src/main/java/de/ozgcloud/admin/settings/SettingsValidator.java rename to src/main/java/de/ozgcloud/admin/setting/SettingValidator.java index e4b91f801e5d04f32b8d74e4d552d8440d05d957..6a8c9951f5dae99a5383ca0c77ad6922a2232961 100644 --- a/src/main/java/de/ozgcloud/admin/settings/SettingsValidator.java +++ b/src/main/java/de/ozgcloud/admin/setting/SettingValidator.java @@ -1,4 +1,4 @@ -package de.ozgcloud.admin.settings; +package de.ozgcloud.admin.setting; import jakarta.validation.ConstraintViolationException; import jakarta.validation.Validation; @@ -8,11 +8,11 @@ import org.springframework.validation.Errors; import org.springframework.validation.Validator; @Component -class SettingsValidator implements Validator { +class SettingValidator implements Validator { @Override public boolean supports(Class<?> clazz) { - return Settings.class.equals(clazz); + return Setting.class.equals(clazz); } @Override diff --git a/src/main/java/de/ozgcloud/admin/setting/VorgangManagerSettingDTO.java b/src/main/java/de/ozgcloud/admin/setting/VorgangManagerSettingDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..aaaabb35fe0929efbd8b6c684f4d281530b28879 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/VorgangManagerSettingDTO.java @@ -0,0 +1,15 @@ +package de.ozgcloud.admin.setting; + +import com.fasterxml.jackson.annotation.JsonUnwrapped; + +import de.ozgcloud.admin.setting.postfach.AbsenderVorgangManager; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +class VorgangManagerSettingDTO implements ApplicationSettingDTO { + @JsonUnwrapped(prefix = "ozgcloud.postfach.absender.") + @Builder.Default + private AbsenderVorgangManager absender = AbsenderVorgangManager.builder().build(); +} diff --git a/src/main/java/de/ozgcloud/admin/settings/postfach/Absender.java b/src/main/java/de/ozgcloud/admin/setting/postfach/Absender.java similarity index 89% rename from src/main/java/de/ozgcloud/admin/settings/postfach/Absender.java rename to src/main/java/de/ozgcloud/admin/setting/postfach/Absender.java index c5be084a5b87420612dcbef48ae3a57359b015e4..d29b58267320ab47f1d8220b5234841e84948fa5 100644 --- a/src/main/java/de/ozgcloud/admin/settings/postfach/Absender.java +++ b/src/main/java/de/ozgcloud/admin/setting/postfach/Absender.java @@ -1,4 +1,4 @@ -package de.ozgcloud.admin.settings.postfach; +package de.ozgcloud.admin.setting.postfach; import static de.ozgcloud.admin.common.errorhandling.ValidationMessageCodes.*; @@ -11,7 +11,7 @@ import lombok.extern.jackson.Jacksonized; @Getter @Builder @Jacksonized -class Absender { +public class Absender { @NotEmpty(message = FIELD_IS_EMPTY) private String name; @NotEmpty(message = FIELD_IS_EMPTY) diff --git a/src/main/java/de/ozgcloud/admin/setting/postfach/AbsenderVorgangManager.java b/src/main/java/de/ozgcloud/admin/setting/postfach/AbsenderVorgangManager.java new file mode 100644 index 0000000000000000000000000000000000000000..1efc59b8a55df0f26cb99e08814bc90fafea6b5c --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/postfach/AbsenderVorgangManager.java @@ -0,0 +1,22 @@ +package de.ozgcloud.admin.setting.postfach; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class AbsenderVorgangManager { + @Builder.Default + private String name = ""; + @Builder.Default + private String anschrift = ""; + @Builder.Default + private String dienst = ""; + @Builder.Default + private String mandant = ""; + @Builder.Default + @JsonProperty("gemeinde-schluessel") + private String gemeindeschluessel = ""; +} diff --git a/src/main/java/de/ozgcloud/admin/setting/postfach/PostfachSettingBody.java b/src/main/java/de/ozgcloud/admin/setting/postfach/PostfachSettingBody.java new file mode 100644 index 0000000000000000000000000000000000000000..a7ea5da7a5ff695e9dd6a921479cba380335d1f2 --- /dev/null +++ b/src/main/java/de/ozgcloud/admin/setting/postfach/PostfachSettingBody.java @@ -0,0 +1,20 @@ +package de.ozgcloud.admin.setting.postfach; + +import jakarta.validation.Valid; + +import org.springframework.data.annotation.TypeAlias; + +import de.ozgcloud.admin.setting.SettingBody; +import lombok.Builder; +import lombok.Getter; +import lombok.extern.jackson.Jacksonized; + +@Getter +@Jacksonized +@Builder +@TypeAlias("Postfach") +public class PostfachSettingBody implements SettingBody { + @Valid + private Absender absender; + private String signatur; +} diff --git a/src/main/java/de/ozgcloud/admin/settings/AdminEnvironmentRepository.java b/src/main/java/de/ozgcloud/admin/settings/AdminEnvironmentRepository.java deleted file mode 100644 index 78d539beba3d9ad97e9197f0fc2efd86b5d99934..0000000000000000000000000000000000000000 --- a/src/main/java/de/ozgcloud/admin/settings/AdminEnvironmentRepository.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.ozgcloud.admin.settings; - -import java.util.List; - -import org.springframework.cloud.config.environment.Environment; -import org.springframework.cloud.config.environment.PropertySource; -import org.springframework.cloud.config.server.environment.EnvironmentRepository; -import org.springframework.stereotype.Component; - -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -@Component -public class AdminEnvironmentRepository implements EnvironmentRepository { - - private final PropertyRepository propertyRepository; - - @Override - public Environment findOne(String application, String profile, String label) { - List<ConfigurationProperties> properties = propertyRepository.findAllByApplication(application); - return buildEnvironment(application, properties); - } - - Environment buildEnvironment(String application, List<ConfigurationProperties> properties) { - var environment = new Environment(application); - environment.addAll(properties.stream().map(this::createPropertySource).toList()); - return environment; - } - - PropertySource createPropertySource(ConfigurationProperties configurationProperties) { - return new PropertySource(null, configurationProperties.getSource()); - } - -} diff --git a/src/main/java/de/ozgcloud/admin/settings/ConfigurationProperties.java b/src/main/java/de/ozgcloud/admin/settings/ConfigurationProperties.java deleted file mode 100644 index aec9487ddc5e61d46dc64ca82e6df0488981ca15..0000000000000000000000000000000000000000 --- a/src/main/java/de/ozgcloud/admin/settings/ConfigurationProperties.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.ozgcloud.admin.settings; - -import java.util.Map; - -import org.springframework.data.mongodb.core.mapping.Document; - -import lombok.Builder; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -@Builder -@Document(ConfigurationProperties.COLLECTION_NAME) -public class ConfigurationProperties { - public static final String COLLECTION_NAME = "configuration_properties"; - private String application; - private String profile; - private Map<String, Object> source; -} diff --git a/src/main/java/de/ozgcloud/admin/settings/PropertyRepository.java b/src/main/java/de/ozgcloud/admin/settings/PropertyRepository.java deleted file mode 100644 index 69b124411a0b340c7f2920705d3698390c46391b..0000000000000000000000000000000000000000 --- a/src/main/java/de/ozgcloud/admin/settings/PropertyRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.ozgcloud.admin.settings; - -import java.util.List; - -import org.springframework.data.mongodb.repository.MongoRepository; - -interface PropertyRepository extends MongoRepository<ConfigurationProperties, String> { - - List<ConfigurationProperties> findAllByApplication(String application); - -} \ No newline at end of file diff --git a/src/main/java/de/ozgcloud/admin/settings/SettingsBody.java b/src/main/java/de/ozgcloud/admin/settings/SettingsBody.java deleted file mode 100644 index f007d95301abb9f33a262299ff864188d1008967..0000000000000000000000000000000000000000 --- a/src/main/java/de/ozgcloud/admin/settings/SettingsBody.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.ozgcloud.admin.settings; - -import com.fasterxml.jackson.annotation.JsonSubTypes; -import com.fasterxml.jackson.annotation.JsonSubTypes.Type; - -import de.ozgcloud.admin.settings.postfach.Postfach; - -@JsonSubTypes({ - @Type(value = Postfach.class, name = "Postfach") -}) -public interface SettingsBody { - -} diff --git a/src/main/java/de/ozgcloud/admin/settings/postfach/Postfach.java b/src/main/java/de/ozgcloud/admin/settings/postfach/Postfach.java deleted file mode 100644 index f764f208307e71d8865e5ebf9bb6dd35c7408ff0..0000000000000000000000000000000000000000 --- a/src/main/java/de/ozgcloud/admin/settings/postfach/Postfach.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.ozgcloud.admin.settings.postfach; - -import jakarta.validation.Valid; - -import de.ozgcloud.admin.settings.SettingsBody; -import lombok.Builder; -import lombok.Getter; -import lombok.extern.jackson.Jacksonized; - -@Getter -@Jacksonized -@Builder -public class Postfach implements SettingsBody { - @Valid - private Absender absender; - private String signatur; -} diff --git a/src/test/java/de/ozgcloud/admin/RootControllerTest.java b/src/test/java/de/ozgcloud/admin/RootControllerTest.java index f703d1a3c3e3f4ab96c340516a66f519626cf187..88e2bb4aada0d7fd5b28034dd77b3254f5e127d3 100644 --- a/src/test/java/de/ozgcloud/admin/RootControllerTest.java +++ b/src/test/java/de/ozgcloud/admin/RootControllerTest.java @@ -30,11 +30,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.info.BuildProperties; import org.springframework.hateoas.EntityModel; import org.springframework.test.web.servlet.MockMvc; @@ -43,7 +41,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import lombok.SneakyThrows; -@ExtendWith(MockitoExtension.class) class RootControllerTest { @Spy diff --git a/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java b/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java index 6510e8f1187d204a062143130590313f075771d0..b237432d1fc03ba9b2ea3b5afa924bc706ed54c6 100644 --- a/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java +++ b/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java @@ -31,17 +31,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties; import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; -@ExtendWith(MockitoExtension.class) class RootModelAssemblerTest { private static final String BASE_PATH = "/api/base"; diff --git a/src/test/java/de/ozgcloud/admin/environment/EnvironmentControllerTest.java b/src/test/java/de/ozgcloud/admin/environment/EnvironmentControllerTest.java index bdeaf83b00de2410112833f1447e940fc088e9b5..1fa9815f84c8421d85cc65035f63ff377d2d0523 100644 --- a/src/test/java/de/ozgcloud/admin/environment/EnvironmentControllerTest.java +++ b/src/test/java/de/ozgcloud/admin/environment/EnvironmentControllerTest.java @@ -7,13 +7,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -21,7 +19,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import de.ozgcloud.admin.RootController; import lombok.SneakyThrows; -@ExtendWith(MockitoExtension.class) class EnvironmentControllerTest { @Spy diff --git a/src/test/java/de/ozgcloud/admin/security/AdminAuthenticationEntryPointTest.java b/src/test/java/de/ozgcloud/admin/security/AdminAuthenticationEntryPointTest.java index cd032501d67fa6a4bd3ce55d2082bc7eca1d8277..3b582efa9b75efeba55009589eb2754747abb044 100644 --- a/src/test/java/de/ozgcloud/admin/security/AdminAuthenticationEntryPointTest.java +++ b/src/test/java/de/ozgcloud/admin/security/AdminAuthenticationEntryPointTest.java @@ -35,13 +35,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -53,8 +51,7 @@ import org.springframework.web.servlet.function.ServerResponse.Context; import lombok.SneakyThrows; -@ExtendWith(MockitoExtension.class) -public class AdminAuthenticationEntryPointTest { +class AdminAuthenticationEntryPointTest { private static final String REQUEST_URI = "/request-uri"; diff --git a/src/test/java/de/ozgcloud/admin/setting/AlfaSettingDTOTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/AlfaSettingDTOTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..b00c52b64f139c94e90a101aba3bdb8c66df6313 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/AlfaSettingDTOTestFactory.java @@ -0,0 +1,13 @@ +package de.ozgcloud.admin.setting; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBodyTestFactory; + +public class AlfaSettingDTOTestFactory { + public static AlfaSettingDTO create() { + return createBuilder().build(); + } + + public static AlfaSettingDTO.AlfaSettingDTOBuilder createBuilder() { + return AlfaSettingDTO.builder().signatur(PostfachSettingBodyTestFactory.SIGNATUR); + } +} diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentITCase.java b/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..0f8ca42b3cd6517c895bb9da385bd49c200b3c5e --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentITCase.java @@ -0,0 +1,69 @@ + +package de.ozgcloud.admin.setting; + +import static org.assertj.core.api.Assertions.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBodyTestFactory; +import de.ozgcloud.common.test.DataITCase; +import lombok.SneakyThrows; + +@DataITCase +@AutoConfigureMockMvc +@WithMockUser +class SettingEnvironmentITCase { + @Autowired + private MockMvc mockMvc; + + @Autowired + private SettingRepository settingRepository; + + private Setting settingWithPostfach = SettingTestFactory.createBuilder() + .name("Postfach") + .settingBody(PostfachSettingBodyTestFactory.create()) + .build(); + + @Test + @SneakyThrows + void shouldHaveHttpEndpoint() { + var result = mockMvc.perform(get("/configserver/example/path")); + + result.andExpect(status().isOk()); + } + + @Nested + class TestFindOne { + @BeforeEach + void fillDb() { + settingRepository.deleteAll(); + settingRepository.save(settingWithPostfach); + + } + + @Test + @SneakyThrows + void shouldReturnValuesForVorgangManager() { + var result = mockMvc.perform(get("/configserver/OzgCloud_VorgangManager-profile.yaml")); + + assertThat(result.andReturn().getResponse().getContentAsString()).isEqualTo(YamlTestFactory.createVorgangManagerYaml()); + } + + @Test + @SneakyThrows + void shouldReturnValuesForAlfa() { + var result = mockMvc.perform(get("/configserver/Alfa-any.yaml")); + + assertThat(result.andReturn().getResponse().getContentAsString().replaceAll("\\s+", " ")) + .isEqualTo(YamlTestFactory.createAlfaYaml().replaceAll("\\s+", " ")); + } + } +} \ No newline at end of file diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentRepositoryTest.java b/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4bb93d6f696c096c9b537ac4943c41539031aaff --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentRepositoryTest.java @@ -0,0 +1,201 @@ +package de.ozgcloud.admin.setting; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.util.Map; +import java.util.Optional; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.environment.PropertySource; + +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import com.fasterxml.jackson.databind.ObjectMapper; + +import lombok.Getter; + +class SettingEnvironmentRepositoryTest { + @InjectMocks + @Spy + private SettingEnvironmentRepository repository; + @Spy + private ObjectMapper objectMapper; + @Mock + private SettingService service; + @Mock + private ApplicationSettingDTO mockedSettingDTO; + @Mock + private Environment mockedEnvironment; + @Mock + private PropertySource mockedPropertySource; + + private static final String ALFA = "Alfa"; + private static final String VORGANG_MANAGER = "OzgCloud_VorgangManager"; + + @Nested + class TestFindOne { + private static final String ANY_APPLICATION = "any application"; + + @Test + void shouldCallGetAnwendungsSettingDTO() { + repository.findOne(ANY_APPLICATION, "any profile", "any label"); + + verify(repository).findAnwendungSettingDTO(ANY_APPLICATION); + + } + + @Test + void shouldCallBuildEnvironment() { + doReturn(Optional.of(mockedSettingDTO)).when(repository).findAnwendungSettingDTO(any()); + doReturn(mockedEnvironment).when(repository).buildEnvironment(any(), any()); + + repository.findOne(ANY_APPLICATION, "any profile", "any label"); + + verify(repository).buildEnvironment(ANY_APPLICATION, Optional.of(mockedSettingDTO)); + } + + @Test + void shouldReturnBuiltEnvironment() { + doReturn(Optional.of(mockedSettingDTO)).when(repository).findAnwendungSettingDTO(any()); + doReturn(mockedEnvironment).when(repository).buildEnvironment(any(), any()); + + var returnedEnvironment = repository.findOne(ANY_APPLICATION, "any profile", "any label"); + + assertThat(returnedEnvironment).isEqualTo(mockedEnvironment); + } + + } + + @Nested + class TestFindAnwendungsSettingDTO { + + @Test + void shouldReturnEmptyOptionalForUnsupportedApplication() { + var returnedSettingDTO = repository.findAnwendungSettingDTO("UnsupportedApp"); + + assertThat(returnedSettingDTO).isEmpty(); + } + + @Nested + class TestWithAlfaApplication { + @Mock + private AlfaSettingDTO mockedAlfaSettingDTO; + + @BeforeEach + void mockService() { + when(service.getAlfaSettingDTO()).thenReturn(mockedAlfaSettingDTO); + } + + @Test + void shouldOnlyCallGetAlfaSettingDTO() { + repository.findAnwendungSettingDTO(ALFA); + + verify(service).getAlfaSettingDTO(); + verify(service, never()).getVorgangManagerSettingDTO(); + } + + @Test + void shouldReturnAlfaSettingDTO() { + var returnedSettingDTO = repository.findAnwendungSettingDTO(ALFA); + + assertThat(returnedSettingDTO).hasValue(mockedAlfaSettingDTO); + } + } + + @Nested + class TestWithVorgangManagerApplication { + @Mock + private VorgangManagerSettingDTO mockedVorgangManagerSettingDTO; + + @BeforeEach + void mockService() { + when(service.getVorgangManagerSettingDTO()).thenReturn(mockedVorgangManagerSettingDTO); + } + + @Test + void shouldOnlyCallGetVorgangManagerSettingDTO() { + repository.findAnwendungSettingDTO(VORGANG_MANAGER); + + verify(service, never()).getAlfaSettingDTO(); + verify(service).getVorgangManagerSettingDTO(); + } + + @Test + void shouldReturnAlfaSettingDTO() { + var returnedSettingDTO = repository.findAnwendungSettingDTO(VORGANG_MANAGER); + + assertThat(returnedSettingDTO).hasValue(mockedVorgangManagerSettingDTO); + } + } + } + + @Nested + class TestBuildEnvironment { + private static final String APPLICATION_NAME = "app"; + + @BeforeEach + void mockTransformToPropertySource() { + doReturn(mockedPropertySource).when(repository).transformToPropertySource(mockedSettingDTO); + } + + @Test + void shouldCallTransformToPropertySource() { + repository.buildEnvironment(APPLICATION_NAME, Optional.of(mockedSettingDTO)); + + verify(repository).transformToPropertySource(mockedSettingDTO); + } + + @Test + void shouldReturnBuiltEnvironment() { + var expectedEnvironemnt = new Environment(APPLICATION_NAME); + expectedEnvironemnt.add(mockedPropertySource); + + var resultEnvironment = repository.buildEnvironment(APPLICATION_NAME, Optional.of(mockedSettingDTO)); + + assertThat(resultEnvironment).usingRecursiveComparison().isEqualTo(expectedEnvironemnt); + } + } + + @Nested + class TestTransformToPropertySource { + private ApplicationSettingDTO flatSettingDTO = new ApplicationSettingDTO() { + @Getter + private String attribute = "outerValue"; + }; + + private ApplicationSettingDTO nestedSettingDTO = new ApplicationSettingDTO() { + @JsonUnwrapped(prefix = "attribute.") + @Getter + private ApplicationSettingDTO attribute = new ApplicationSettingDTO() { + @Getter + private String nestedAttribute = "innerValue"; + }; + }; + + @Test + void shouldReturnPropertySourceOfFlatObject() { + var expectedPropertySource = new PropertySource(StringUtils.EMPTY, Map.of("attribute", "outerValue")); + + var resultPropertySource = repository.transformToPropertySource(flatSettingDTO); + + assertThat(resultPropertySource).usingRecursiveComparison().isEqualTo(expectedPropertySource); + } + + @Test + void shouldReturnPropertySourceOfNestedObject() { + var expectedPropertySource = new PropertySource(StringUtils.EMPTY, Map.of("attribute.nestedAttribute", "innerValue")); + + var resultPropertySource = repository.transformToPropertySource(nestedSettingDTO); + + assertThat(resultPropertySource).usingRecursiveComparison().isEqualTo(expectedPropertySource); + } + } +} diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingITCase.java b/src/test/java/de/ozgcloud/admin/setting/SettingITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..7bdbbf2ffc2120affc2cb715f47da788ced89619 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/SettingITCase.java @@ -0,0 +1,356 @@ +/* + * Copyright (c) 2024. Das Land Schleswig-Holstein vertreten durch das Ministerium für Energiewende, Klimaschutz, Umwelt und Natur + * Zentrales IT-Management + * + * Lizenziert unter der EUPL, Version 1.2 oder - sobald + * diese von der Europäischen Kommission genehmigt wurden - + * Folgeversionen der EUPL ("Lizenz"); + * Sie dürfen dieses Werk ausschließlich gemäß + * dieser Lizenz nutzen. + * Eine Kopie der Lizenz finden Sie hier: + * + * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * Sofern nicht durch anwendbare Rechtsvorschriften + * gefordert oder in schriftlicher Form vereinbart, wird + * die unter der Lizenz verbreitete Software "so wie sie + * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - + * ausdrücklich oder stillschweigend - verbreitet. + * Die sprachspezifischen Genehmigungen und Beschränkungen + * unter der Lizenz sind dem Lizenztext zu entnehmen. + */ +package de.ozgcloud.admin.setting; + +import static org.assertj.core.api.Assertions.*; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import de.ozgcloud.admin.setting.postfach.Absender; +import de.ozgcloud.admin.setting.postfach.AbsenderTestFactory; +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; +import de.ozgcloud.admin.setting.postfach.PostfachSettingBodyTestFactory; +import de.ozgcloud.common.test.DataITCase; +import lombok.SneakyThrows; + +@DataITCase +@AutoConfigureMockMvc +@WithMockUser +class SettingITCase { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private MongoOperations mongoOperations; + + @Autowired + private RepositoryRestProperties restProperties; + + @Nested + class TestRestRepo { + + private String id; + + @BeforeEach + void init() { + mongoOperations.dropCollection(Setting.class); + id = mongoOperations.save(SettingTestFactory.create()).getId(); + } + + @Nested + class TestGet { + + @Test + @SneakyThrows + void shouldHaveStatusOkForExisting() { + var result = performGet(id); + + result.andExpect(status().isOk()); + } + + @Test + @SneakyThrows + void shouldHaveStatusNotFoundForNonExisting() { + var result = performGet("unknown"); + + result.andExpect(status().is(HttpStatus.NOT_FOUND.value())); + } + + @SneakyThrows + private ResultActions performGet(String id) { + return mockMvc.perform(get(String.join("/", restProperties.getBasePath(), SettingConstants.PATH, id))); + } + } + } + + @Nested + class TestForSettingWithPostfach { + private static final String POSTFACH_NAME = "Postfach"; + + private Setting settingWithPostfach = SettingTestFactory.createBuilder() + .name(POSTFACH_NAME) + .settingBody(PostfachSettingBodyTestFactory.create()) + .build(); + + @BeforeEach + void clear() { + mongoOperations.dropCollection(Setting.class); + } + + @Nested + class TestSave { + + @Test + @SneakyThrows + void shouldHaveResponseStatusCreated() { + var result = performPost(settingWithPostfach); + + result.andExpect(status().isCreated()); + } + + @Test + @SneakyThrows + void shouldCreateSettingItem() { + performPost(settingWithPostfach); + + assertThat(getSettingWithPostfachFromDb()) + .usingRecursiveComparison().ignoringFields("id").isEqualTo(settingWithPostfach); + } + + @Nested + class TestPostfachSetting { + + @Test + @SneakyThrows + void shouldBeInstanceOfPostfach() { + performPost(settingWithPostfach); + + assertThat(getSettingWithPostfachFromDb() + .getSettingBody()).isInstanceOf(PostfachSettingBody.class); + } + + @Test + @SneakyThrows + void shouldCreateEmptySignatur() { + var postfachWithEmptySignatur = PostfachSettingBodyTestFactory.createBuilder() + .signatur(StringUtils.EMPTY).build(); + var settingWithPostfachEmptySignatur = createSettingWithPostfach(postfachWithEmptySignatur); + + performPost(settingWithPostfachEmptySignatur); + + assertThat(getPostfachFromDb().getSignatur()).isEmpty(); + } + + private PostfachSettingBody getPostfachFromDb() { + return (PostfachSettingBody) getSettingWithPostfachFromDb().getSettingBody(); + } + + @Nested + class TestAbsenderValidation { + @Test + @SneakyThrows + void shouldReturnUnprocessableEntityOnEmptyName() { + var absenderWithEmptyName = AbsenderTestFactory.createBuilder().name(StringUtils.EMPTY).build(); + var settingWithPostfachEmptyAbsenderName = createSettingWithPostfachAbsender(absenderWithEmptyName); + + var result = performPost(settingWithPostfachEmptyAbsenderName); + + result.andExpect(status().isUnprocessableEntity()); + } + + @Test + @SneakyThrows + void shouldReturnUnprocessableEntityOnEmptyAnschrift() { + var absenderWithEmptyAnschrift = AbsenderTestFactory.createBuilder().anschrift(StringUtils.EMPTY).build(); + var settingWithPostfachEmptyAnschrift = createSettingWithPostfachAbsender(absenderWithEmptyAnschrift); + + var result = performPost(settingWithPostfachEmptyAnschrift); + + result.andExpect(status().isUnprocessableEntity()); + } + + @Test + @SneakyThrows + void shouldReturnUnprocessableEntityOnEmptyDienst() { + var absenderWithEmptyDienst = AbsenderTestFactory.createBuilder().dienst(StringUtils.EMPTY).build(); + var settingWithPostfachEmptyDienst = createSettingWithPostfachAbsender(absenderWithEmptyDienst); + + var result = performPost(settingWithPostfachEmptyDienst); + + result.andExpect(status().isUnprocessableEntity()); + } + + @Test + @SneakyThrows + void shouldReturnUnprocessableEntityOnEmptyMandant() { + var absenderWithEmptyMandant = AbsenderTestFactory.createBuilder().mandant(StringUtils.EMPTY).build(); + var settingWithPostfachEmptyMandant = createSettingWithPostfachAbsender(absenderWithEmptyMandant); + + var result = performPost(settingWithPostfachEmptyMandant); + + result.andExpect(status().isUnprocessableEntity()); + } + + @Test + @SneakyThrows + void shouldReturnUnprocessableEntityOnEmptyGemeindeSchluessel() { + var absenderWithEmptyGemeindeschluessel = AbsenderTestFactory.createBuilder().gemeindeschluessel(StringUtils.EMPTY).build(); + var settingWithPostfachEmptyGemeindeschluessel = createSettingWithPostfachAbsender(absenderWithEmptyGemeindeschluessel); + + var result = performPost(settingWithPostfachEmptyGemeindeschluessel); + + result.andExpect(status().isUnprocessableEntity()); + } + + private Setting createSettingWithPostfachAbsender(Absender absender) { + var postfach = PostfachSettingBodyTestFactory.createBuilder().absender(absender).build(); + return createSettingWithPostfach(postfach); + } + + } + + private Setting createSettingWithPostfach(PostfachSettingBody postfach) { + return SettingTestFactory.createBuilder().name(POSTFACH_NAME).settingBody(postfach).build(); + } + } + + @SneakyThrows + private ResultActions performPost(Setting setting) { + var postBody = convertSettingToString(setting); + return mockMvc.perform(post(String.join("/", restProperties.getBasePath(), + SettingConstants.PATH)) + .with(csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(postBody)); + } + } + + @Nested + class TestGet { + private String id; + @Autowired + private ObjectMapper mapper; + + @BeforeEach + void init() { + id = mongoOperations.save(settingWithPostfach).getId(); + } + + @Test + @SneakyThrows + void shouldHaveStatusOkForExisting() { + var result = performGet(); + + result.andExpect(status().isOk()); + } + + @Test + @SneakyThrows + void shouldHaveInstanceOfPostfach() { + + var result = performGet(); + + assertThat(mapper.readValue(result.andReturn().getResponse().getContentAsString(), Setting.class).getSettingBody()) + .isInstanceOf(PostfachSettingBody.class); + } + + @SneakyThrows + private ResultActions performGet() { + return mockMvc.perform(get(String.join("/", restProperties.getBasePath(), SettingConstants.PATH, id))); + } + } + + @Nested + class TestPut { + private String id; + private PostfachSettingBody updatedPostfach = PostfachSettingBodyTestFactory.createBuilder() + .absender(AbsenderTestFactory.createBuilder() + .name("Neuer Name") + .anschrift("Neue Anschrift") + .build()) + .build(); + private Setting updatedSetting = SettingTestFactory.createBuilder() + .name(POSTFACH_NAME) + .settingBody(updatedPostfach) + .build(); + + @BeforeEach + void init() { + id = mongoOperations.save(settingWithPostfach).getId(); + } + + @Test + @SneakyThrows + void shouldHaveStatusNoContent() { + var result = performPut(); + + result.andExpect(status().isNoContent()); + } + + @Test + @SneakyThrows + void shouldHaveUpdatedSetting() { + performPut(); + + assertThat(getSettingWithPostfachFromDb()) + .usingRecursiveComparison().ignoringFields("id").isEqualTo(updatedSetting); + } + + @Test + @SneakyThrows + void shouldHaveExactlyOnePostfachSetting() { + performPut(); + + List<Setting> postfachSettings = mongoOperations.find(createQueryForPostfach(), Setting.class); + + assertThat(postfachSettings).hasSize(1); + } + + @SneakyThrows + private ResultActions performPut() { + var body = convertSettingToString(updatedSetting); + return mockMvc.perform(put(String.join("/", restProperties.getBasePath(), SettingConstants.PATH, id)) + .with(csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(body)); + } + + } + + private Setting getSettingWithPostfachFromDb() { + return mongoOperations.findOne(createQueryForPostfach(), Setting.class); + } + + private Query createQueryForPostfach() { + return new Query().addCriteria(Criteria.where("name").in(POSTFACH_NAME)); + } + + @SneakyThrows + private String convertSettingToString(Setting setting) { + return SettingTestFactory.buildSettingJson(setting, + PostfachSettingBodyTestFactory.buildPostfachJson((PostfachSettingBody) setting.getSettingBody())); + } + } + +} \ No newline at end of file diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingMapperTest.java b/src/test/java/de/ozgcloud/admin/setting/SettingMapperTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d19e81a2dc480db8bfc2e8d6e1daefc7e001392c --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/SettingMapperTest.java @@ -0,0 +1,41 @@ +package de.ozgcloud.admin.setting; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; +import org.mockito.Spy; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; +import de.ozgcloud.admin.setting.postfach.PostfachSettingBodyTestFactory; + +class SettingMapperTest { + private final PostfachSettingBody postfach = PostfachSettingBodyTestFactory.create(); + private final AlfaSettingDTO alfaSettingDTO = AlfaSettingDTOTestFactory.create(); + private final VorgangManagerSettingDTO vorgangManagerSettingDTO = VorgangManagerSettingDTOTestFactory.create(); + + @Spy + private final SettingMapper mapper = Mappers.getMapper(SettingMapper.class); + + @Nested + class TestMapPostfachToAlfaSetting { + @Test + void shouldReturnFlattenedAlfaMap() { + var resultAlfaSetting = mapper.mapAlfaPostfach(AlfaSettingDTO.builder(), postfach).build(); + + assertThat(resultAlfaSetting).usingRecursiveComparison().isEqualTo(alfaSettingDTO); + } + } + + @Nested + class TestMapPostfachToVorgangManagerSetting { + @Test + void shouldReturnFlattenedVorgangManagerMap() { + var resultVorgangmanagerSetting = mapper.mapVorgangManagerPostfach(VorgangManagerSettingDTO.builder(), postfach).build(); + + assertThat(resultVorgangmanagerSetting).usingRecursiveComparison().isEqualTo(vorgangManagerSettingDTO); + } + } + +} diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingRepositoryITCase.java b/src/test/java/de/ozgcloud/admin/setting/SettingRepositoryITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..f255693e6e13a86d8add0dc4ab326eadfbc250d6 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/SettingRepositoryITCase.java @@ -0,0 +1,36 @@ +package de.ozgcloud.admin.setting; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; + +import de.ozgcloud.common.test.DataITCase; + +@DataITCase +class SettingRepositoryITCase { + @Autowired + private MongoOperations mongoOperations; + @Autowired + private SettingRepository repository; + + private final static String SETTING_NAME = "Name"; + private Setting setting = SettingTestFactory.createBuilder() + .name(SETTING_NAME) + .build(); + + @Nested + class TestFindOneByName { + @Test + void shouldGetSavedData() { + mongoOperations.dropCollection(Setting.COLLECTION_NAME); + mongoOperations.save(setting); + + var settingFromDb = repository.findOneByName(SETTING_NAME).get(); + + assertThat(settingFromDb).usingRecursiveComparison().isEqualTo(setting); + } + } +} diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java b/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..00af130bc3b191d9c1ff8b02af5812c628922662 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java @@ -0,0 +1,150 @@ +package de.ozgcloud.admin.setting; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.util.Optional; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; +import de.ozgcloud.admin.setting.postfach.PostfachSettingBodyTestFactory; + +class SettingServiceTest { + + @InjectMocks + @Spy + private SettingService service; + + @Mock + private SettingRepository repository; + + @Mock + private SettingMapper mapper; + + private final PostfachSettingBody postfach = PostfachSettingBodyTestFactory.create(); + + private static final String POSTFACH = "Postfach"; + private final Setting settingWithPostfach = SettingTestFactory.createBuilder().name(POSTFACH).settingBody(postfach).build(); + + @Nested + class TestGetAlfaSettingDTO { + @Captor + private ArgumentCaptor<AlfaSettingDTO.AlfaSettingDTOBuilder> alfaBuilderCaptor; + + private final AlfaSettingDTO alfaSetting = AlfaSettingDTOTestFactory.create(); + + @BeforeEach + void mockGetSettingWithPostfachFromDb() { + doReturn(postfach).when(service).getSettingWithPostfachFromDb(); + } + + @Test + void shouldCallGetSettingWithPostfachFromDb() { + when(mapper.mapAlfaPostfach(any(), any())).thenReturn(AlfaSettingDTOTestFactory.createBuilder()); + + service.getAlfaSettingDTO(); + + verify(service).getSettingWithPostfachFromDb(); + } + + @Test + void shouldCallMapperWithEmptyBuilderAndPostfach() { + when(mapper.mapAlfaPostfach(any(), any())).thenReturn(AlfaSettingDTOTestFactory.createBuilder()); + + service.getAlfaSettingDTO(); + + verify(mapper).mapAlfaPostfach(alfaBuilderCaptor.capture(), eq(postfach)); + assertThat(alfaBuilderCaptor.getValue()).usingRecursiveComparison().isEqualTo(AlfaSettingDTO.builder()); + } + + @Test + void shouldReturnBuiltAlfaSettingDTO() { + when(mapper.mapAlfaPostfach(any(), any())).thenReturn(AlfaSettingDTOTestFactory.createBuilder()); + + var resultSettingDTO = service.getAlfaSettingDTO(); + + assertThat(resultSettingDTO).usingRecursiveComparison().isEqualTo(alfaSetting); + } + } + + @Nested + class TestGetVorgangManagerSettingDTO { + @Captor + private ArgumentCaptor<VorgangManagerSettingDTO.VorgangManagerSettingDTOBuilder> vorgangManagerBuilderCaptor; + + private final VorgangManagerSettingDTO vorgangManagerSetting = VorgangManagerSettingDTOTestFactory.create(); + + @BeforeEach + void mockGetSettingWithPostfachFromDb() { + doReturn(postfach).when(service).getSettingWithPostfachFromDb(); + } + + @BeforeEach + void mockMapper() { + when(mapper.mapVorgangManagerPostfach(any(), any())).thenReturn(VorgangManagerSettingDTOTestFactory.createBuilder()); + } + + @Test + void shouldCallGetSettingWithPostfachFromDb() { + service.getVorgangManagerSettingDTO(); + + verify(service).getSettingWithPostfachFromDb(); + } + + @Test + void shouldCallMapperWithEmptyBuilderAndPostfach() { + service.getVorgangManagerSettingDTO(); + + verify(mapper).mapVorgangManagerPostfach(vorgangManagerBuilderCaptor.capture(), eq(postfach)); + assertThat(vorgangManagerBuilderCaptor.getValue()).usingRecursiveComparison().isEqualTo(VorgangManagerSettingDTO.builder()); + } + + @Test + void shouldReturnBuiltVorgangManagerSettingDTO() { + var resultSettingDTO = service.getVorgangManagerSettingDTO(); + + assertThat(resultSettingDTO).usingRecursiveComparison().isEqualTo(vorgangManagerSetting); + } + + } + + @Nested + class TestGetSettingWithPostfachFromDb { + @Test + void shouldCallRepository() { + when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(settingWithPostfach)); + + service.getSettingWithPostfachFromDb(); + + verify(repository).findOneByName(POSTFACH); + } + + @Test + void shouldReturnPostfachSettingBody() { + when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(settingWithPostfach)); + + var returnedBody = service.getSettingWithPostfachFromDb(); + + assertThat(returnedBody).isEqualTo(postfach); + } + + @Test + void shouldReturnEmptyPostfachSettingBodyForEmptySetting() { + when(repository.findOneByName(POSTFACH)).thenReturn(Optional.empty()); + + var returnedBody = service.getSettingWithPostfachFromDb(); + + assertThat(returnedBody).usingRecursiveComparison().isEqualTo(PostfachSettingBody.builder().build()); + } + } + +} diff --git a/src/test/java/de/ozgcloud/admin/settings/SettingsTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/SettingTestFactory.java similarity index 75% rename from src/test/java/de/ozgcloud/admin/settings/SettingsTestFactory.java rename to src/test/java/de/ozgcloud/admin/setting/SettingTestFactory.java index ddf73db41640f52d74bd08155f4415581278cffa..023102f84527913302dd14d68acab990f0849bb0 100644 --- a/src/test/java/de/ozgcloud/admin/settings/SettingsTestFactory.java +++ b/src/test/java/de/ozgcloud/admin/setting/SettingTestFactory.java @@ -19,21 +19,21 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -package de.ozgcloud.admin.settings; +package de.ozgcloud.admin.setting; import de.ozgcloud.common.test.TestUtils; -public class SettingsTestFactory { +public class SettingTestFactory { - public static Settings create() { + public static Setting create() { return createBuilder().build(); } - public static Settings.SettingsBuilder createBuilder() { - return Settings.builder(); + public static Setting.SettingBuilder createBuilder() { + return Setting.builder(); } - public static String buildSettingsJson(Settings settings, String settingBodyString) { - return TestUtils.loadTextFile("jsonTemplates/settings/createSettings.json.tmpl", settings.getName(), settingBodyString); + public static String buildSettingJson(Setting setting, String settingBodyString) { + return TestUtils.loadTextFile("jsonTemplates/settings/createSettings.json.tmpl", setting.getName(), settingBodyString); } } diff --git a/src/test/java/de/ozgcloud/admin/setting/VorgangManagerSettingDTOTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/VorgangManagerSettingDTOTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..88b58eeedf6006f967346bb5356137c8963e99bd --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/VorgangManagerSettingDTOTestFactory.java @@ -0,0 +1,16 @@ +package de.ozgcloud.admin.setting; + +import de.ozgcloud.admin.setting.postfach.AbsenderVorgangManager; +import de.ozgcloud.admin.setting.postfach.AbsenderVorgangManagerTestFactory; + +public class VorgangManagerSettingDTOTestFactory { + public static AbsenderVorgangManager ABSENDER_VM = AbsenderVorgangManagerTestFactory.create(); + + public static VorgangManagerSettingDTO create() { + return createBuilder().build(); + } + + public static VorgangManagerSettingDTO.VorgangManagerSettingDTOBuilder createBuilder() { + return VorgangManagerSettingDTO.builder().absender(ABSENDER_VM); + } +} diff --git a/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..e46098b9caef41f936cce70994c983172f51b5cf --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java @@ -0,0 +1,22 @@ +package de.ozgcloud.admin.setting; + +import de.ozgcloud.admin.setting.postfach.PostfachSettingBody; +import de.ozgcloud.admin.setting.postfach.PostfachSettingBodyTestFactory; +import de.ozgcloud.common.test.TestUtils; + +public class YamlTestFactory { + public static PostfachSettingBody postfach = PostfachSettingBodyTestFactory.create(); + + public static String createVorgangManagerYaml() { + return TestUtils.loadTextFile("yamlTemplates/settings/vorgangManager.yaml.tmpl", + postfach.getAbsender().getName(), + postfach.getAbsender().getAnschrift(), + postfach.getAbsender().getDienst(), + postfach.getAbsender().getMandant(), + postfach.getAbsender().getGemeindeschluessel()); + } + + public static String createAlfaYaml() { + return TestUtils.loadTextFile("yamlTemplates/settings/alfa.yaml.tmpl", postfach.getSignatur()); + } +} diff --git a/src/test/java/de/ozgcloud/admin/settings/postfach/AbsenderTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/postfach/AbsenderTestFactory.java similarity index 94% rename from src/test/java/de/ozgcloud/admin/settings/postfach/AbsenderTestFactory.java rename to src/test/java/de/ozgcloud/admin/setting/postfach/AbsenderTestFactory.java index d048fb4c1b12ab988c88540ede24d3cd1b21e06a..abff05656418c3dbe9ef04e76ae155f56e930ab4 100644 --- a/src/test/java/de/ozgcloud/admin/settings/postfach/AbsenderTestFactory.java +++ b/src/test/java/de/ozgcloud/admin/setting/postfach/AbsenderTestFactory.java @@ -1,4 +1,4 @@ -package de.ozgcloud.admin.settings.postfach; +package de.ozgcloud.admin.setting.postfach; import java.util.Random; diff --git a/src/test/java/de/ozgcloud/admin/setting/postfach/AbsenderVorgangManagerTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/postfach/AbsenderVorgangManagerTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..268ec8acafa399a8caae5ecfeb01c5a88b52f7e9 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/postfach/AbsenderVorgangManagerTestFactory.java @@ -0,0 +1,16 @@ +package de.ozgcloud.admin.setting.postfach; + +public class AbsenderVorgangManagerTestFactory { + public static AbsenderVorgangManager create() { + return createBuilder().build(); + } + + public static AbsenderVorgangManager.AbsenderVorgangManagerBuilder createBuilder() { + return AbsenderVorgangManager.builder() + .name(AbsenderTestFactory.NAME) + .anschrift(AbsenderTestFactory.ANSCHRIFT) + .dienst(AbsenderTestFactory.DIENST) + .mandant(AbsenderTestFactory.MANDANT) + .gemeindeschluessel(AbsenderTestFactory.GEMEINDESCHLUESSEL); + } +} \ No newline at end of file diff --git a/src/test/java/de/ozgcloud/admin/setting/postfach/PostfachSettingBodyTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/postfach/PostfachSettingBodyTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..97b877744f8f0c4d2d2966cebe628331ca351104 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/setting/postfach/PostfachSettingBodyTestFactory.java @@ -0,0 +1,40 @@ +package de.ozgcloud.admin.setting.postfach; + +import java.util.Map; + +import com.thedeanda.lorem.LoremIpsum; + +import de.ozgcloud.common.test.TestUtils; + +public class PostfachSettingBodyTestFactory { + public static final Absender ABSENDER = AbsenderTestFactory.create(); + public static final String SIGNATUR = LoremIpsum.getInstance().getHtmlParagraphs(1, 2); + + public static PostfachSettingBody create() { + return createBuilder().build(); + } + + public static PostfachSettingBody.PostfachSettingBodyBuilder createBuilder() { + return PostfachSettingBody.builder() + .absender(ABSENDER) + .signatur(SIGNATUR); + } + + public static String buildPostfachJson(PostfachSettingBody postfach) { + return TestUtils.loadTextFile("jsonTemplates/settings/createPostfach.json.tmpl", postfach.getAbsender().getName(), + postfach.getAbsender().getAnschrift(), postfach.getAbsender().getDienst(), postfach.getAbsender().getMandant(), + postfach.getAbsender().getGemeindeschluessel(), postfach.getSignatur()); + } + + public static Map<String, Object> createPostfachAsFlattenedMapVorgangManager() { + return Map.of("ozgcloud.postfach.absender.name", ABSENDER.getName(), + "ozgcloud.postfach.absender.anschrift", ABSENDER.getAnschrift(), + "ozgcloud.postfach.absender.dienst", ABSENDER.getDienst(), + "ozgcloud.postfach.absender.mandant", ABSENDER.getMandant(), + "ozgcloud.postfach.absender.gemeinde-schluessel", ABSENDER.getGemeindeschluessel()); + } + + public static Map<String, Object> createPostfachAsFlattenedMapAlfa() { + return Map.of("ozgcloud.postfach.signatur", SIGNATUR); + } +} diff --git a/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentITCase.java b/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentITCase.java deleted file mode 100644 index bb562e80385ddcef643285c9ff78f1cacabc3f79..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentITCase.java +++ /dev/null @@ -1,30 +0,0 @@ - -package de.ozgcloud.admin.settings; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.web.servlet.MockMvc; - -import de.ozgcloud.common.test.DataITCase; -import lombok.SneakyThrows; - -@DataITCase -@AutoConfigureMockMvc -@WithMockUser -class AdminEnvironmentITCase { - @Autowired - private MockMvc mockMvc; - - @Test - @SneakyThrows - void shouldHaveHttpEndpoint() { - var result = mockMvc.perform(get("/configserver/example/path")); - - result.andExpect(status().isOk()); - } -} \ No newline at end of file diff --git a/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentRepositoryITCase.java b/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentRepositoryITCase.java deleted file mode 100644 index a894dec5af5e10e2a510daae05ebba6f508977cc..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentRepositoryITCase.java +++ /dev/null @@ -1,85 +0,0 @@ -package de.ozgcloud.admin.settings; - -import static org.assertj.core.api.Assertions.*; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.config.environment.Environment; -import org.springframework.cloud.config.server.environment.EnvironmentRepository; -import org.springframework.security.test.context.support.WithMockUser; - -import de.ozgcloud.common.test.DataITCase; - -@DataITCase -@WithMockUser -class AdminEnvironmentRepositoryITCase { - - @Autowired - private EnvironmentRepository repository; - @Autowired - private PropertyRepository propertyRepository; - - private ConfigurationProperties configurationProperties = ConfigurationPropertiesTestFactory.create(); - - @AfterEach - void clearData() { - propertyRepository.deleteAll(); - } - - @Nested - class TestFindOne { - @BeforeEach - void initDb() { - saveMongoSource(configurationProperties); - } - - @Test - void shouldFindEnvironmentWithOneCorrectPropertySource() { - - var environment = findOneEnvironment(); - - assertThat(environment.getPropertySources()).hasSize(1); - } - - @Test - void shouldFindEnvironmentWithTwoCorrectPropertySources() { - var configurationProperties2 = ConfigurationPropertiesTestFactory.createBuilder().profile("other").build(); - saveMongoSource(configurationProperties2); - - var environment = findOneEnvironment(); - - assertThat(environment.getPropertySources()).hasSize(2); - } - - private void saveMongoSource(ConfigurationProperties configurationProperties) { - propertyRepository.save(configurationProperties); - } - - @Test - void shouldFindEnvironmentWithoutPropertySources() { - - var environment = repository.findOne("FalseAppName", ConfigurationPropertiesTestFactory.PROFILE, null); - - assertThat(environment.getPropertySources()).isEmpty(); - - } - - @Test - void shouldFindCorrectEnvironment() { - - var environment = findOneEnvironment(); - - assertThat(environment.getPropertySources().getLast().getSource()).isEqualTo(ConfigurationPropertiesTestFactory.PROPERTIES); - } - - private Environment findOneEnvironment() { - return repository.findOne(ConfigurationPropertiesTestFactory.APPNAME, ConfigurationPropertiesTestFactory.PROFILE, - null); - } - - } - -} diff --git a/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentRepositoryTest.java b/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentRepositoryTest.java deleted file mode 100644 index bc10bdbd739bbd215f54f352c9413b77b3186c3b..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/AdminEnvironmentRepositoryTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package de.ozgcloud.admin.settings; - -import static org.assertj.core.api.Assertions.*; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -public class AdminEnvironmentRepositoryTest { - - @InjectMocks - private AdminEnvironmentRepository repository; - - private ConfigurationProperties configurationProperties = ConfigurationPropertiesTestFactory.create(); - - @Nested - class TestBuildEnvironment { - @Test - void shouldHaveCorrectAppName() { - - var environment = repository.buildEnvironment(ConfigurationPropertiesTestFactory.APPNAME, new ArrayList<ConfigurationProperties>()); - - assertThat(environment.getName()).isEqualTo(ConfigurationPropertiesTestFactory.APPNAME); - } - - @Test - void shouldHaveCorrectNumberOfPropertySources() { - - var environment = repository.buildEnvironment(ConfigurationPropertiesTestFactory.APPNAME, - List.of(configurationProperties, configurationProperties)); - - assertThat(environment.getPropertySources()).hasSize(2); - } - - @Test - void shouldHandleEmptyConfigurationList() { - - var environment = repository.buildEnvironment(ConfigurationPropertiesTestFactory.APPNAME, new ArrayList<ConfigurationProperties>()); - - assertThat(environment.getPropertySources()).isEmpty(); - - } - } - - @Nested - class TestCreatePropertySource { - @Test - void shouldHaveCorrectSource() { - - var propertySource = repository.createPropertySource(configurationProperties); - - assertThat(propertySource.getSource()).isEqualTo(configurationProperties.getSource()); - } - - @Test - void shouldHaveCorrectName() { - - var propertySource = repository.createPropertySource(configurationProperties); - - assertThat(propertySource.getName()).isNull(); - } - } - -} diff --git a/src/test/java/de/ozgcloud/admin/settings/ConfigurationPropertiesTestFactory.java b/src/test/java/de/ozgcloud/admin/settings/ConfigurationPropertiesTestFactory.java deleted file mode 100644 index c1db07dd6f33924272dae792d6cac31eed53dc6e..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/ConfigurationPropertiesTestFactory.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.ozgcloud.admin.settings; - -import java.util.Map; - -public class ConfigurationPropertiesTestFactory { - public static final String APPNAME = "testapp"; - public static final String PROFILE = "testprofile"; - public static final String PROPERTY1 = "property1"; - public static final String VALUE1 = "value1"; - public static Map<String, Object> PROPERTIES = Map.of(PROPERTY1, VALUE1); - - public static ConfigurationProperties create() { - return createBuilder().build(); - } - - public static ConfigurationProperties.ConfigurationPropertiesBuilder createBuilder() { - return ConfigurationProperties.builder() - .application(APPNAME) - .profile(PROFILE) - .source(PROPERTIES); - } -} diff --git a/src/test/java/de/ozgcloud/admin/settings/SettingsITCase.java b/src/test/java/de/ozgcloud/admin/settings/SettingsITCase.java deleted file mode 100644 index 8b21102c73aaec826c2d6a62d0fd5ab59620556f..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/SettingsITCase.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2024. Das Land Schleswig-Holstein vertreten durch das Ministerium für Energiewende, Klimaschutz, Umwelt und Natur - * Zentrales IT-Management - * - * Lizenziert unter der EUPL, Version 1.2 oder - sobald - * diese von der Europäischen Kommission genehmigt wurden - - * Folgeversionen der EUPL ("Lizenz"); - * Sie dürfen dieses Werk ausschließlich gemäß - * dieser Lizenz nutzen. - * Eine Kopie der Lizenz finden Sie hier: - * - * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 - * - * Sofern nicht durch anwendbare Rechtsvorschriften - * gefordert oder in schriftlicher Form vereinbart, wird - * die unter der Lizenz verbreitete Software "so wie sie - * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - - * ausdrücklich oder stillschweigend - verbreitet. - * Die sprachspezifischen Genehmigungen und Beschränkungen - * unter der Lizenz sind dem Lizenztext zu entnehmen. - */ -package de.ozgcloud.admin.settings; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.http.HttpStatus; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; - -import de.ozgcloud.common.test.DataITCase; -import lombok.SneakyThrows; - -@DataITCase -@AutoConfigureMockMvc -@WithMockUser -class SettingsITCase { - - @Autowired - private MockMvc mockMvc; - - @Autowired - private MongoOperations mongoOperations; - - @Autowired - private RepositoryRestProperties restProperties; - - @Nested - class TestRestRepo { - - @BeforeEach - void init() { - mongoOperations.dropCollection(Settings.class); - mongoOperations.save(SettingsTestFactory.create()); - } - - @Test - @SneakyThrows - void shouldHaveStatusOkForExisting() { - var id = mongoOperations.findAll(Settings.class).get(0).getId(); - - var result = doPerform(id); - - result.andExpect(status().isOk()); - } - - @Test - @SneakyThrows - void shouldHaveStatusNotFoundForNonExisting() { - var result = doPerform("unknown"); - - result.andExpect(status().is(HttpStatus.NOT_FOUND.value())); - } - - @SneakyThrows - private ResultActions doPerform(String id) { - return mockMvc.perform(get(String.join("/", restProperties.getBasePath(), SettingsConstants.PATH, id))); - } - } - -} \ No newline at end of file diff --git a/src/test/java/de/ozgcloud/admin/settings/postfach/PostfachITCase.java b/src/test/java/de/ozgcloud/admin/settings/postfach/PostfachITCase.java deleted file mode 100644 index a00766eebadd1f1b62e1fad29bd46b85ab07ccc6..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/postfach/PostfachITCase.java +++ /dev/null @@ -1,291 +0,0 @@ -package de.ozgcloud.admin.settings.postfach; - -import static org.assertj.core.api.Assertions.*; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.http.MediaType; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import de.ozgcloud.admin.settings.Settings; -import de.ozgcloud.admin.settings.SettingsBody; -import de.ozgcloud.admin.settings.SettingsConstants; -import de.ozgcloud.admin.settings.SettingsTestFactory; -import de.ozgcloud.common.test.DataITCase; -import lombok.SneakyThrows; - -@DataITCase -@AutoConfigureMockMvc -@WithMockUser -class PostfachITCase { - private static final String POSTFACH_NAME = "Postfach"; - - @Autowired - private MockMvc mockMvc; - - @Autowired - private MongoOperations mongoOperations; - - @Autowired - private RepositoryRestProperties restProperties; - - private Settings settingsWithPostfach = SettingsTestFactory.createBuilder() - .name(POSTFACH_NAME) - .settingsBody(PostfachTestFactory.create()) - .build(); - - @BeforeEach - void clear() { - mongoOperations.dropCollection(Settings.class); - } - - @Nested - class TestSave { - - @Test - @SneakyThrows - void shouldHaveResponseStatusCreated() { - var result = performPost(settingsWithPostfach); - - result.andExpect(status().isCreated()); - } - - @Test - @SneakyThrows - void shouldCreateSettingsItem() { - performPost(settingsWithPostfach); - - assertThat(getSettingWithPostfachFromDb()) - .usingRecursiveComparison().ignoringFields("id").isEqualTo(settingsWithPostfach); - } - - @Nested - class TestPostfachSettings { - - @Test - @SneakyThrows - void shouldBeInstanceOfPostfach() { - performPost(settingsWithPostfach); - - assertThat(getSettingWithPostfachFromDb() - .getSettingsBody()).isInstanceOf(Postfach.class); - } - - @Test - @SneakyThrows - void shouldCreateEmptySignatur() { - var postfachWithEmptySignatur = PostfachTestFactory.createBuilder() - .signatur(StringUtils.EMPTY).build(); - var settingsWithPostfachEmptySignatur = createSettingsWithPostfach(postfachWithEmptySignatur); - - performPost(settingsWithPostfachEmptySignatur); - - assertThat(getPostfachFromDb().getSignatur()).isEmpty(); - } - - private Postfach getPostfachFromDb() { - return (Postfach) getSettingWithPostfachFromDb().getSettingsBody(); - } - - @Nested - class TestAbsenderValidation { - @Test - @SneakyThrows - void shouldReturnUnprocessableEntityOnEmptyName() { - var absenderWithEmptyName = AbsenderTestFactory.createBuilder().name(StringUtils.EMPTY).build(); - var settingsWithPostfachEmptyAbsenderName = createSettingsWithPostfachAbsender(absenderWithEmptyName); - - var result = performPost(settingsWithPostfachEmptyAbsenderName); - - result.andExpect(status().isUnprocessableEntity()); - } - - @Test - @SneakyThrows - void shouldReturnUnprocessableEntityOnEmptyAnschrift() { - var absenderWithEmptyAnschrift = AbsenderTestFactory.createBuilder().anschrift(StringUtils.EMPTY).build(); - var settingsWithPostfachEmptyAnschrift = createSettingsWithPostfachAbsender(absenderWithEmptyAnschrift); - - var result = performPost(settingsWithPostfachEmptyAnschrift); - - result.andExpect(status().isUnprocessableEntity()); - } - - @Test - @SneakyThrows - void shouldReturnUnprocessableEntityOnEmptyDienst() { - var absenderWithEmptyDienst = AbsenderTestFactory.createBuilder().dienst(StringUtils.EMPTY).build(); - var settingsWithPostfachEmptyDienst = createSettingsWithPostfachAbsender(absenderWithEmptyDienst); - - var result = performPost(settingsWithPostfachEmptyDienst); - - result.andExpect(status().isUnprocessableEntity()); - } - - @Test - @SneakyThrows - void shouldReturnUnprocessableEntityOnEmptyMandant() { - var absenderWithEmptyMandant = AbsenderTestFactory.createBuilder().mandant(StringUtils.EMPTY).build(); - var setttingsWithPostfachEmptyMandant = createSettingsWithPostfachAbsender(absenderWithEmptyMandant); - - var result = performPost(setttingsWithPostfachEmptyMandant); - - result.andExpect(status().isUnprocessableEntity()); - } - - @Test - @SneakyThrows - void shouldReturnUnprocessableEntityOnEmptyGemeindeSchluessel() { - var absenderWithEmptyGemeindeschluessel = AbsenderTestFactory.createBuilder().gemeindeschluessel(StringUtils.EMPTY).build(); - var settingsWithPostfachEmptyGemeindeschluessel = createSettingsWithPostfachAbsender(absenderWithEmptyGemeindeschluessel); - - var result = performPost(settingsWithPostfachEmptyGemeindeschluessel); - - result.andExpect(status().isUnprocessableEntity()); - } - - private Settings createSettingsWithPostfachAbsender(Absender absender) { - var postfach = PostfachTestFactory.createBuilder().absender(absender).build(); - return createSettingsWithPostfach(postfach); - } - - } - - private Settings createSettingsWithPostfach(SettingsBody postfach) { - return SettingsTestFactory.createBuilder().name(POSTFACH_NAME).settingsBody(postfach).build(); - } - } - - @SneakyThrows - private ResultActions performPost(Settings setting) { - var postBody = convertSettingToString(setting); - return mockMvc.perform(post(String.join("/", restProperties.getBasePath(), - SettingsConstants.PATH)) - .with(csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(postBody)); - } - } - - @Nested - class TestGet { - private String id; - @Autowired - private ObjectMapper mapper; - - @BeforeEach - void init() { - id = mongoOperations.save(settingsWithPostfach).getId(); - } - - @Test - @SneakyThrows - void shouldHaveStatusOkForExisting() { - var result = performGet(); - - result.andExpect(status().isOk()); - } - - @Test - @SneakyThrows - void shouldHaveInstanceOfPostfach() { - - var result = performGet(); - - assertThat(mapper.readValue(result.andReturn().getResponse().getContentAsString(), Settings.class).getSettingsBody()) - .isInstanceOf(Postfach.class); - } - - @SneakyThrows - private ResultActions performGet() { - return mockMvc.perform(get(String.join("/", restProperties.getBasePath(), SettingsConstants.PATH, id))); - } - } - - @Nested - class TestPut { - private String id; - private Postfach updatedPostfach = PostfachTestFactory.createBuilder() - .absender(AbsenderTestFactory.createBuilder() - .name("Neuer Name") - .anschrift("Neue Anschrift") - .build()) - .build(); - private Settings updatedSettings = SettingsTestFactory.createBuilder() - .name(POSTFACH_NAME) - .settingsBody(updatedPostfach) - .build(); - - @BeforeEach - void init() { - id = mongoOperations.save(settingsWithPostfach).getId(); - } - - @Test - @SneakyThrows - void shouldHaveStatusNoContent() { - var result = performPut(); - - result.andExpect(status().isNoContent()); - } - - @Test - @SneakyThrows - void shouldHaveUpdatedSettings() { - performPut(); - - assertThat(getSettingWithPostfachFromDb()) - .usingRecursiveComparison().ignoringFields("id").isEqualTo(updatedSettings); - } - - @Test - @SneakyThrows - void shouldHaveExactlyOnePostfachSetting() { - performPut(); - - List<Settings> postfachSettings = mongoOperations.find(createQueryForPostfach(), Settings.class); - - assertThat(postfachSettings).hasSize(1); - } - - @SneakyThrows - private ResultActions performPut() { - var body = convertSettingToString(updatedSettings); - return mockMvc.perform(put(String.join("/", restProperties.getBasePath(), SettingsConstants.PATH, id)) - .with(csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(body)); - } - - } - - private Settings getSettingWithPostfachFromDb() { - return mongoOperations.findOne(createQueryForPostfach(), Settings.class); - } - - private Query createQueryForPostfach() { - return new Query().addCriteria(Criteria.where("name").in(POSTFACH_NAME)); - } - - @SneakyThrows - private String convertSettingToString(Settings setting) { - return SettingsTestFactory.buildSettingsJson(setting, PostfachTestFactory.buildPostfachJson((Postfach) setting.getSettingsBody())); - } -} diff --git a/src/test/java/de/ozgcloud/admin/settings/postfach/PostfachTestFactory.java b/src/test/java/de/ozgcloud/admin/settings/postfach/PostfachTestFactory.java deleted file mode 100644 index 4f82a2a55e84d76d63b49d54be9ee8100a76b498..0000000000000000000000000000000000000000 --- a/src/test/java/de/ozgcloud/admin/settings/postfach/PostfachTestFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.ozgcloud.admin.settings.postfach; - -import com.thedeanda.lorem.LoremIpsum; - -import de.ozgcloud.common.test.TestUtils; - -public class PostfachTestFactory { - public static final Absender ABSENDER = AbsenderTestFactory.create(); - public static final String SIGNATUR = LoremIpsum.getInstance().getHtmlParagraphs(1, 2); - - public static Postfach create() { - return createBuilder().build(); - } - - public static Postfach.PostfachBuilder createBuilder() { - return Postfach.builder() - .absender(ABSENDER) - .signatur(SIGNATUR); - } - - public static String buildPostfachJson(Postfach postfach) { - return TestUtils.loadTextFile("jsonTemplates/settings/createPostfach.json.tmpl", postfach.getAbsender().getName(), - postfach.getAbsender().getAnschrift(), postfach.getAbsender().getDienst(), postfach.getAbsender().getMandant(), - postfach.getAbsender().getGemeindeschluessel(), postfach.getSignatur()); - } - -} diff --git a/src/test/resources/META-INF/spring/org.mockito.junit.jupiter.MockitoExtension b/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension similarity index 100% rename from src/test/resources/META-INF/spring/org.mockito.junit.jupiter.MockitoExtension rename to src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension diff --git a/src/test/resources/jsonTemplates/settings/createSettings.json.tmpl b/src/test/resources/jsonTemplates/settings/createSettings.json.tmpl index a0dd5cfd77775267d1f40a54948810e02ac4e766..3c36aa163c522f47080816a55dcaaa01b758ddb6 100644 --- a/src/test/resources/jsonTemplates/settings/createSettings.json.tmpl +++ b/src/test/resources/jsonTemplates/settings/createSettings.json.tmpl @@ -1,4 +1,4 @@ { "name" : "%s", - "settingsBody" : %s + "settingBody" : %s } \ No newline at end of file diff --git a/src/test/resources/yamlTemplates/settings/alfa.yaml.tmpl b/src/test/resources/yamlTemplates/settings/alfa.yaml.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..65b37e576b646c76bf03a83b18b8ba8e911cfe4d --- /dev/null +++ b/src/test/resources/yamlTemplates/settings/alfa.yaml.tmpl @@ -0,0 +1,3 @@ +ozgcloud: + postfach: + signatur: %s diff --git a/src/test/resources/yamlTemplates/settings/vorgangManager.yaml.tmpl b/src/test/resources/yamlTemplates/settings/vorgangManager.yaml.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..8bca4ba163aa5d632fc7d38c9b1af9998662993e --- /dev/null +++ b/src/test/resources/yamlTemplates/settings/vorgangManager.yaml.tmpl @@ -0,0 +1,8 @@ +ozgcloud: + postfach: + absender: + name: %s + anschrift: %s + dienst: %s + mandant: %s + gemeinde-schluessel: '%s'