diff --git a/src/main/java/org/etsi/osl/tmf/configuration/SwaggerDocumentationConfig.java b/src/main/java/org/etsi/osl/tmf/configuration/SwaggerDocumentationConfig.java index ea00cc6ac4e3ab952b6e0e4c683e40a62f738e70..e42863bc7222e211c91882e29736f63eea7688b4 100644 --- a/src/main/java/org/etsi/osl/tmf/configuration/SwaggerDocumentationConfig.java +++ b/src/main/java/org/etsi/osl/tmf/configuration/SwaggerDocumentationConfig.java @@ -22,7 +22,6 @@ package org.etsi.osl.tmf.configuration; import org.springdoc.core.customizers.OpenApiCustomizer; import org.springdoc.core.models.GroupedOpenApi; import org.springdoc.core.utils.SpringDocUtils; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -31,7 +30,6 @@ import io.swagger.v3.oas.annotations.security.OAuthFlow; import io.swagger.v3.oas.annotations.security.OAuthFlows; import io.swagger.v3.oas.annotations.security.OAuthScope; import io.swagger.v3.oas.annotations.security.SecurityScheme; -import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.ExternalDocumentation; import io.swagger.v3.oas.models.SpecVersion; import io.swagger.v3.oas.models.info.Info; @@ -811,8 +809,46 @@ public class SwaggerDocumentationConfig { .build(); } - - + + + /** + * TMF 673 Geographic Address Management + * @return + */ + + + @Bean + public OpenApiCustomizer gam673OpenAPI() { + return openApi -> openApi + .specVersion( SpecVersion.V30 ).addSecurityItem(new SecurityRequirement().addList("security_auth")) + .info(new Info() + .title("TMF 673 Geographic Address Management") + .description("## TMF API Reference: TMF673 - Geographic Address Management ### Release : 20.0 - May 2020 ") + .version("4.0.0") + .license(new License() + .name("Apache 2.0") + .url("http://openslice.io"))) + .externalDocs(new ExternalDocumentation() + .description("TMF API Tables") + .url("https://www.tmforum.org/oda/open-apis/table")); + } + + @Bean + public GroupedOpenApi gam673(){ + + SpringDocUtils.getConfig().replaceWithClass(java.time.LocalDate.class, java.sql.Date.class); + SpringDocUtils.getConfig().replaceWithClass(java.time.OffsetDateTime.class, java.util.Date.class); + + return GroupedOpenApi.builder() + .group("tmf-api-673-Geographic Address Management-v4.0.0") + .addOpenApiCustomizer( this.stm653OpenAPI() ) + .packagesToScan("org.etsi.osl.tmf.gam673.api") + .build(); + + } + + + // @Bean // public SecurityConfiguration security() { // return SecurityConfigurationBuilder.builder() diff --git a/src/main/java/org/etsi/osl/tmf/gam673/api/Generated.java b/src/main/java/org/etsi/osl/tmf/gam673/api/Generated.java new file mode 100644 index 0000000000000000000000000000000000000000..f860fd506397093f77efbc7020f8e5d026c66ad5 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/api/Generated.java @@ -0,0 +1,16 @@ +package org.etsi.osl.tmf.gam673.api; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Documented +@Retention(RUNTIME) +@Target({TYPE, METHOD, CONSTRUCTOR}) +public @interface Generated { +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressManagementApi.java b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressManagementApi.java new file mode 100644 index 0000000000000000000000000000000000000000..474b79d3f916a1664007cb24ce4c1452eadb7574 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressManagementApi.java @@ -0,0 +1,144 @@ +package org.etsi.osl.tmf.gam673.api; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.Valid; +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicSubAddress; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +@Generated +public interface GeographicAddressManagementApi { + default Optional getObjectMapper() { + return Optional.empty(); + } + + default Optional getRequest() { + return Optional.empty(); + } + + default Optional getAcceptHeader() { + return getRequest().map(r -> r.getHeader("Accept")); + } + + @Operation(summary = "Creates a 'GeographicAddress'", operationId = "createGeographicAddress", description = "", tags={ "GeographicAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode = "400", description = "Created" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @PostMapping(value = "/geographicAddress", + produces = { "application/json" }, + consumes = { "application/json" }) + ResponseEntity createGeographicAddress(@Parameter(description = "The geographic address to be created", required = true) @Valid @RequestBody GeographicAddress geographicAddress); + + @Operation(summary = "Deletes a 'GeographicAddress' by Id", operationId = "deleteGeographicAddress", description = "", tags={ "GeographicAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode = "204", description = "Deleted" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @DeleteMapping(value = "/geographicAddress/{id}", + produces = { "application/json" }) + ResponseEntity deleteGeographicAddress(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id); + + + @Operation(summary = "List or find 'GeographicAddress' objects", operationId = "listGeographicAddress", description = "" , tags={ "GeographicAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Ok" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @GetMapping(value = "/geographicAddress", + produces = { "application/json" }) + ResponseEntity> listGeographicAddress(); + + + @Operation(summary = "Updates partially a 'GeographicAddress' by Id", operationId = "patchGeographicAddress", description = "", tags={ "GeographicAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Updated" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @PatchMapping(value = "/geographicAddress/{id}", + produces = { "application/json" }, + consumes = { "application/json" }) + ResponseEntity patchGeographicAddress(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id, @Parameter(description = "The Service Level Specification to be updated", required = true) @Valid @RequestBody GeographicAddress geographicAddress); + + + @Operation(summary = "Retrieves a 'GeographicAddress' by Id", operationId = "retrieveGeographicAddress", description = "" , tags={ "GeographicAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Ok" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @GetMapping(value = "/geographicAddress/{id}", + produces = { "application/json" }) + ResponseEntity retrieveGeographicAddress(@Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id); + + @Operation(summary = "Retrieves a 'GeographicSubAddress' by Id", operationId = "retrieveGeographicSubAddress", description = "" , tags={ "GeographicSubAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Ok" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @GetMapping(value = "/geographicAddress/{id}/geographicSubAddress", + produces = { "application/json" }) + ResponseEntity> retrieveGeographicSubAddresses( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id); + + @Operation(summary = "Retrieves a 'GeographicSubAddress' by Id", operationId = "retrieveGeographicSubAddress", description = "" , tags={ "GeographicSubAddress", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Ok" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @GetMapping(value = "/geographicAddress/{geoAddressId}/geographicSubAddress/{geoSubAddressId}", + produces = { "application/json" }) + ResponseEntity retrieveGeographicSubAddress( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("geoAddressId") String geoAddressId, + @Parameter(description = "Identifier of the geographic sub address", required = true) @PathVariable("geoSubAddressId") String geoSubAddressId); + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressManagementApiController.java b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressManagementApiController.java new file mode 100644 index 0000000000000000000000000000000000000000..5541f5e0e8149192302ddca156fd529744994834 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressManagementApiController.java @@ -0,0 +1,135 @@ +package org.etsi.osl.tmf.gam673.api; + + +import io.swagger.v3.oas.annotations.Parameter; +import jakarta.validation.Valid; +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicSubAddress; +import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressManagementService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.util.List; +import java.util.Set; + +@Controller +@RequestMapping("/geographicAddressManagement/v4/") +public class GeographicAddressManagementApiController implements GeographicAddressManagementApi{ + private static final Logger log = LoggerFactory.getLogger(GeographicAddressManagementApiController.class); + + private static final String COULD_NOT_SERIALIZE="Couldn't serialize response for content type application/json"; + private final GeographicAddressManagementService geographicAddressManagementService; + + @Autowired + public GeographicAddressManagementApiController(GeographicAddressManagementService geographicAddressManagementService) { + this.geographicAddressManagementService = geographicAddressManagementService; + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity> listGeographicAddress() { + try { + return new ResponseEntity<>(geographicAddressManagementService.findAllGeographicAddress(), HttpStatus.OK); + + } catch (Exception e) { + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity createGeographicAddress( + @Parameter(description = "The geographic address to be created", required = true) @Valid @RequestBody GeographicAddress geographicAddress + ) { + try{ + GeographicAddress c = geographicAddressManagementService.createGeographicAddress(geographicAddress); + return new ResponseEntity<>(c, HttpStatus.OK); + } catch (Exception e) { + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity deleteGeographicAddress( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id) { + + try { + return new ResponseEntity<>(geographicAddressManagementService.deleteGeographicAddressById(id), HttpStatus.OK); + }catch (Exception e) { + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity retrieveGeographicAddress( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id) { + try{ + GeographicAddress c = geographicAddressManagementService.findGeographicAddressByUUID(id); + + return new ResponseEntity<>(c, HttpStatus.OK); + }catch (Exception e){ + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity> retrieveGeographicSubAddresses( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id) { + try{ + Set c = geographicAddressManagementService.findAllGeographicSubAddresses(id); + + return new ResponseEntity<>(c, HttpStatus.OK); + }catch (Exception e){ + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity patchGeographicAddress( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id, + @Parameter(description = "The Geographic Address to be updated", required = true) @Valid @RequestBody GeographicAddress geographicAddress) { + try{ + GeographicAddress c = geographicAddressManagementService.updateGeographicAddress(id, geographicAddress); + + return new ResponseEntity<>(c, HttpStatus.OK); + }catch (Exception e){ + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity retrieveGeographicSubAddress( + @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("geoAddressId") String geoAddressId, + @Parameter(description = "Identifier of the geographic sub address", required = true) @PathVariable("geoSubAddressId") String geoSubAddressId){ + try{ + GeographicSubAddress c = geographicAddressManagementService.findGeographicSubAddressByUUID(geoAddressId,geoSubAddressId); + + return new ResponseEntity<>(c, HttpStatus.OK); + }catch (Exception e){ + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressValidationManagementApi.java b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressValidationManagementApi.java new file mode 100644 index 0000000000000000000000000000000000000000..ce79126d5bcc9ff54c1b18c0c70a1968bb29054b --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressValidationManagementApi.java @@ -0,0 +1,111 @@ +package org.etsi.osl.tmf.gam673.api; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.Valid; +import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; +import java.util.Optional; + +@Generated +public interface GeographicAddressValidationManagementApi { + default Optional getObjectMapper() { + return Optional.empty(); + } + + default Optional getRequest() { + return Optional.empty(); + } + + default Optional getAcceptHeader() { + return getRequest().map(r -> r.getHeader("Accept")); + } + + @Operation(summary = "Creates a 'GeographicAddressValidation'", operationId = "createGeographicAddressValidation", description = "", tags={ "GeographicAddressValidation", }) + @ApiResponses(value = { + @ApiResponse(responseCode = "400", description = "Created" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @PostMapping(value = "/geographicAddressValidation", + produces = { "application/json" }, + consumes = { "application/json" }) + ResponseEntity createGeographicAddressValidation(@Parameter(description = "The geographic address to be created", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidation); + + @Operation(summary = "Deletes a 'GeographicAddressValidation' by Id", operationId = "deleteGeographicAddressValidation", description = "", tags={ "GeographicAddressValidation", }) + @ApiResponses(value = { + @ApiResponse(responseCode = "204", description = "Deleted" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @DeleteMapping(value = "/geographicAddressValidation/{id}", + produces = { "application/json" }) + ResponseEntity deleteGeographicAddressValidation(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id); + + + @Operation(summary = "List or find 'GeographicAddressValidation' objects", operationId = "listGeographicAddressValidation", description = "" , tags={ "GeographicAddressValidation", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Ok" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @GetMapping(value = "/geographicAddressValidation", + produces = { "application/json" }) + ResponseEntity> listGeographicAddressValidation(); + + + @Operation(summary = "Updates partially a 'GeographicAddressValidation' by Id", operationId = "patchGeographicAddressValidation", description = "", tags={ "GeographicAddressValidation", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Updated" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @PatchMapping(value = "/geographicAddressValidation/{id}", + produces = { "application/json" }, + consumes = { "application/json" }) + ResponseEntity patchGeographicAddressValidation(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id, @Parameter(description = "The Service Level Specification to be updated", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidation); + + + @Operation(summary = "Retrieves a 'GeographicAddressValidation' by Id", operationId = "retrieveGeographicAddressValidation", description = "" , tags={ "GeographicAddressValidation", }) + @ApiResponses(value = { + @ApiResponse(responseCode ="200", description = "Ok" ), + @ApiResponse(responseCode = "400", description = "Bad Request" ), + @ApiResponse(responseCode = "401", description = "Unauthorized" ), + @ApiResponse(responseCode = "403", description = "Forbidden" ), + @ApiResponse(responseCode = "404", description = "Not Found" ), + @ApiResponse(responseCode = "405", description = "Method Not allowed" ), + @ApiResponse(responseCode = "409", description = "Conflict" ), + @ApiResponse(responseCode = "500", description = "Internal Server Error" ) }) + @GetMapping(value = "/geographicAddressValidation/{id}", + produces = { "application/json" }) + ResponseEntity retrieveGeographicAddressValidation(@Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id); + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressValidationManagementApiController.java b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressValidationManagementApiController.java new file mode 100644 index 0000000000000000000000000000000000000000..514d57aaea83a71191b3517614c0323a54c0b216 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/api/GeographicAddressValidationManagementApiController.java @@ -0,0 +1,105 @@ +package org.etsi.osl.tmf.gam673.api; + + +import io.swagger.v3.oas.annotations.Parameter; +import jakarta.validation.Valid; +import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation; +import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressValidationManagementService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.util.List; + +@Controller +@RequestMapping("/geographicAddressValidationManagement/v4/") +public class GeographicAddressValidationManagementApiController implements GeographicAddressValidationManagementApi{ + private static final Logger log = LoggerFactory.getLogger(GeographicAddressValidationManagementApiController.class); + + private static final String COULD_NOT_SERIALIZE="Couldn't serialize response for content type application/json"; + private final GeographicAddressValidationManagementService geographicAddressValidationManagementService; + + @Autowired + public GeographicAddressValidationManagementApiController(GeographicAddressValidationManagementService geographicAddressValidationManagementService) { + this.geographicAddressValidationManagementService = geographicAddressValidationManagementService; + } + + // @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity> listGeographicAddressValidation() { + try { + return new ResponseEntity<>(geographicAddressValidationManagementService.findAllGeographicAddressValidations(), HttpStatus.OK); + + } catch (Exception e) { + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity createGeographicAddressValidation( + @Parameter(description = "The geographic address validation to be created", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidation + ) { + try{ + GeographicAddressValidation c = geographicAddressValidationManagementService.createGeographicAddressValidation(geographicAddressValidation); + return new ResponseEntity<>(c, HttpStatus.OK); + } catch (Exception e) { + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity deleteGeographicAddressValidation( + @Parameter(description = "Identifier of the geographic address validation", required = true) @PathVariable("id") String id) { + + try { + return new ResponseEntity<>(geographicAddressValidationManagementService.deleteGeographicAddressValidationById(id), HttpStatus.OK); + }catch (Exception e) { + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity retrieveGeographicAddressValidation( + @Parameter(description = "Identifier of the geographic address validation", required = true) @PathVariable("id") String id) { + try{ + GeographicAddressValidation c = geographicAddressValidationManagementService.findGeographicAddressValidationByUUID(id); + + return new ResponseEntity<>(c, HttpStatus.OK); + }catch (Exception e){ + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity patchGeographicAddressValidation( + @Parameter(description = "Identifier of the geographic address validation", required = true) @PathVariable("id") String id, + @Parameter(description = "The Geographic Address Validation to be updated", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddress) { + try{ + GeographicAddressValidation c = geographicAddressValidationManagementService.updateGeographicAddressValidation(id, geographicAddress); + + return new ResponseEntity<>(c, HttpStatus.OK); + }catch (Exception e){ + log.error(COULD_NOT_SERIALIZE, e); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/mapper/MapperUtils.java b/src/main/java/org/etsi/osl/tmf/gam673/mapper/MapperUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..178835dd19122482f0771560779bbaca9a14a798 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/mapper/MapperUtils.java @@ -0,0 +1,39 @@ +package org.etsi.osl.tmf.gam673.mapper; + +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicSubAddress; + +public class MapperUtils { + + public static GeographicAddress geographicAddressMap(GeographicAddress newGeographicAddress, GeographicAddress existingGeographicAddress){ + if(newGeographicAddress.getCity()!=null) existingGeographicAddress.setCity(newGeographicAddress.getCity()); + if(newGeographicAddress.getCountry()!=null) existingGeographicAddress.setCountry(newGeographicAddress.getCountry()); + if(newGeographicAddress.getLocality()!=null) existingGeographicAddress.setLocality(newGeographicAddress.getLocality()); + if(newGeographicAddress.getPostcode()!=null) existingGeographicAddress.setPostcode(newGeographicAddress.getPostcode()); + if(newGeographicAddress.getStateOrProvince()!=null) existingGeographicAddress.setStateOrProvince(newGeographicAddress.getStateOrProvince()); + if(newGeographicAddress.getStreetName()!=null) existingGeographicAddress.setStreetName(newGeographicAddress.getStreetName()); + if(newGeographicAddress.getStreetNr()!=null) existingGeographicAddress.setStreetNr(newGeographicAddress.getStreetNr()); + if(newGeographicAddress.getStreetNrLast()!=null) existingGeographicAddress.setStreetNrLast(newGeographicAddress.getStreetNrLast()); + if(newGeographicAddress.getStreetNrLastSuffix()!=null) existingGeographicAddress.setStreetNrLastSuffix(newGeographicAddress.getStreetNrLastSuffix()); + if(newGeographicAddress.getStreetSuffix()!=null) existingGeographicAddress.setStreetSuffix(newGeographicAddress.getStreetSuffix()); + if(newGeographicAddress.getStreetType()!=null) existingGeographicAddress.setStreetType(newGeographicAddress.getStreetType()); + if(newGeographicAddress.getHref()!=null) existingGeographicAddress.setHref(newGeographicAddress.getHref()); + if(newGeographicAddress.getName()!=null) existingGeographicAddress.setName(newGeographicAddress.getName()); + + if(newGeographicAddress.getGeographicLocation()!=null){ + if(newGeographicAddress.getGeographicLocation().getReferredType()!=null) existingGeographicAddress.getGeographicLocation().setReferredType(newGeographicAddress.getGeographicLocation().getReferredType()); + if(newGeographicAddress.getGeographicLocation().getRole()!=null) existingGeographicAddress.getGeographicLocation().setRole(newGeographicAddress.getGeographicLocation().getRole()); + } + + + if(newGeographicAddress.getGeographicSubAddresses()!=null){ + for(GeographicSubAddress n : newGeographicAddress.getGeographicSubAddresses()){ + if(n.getUuid()==null){ + existingGeographicAddress.addGeographicSubAddress(n); + } + } + } + return existingGeographicAddress; + } + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/repo/GeographicAddressManagementRepository.java b/src/main/java/org/etsi/osl/tmf/gam673/repo/GeographicAddressManagementRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..1a7f90e14ae6eeecd03bf796b1ca581df7bf11f7 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/repo/GeographicAddressManagementRepository.java @@ -0,0 +1,15 @@ +package org.etsi.osl.tmf.gam673.repo; + +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface GeographicAddressManagementRepository extends CrudRepository, PagingAndSortingRepository { + + Optional findByUuid(String id); + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/repo/GeographicAddressValidationManagementRepository.java b/src/main/java/org/etsi/osl/tmf/gam673/repo/GeographicAddressValidationManagementRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..51eb5b124c94532581e1e2b9021d48728f50d144 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/repo/GeographicAddressValidationManagementRepository.java @@ -0,0 +1,15 @@ +package org.etsi.osl.tmf.gam673.repo; + +import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface GeographicAddressValidationManagementRepository extends CrudRepository, PagingAndSortingRepository { + + Optional findByUuid(String id); + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/reposervices/GeographicAddressManagementService.java b/src/main/java/org/etsi/osl/tmf/gam673/reposervices/GeographicAddressManagementService.java new file mode 100644 index 0000000000000000000000000000000000000000..33cc3054ba9e894772f4d8d21517d57df481ca14 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/reposervices/GeographicAddressManagementService.java @@ -0,0 +1,81 @@ +package org.etsi.osl.tmf.gam673.reposervices; + +import org.etsi.osl.tmf.gam673.mapper.MapperUtils; +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicSubAddress; +import org.etsi.osl.tmf.gam673.repo.GeographicAddressManagementRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +@Service +@Transactional +public class GeographicAddressManagementService { + + private static final Logger log = LoggerFactory.getLogger(GeographicAddressManagementService.class); + + private final GeographicAddressManagementRepository geographicAddressManagementRepository; + + private MapperUtils mapperUtils; + + @Autowired + public GeographicAddressManagementService(GeographicAddressManagementRepository geographicAddressManagementRepository) { + this.geographicAddressManagementRepository = geographicAddressManagementRepository; + } + + public List findAllGeographicAddress(){ + return (List) geographicAddressManagementRepository.findAll(); + } + + public GeographicAddress findGeographicAddressByUUID(String uuid){ + return geographicAddressManagementRepository.findByUuid(uuid).get(); + } + + public Set findAllGeographicSubAddresses(String uuid){ + Optional geoAddress = geographicAddressManagementRepository.findByUuid(uuid); + return geoAddress.get().getGeographicSubAddresses(); + } + + public GeographicSubAddress findGeographicSubAddressByUUID(String geoAddressId , String geoSubAddressId){ + Optional geoAddress = geographicAddressManagementRepository.findByUuid(geoAddressId); + return geoAddress.get().getGeographicSubAddresses().stream() + .filter(geoSubAddress -> geoSubAddress.getUuid().equals(geoSubAddressId)) + .map(geoSubAddress -> geoSubAddress) + .collect(java.util.stream.Collectors.toList()).get(0); + } + + public GeographicAddress createGeographicAddress(GeographicAddress geographicAddress){ + log.info("Add another geographic address: {}",geographicAddress); + return geographicAddressManagementRepository.save(geographicAddress); + + } + + public GeographicAddress updateGeographicAddress(String id,GeographicAddress geographicAddress){ + log.info("Update geographic address with id: {}",id); + Optional gs = geographicAddressManagementRepository.findByUuid(id); + return gs.map(address -> updateFields(geographicAddress, address)).orElse(null); + } + + public Void deleteGeographicAddressById(String id){ + log.info("Delete geographic address with id: {}",id); + GeographicAddress gs=geographicAddressManagementRepository.findByUuid(id).orElseThrow(); + geographicAddressManagementRepository.delete(gs); + return null; + } + + private GeographicAddress updateFields(GeographicAddress newGeographicAddress, GeographicAddress existingGeographicAddress){ + + existingGeographicAddress = mapperUtils.geographicAddressMap(newGeographicAddress, existingGeographicAddress); + + geographicAddressManagementRepository.save(existingGeographicAddress); + return existingGeographicAddress; + } + + +} diff --git a/src/main/java/org/etsi/osl/tmf/gam673/reposervices/GeographicAddressValidationManagementService.java b/src/main/java/org/etsi/osl/tmf/gam673/reposervices/GeographicAddressValidationManagementService.java new file mode 100644 index 0000000000000000000000000000000000000000..40e5e7709ec1158f40ea95704f1443502e195637 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gam673/reposervices/GeographicAddressValidationManagementService.java @@ -0,0 +1,89 @@ +package org.etsi.osl.tmf.gam673.reposervices; + +import org.etsi.osl.tmf.gam673.mapper.MapperUtils; +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation; +import org.etsi.osl.tmf.gam673.repo.GeographicAddressValidationManagementRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@Transactional +public class GeographicAddressValidationManagementService { + + private static final Logger log = LoggerFactory.getLogger(GeographicAddressValidationManagementService.class); + + private final GeographicAddressValidationManagementRepository geographicAddressValidationManagementRepository; + + private MapperUtils mapperUtils; + + @Autowired + public GeographicAddressValidationManagementService(GeographicAddressValidationManagementRepository geographicAddressValidationManagementRepository) { + this.geographicAddressValidationManagementRepository = geographicAddressValidationManagementRepository; + } + + public List findAllGeographicAddressValidations(){ + return (List) geographicAddressValidationManagementRepository.findAll(); + } + + public GeographicAddressValidation findGeographicAddressValidationByUUID(String uuid){ + return geographicAddressValidationManagementRepository.findByUuid(uuid).get(); + } + + public GeographicAddressValidation createGeographicAddressValidation(GeographicAddressValidation geographicAddressValidation){ + log.info("Add another geographic address validation: {}",geographicAddressValidation); + return geographicAddressValidationManagementRepository.save(geographicAddressValidation); + + } + + public GeographicAddressValidation updateGeographicAddressValidation(String id,GeographicAddressValidation newGeographicAddressValidation){ + log.info("Update geographic address validation with id: {}",id); + Optional gs=geographicAddressValidationManagementRepository.findByUuid(id); + return gs.map(oldAddress -> updateFields(newGeographicAddressValidation, oldAddress)).orElse(null); + } + + public Void deleteGeographicAddressValidationById(String id){ + log.info("Delete geographic address validation with id: {}",id); + GeographicAddressValidation gs=geographicAddressValidationManagementRepository.findByUuid(id).orElseThrow(); + geographicAddressValidationManagementRepository.delete(gs); + return null; + } + + private GeographicAddressValidation updateFields(GeographicAddressValidation newGeographicAddressValidation, GeographicAddressValidation existingGeographicAddressValidation){ + + if(newGeographicAddressValidation.getValidationDate()!=null) existingGeographicAddressValidation.setValidationDate(newGeographicAddressValidation.getValidationDate()); + if(newGeographicAddressValidation.getStatus()!=null) existingGeographicAddressValidation.setStatus(newGeographicAddressValidation.getStatus()); + if(newGeographicAddressValidation.getValidationResult()!=null) existingGeographicAddressValidation.setValidationResult(newGeographicAddressValidation.getValidationResult()); + if(newGeographicAddressValidation.getProvideAlternative()!=null) existingGeographicAddressValidation.setProvideAlternative(newGeographicAddressValidation.getProvideAlternative()); + + if(newGeographicAddressValidation.getAlternateGeographicAddresses()!=null){ + for(GeographicAddress n : newGeographicAddressValidation.getAlternateGeographicAddresses()){ + if(n.getUuid()==null){ + existingGeographicAddressValidation.addGeographicAddress(n); + }else { + for (GeographicAddress oldGeographicAddress : existingGeographicAddressValidation.getAlternateGeographicAddresses()){ + if (n.getUuid().equals(oldGeographicAddress.getUuid())){ + GeographicAddress geographicAddress = mapperUtils.geographicAddressMap(n, oldGeographicAddress); + } + } + } + } + } + + if(newGeographicAddressValidation.getValidAddress()!=null){ + GeographicAddress geographicAddress = mapperUtils.geographicAddressMap(newGeographicAddressValidation.getValidAddress(), existingGeographicAddressValidation.getValidAddress()); + existingGeographicAddressValidation.setValidAddress(geographicAddress); + } + + geographicAddressValidationManagementRepository.save(existingGeographicAddressValidation); + return existingGeographicAddressValidation; + } + + +} diff --git a/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressManagementControllerTest.java b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressManagementControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a54f533b94a3206b549f1e3ce83a7658337b8ee3 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressManagementControllerTest.java @@ -0,0 +1,186 @@ +package org.etsi.osl.services.api.gam673; + +import org.etsi.osl.tmf.gam673.api.GeographicAddressManagementApiController; +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicSubAddress; +import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressManagementService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +class GeographicAddressManagementControllerTest { + + @InjectMocks + private GeographicAddressManagementApiController controller; + + + @Mock + private GeographicAddressManagementService service; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testRetrieveGeographicAddressByUUID() { + GeographicAddress address = new GeographicAddress(); + // Add test data to address list + when(service.findGeographicAddressByUUID("test")).thenReturn(address); + + ResponseEntity response = controller.retrieveGeographicAddress("test"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(address, response.getBody()); + } + + @Test + void throwExceptionTestWhenRetrieveGeographicAddressByUUID(){ + when(service.findGeographicAddressByUUID("test")).thenThrow(RuntimeException.class); + ResponseEntity response = controller.retrieveGeographicAddress("test"); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + + } + + @Test + void testRetrieveGeographicSubAddressByUUID() { + GeographicSubAddress address = new GeographicSubAddress(); + // Add test data to address list + when(service.findGeographicSubAddressByUUID("test","test")).thenReturn(address); + + ResponseEntity response = controller.retrieveGeographicSubAddress("test","test"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(address, response.getBody()); + } + + @Test + void throwExceptionTestWhenRetrieveGeographicSubAddressByUUID(){ + when(service.findGeographicSubAddressByUUID("test","test")).thenThrow(RuntimeException.class); + ResponseEntity response = controller.retrieveGeographicSubAddress("test","test"); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + + } + + @Test + void testRetrieveGeographicSubAddresses() { + Set subAddress = new HashSet<>(); + // Add test data to address list + when(service.findAllGeographicSubAddresses("test")).thenReturn(subAddress); + + ResponseEntity> response = controller.retrieveGeographicSubAddresses("test"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(subAddress, response.getBody()); + } + + @Test + void throwExceptionTestWhenRetrieveGeographicSubAddresses(){ + when(service.findAllGeographicSubAddresses("test")).thenThrow(RuntimeException.class); + ResponseEntity> response = controller.retrieveGeographicSubAddresses("test"); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + + } + + @Test + void testRetrieveGeographicAddresses() { + List addresses = new ArrayList<>(); + // Add test data to address list + when(service.findAllGeographicAddress()).thenReturn(addresses); + + ResponseEntity> response = controller.listGeographicAddress(); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(addresses, response.getBody()); + } + + @Test + void throwExceptionTestWhenRetrieveGeographicAddresses(){ + when(service.findAllGeographicAddress()).thenThrow(RuntimeException.class); + ResponseEntity> response = controller.listGeographicAddress(); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + + } + @Test + void testCreateGeographicAddress() { + GeographicAddress address = new GeographicAddress(); + + when(service.createGeographicAddress(any())).thenReturn(address); + + ResponseEntity response = controller.createGeographicAddress(address); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(address, response.getBody()); + } + + @Test + void testExceptionWhenCreateGeographicAddress(){ + GeographicAddress address = new GeographicAddress(); + + when(service.createGeographicAddress(any())).thenThrow(RuntimeException.class); + + ResponseEntity response = controller.createGeographicAddress(address); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testDeleteGeographicAddress() { + + doNothing().when(service).deleteGeographicAddressById(anyString()); + ResponseEntity response = controller.deleteGeographicAddress("addressId"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + void testDeleteGeographicAddressException() { + + doThrow(RuntimeException.class).when(service).deleteGeographicAddressById(anyString()); + ResponseEntity response = controller.deleteGeographicAddress("addressId"); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testPatchGeographicAddress() { + String addressId = "addressId"; + GeographicAddress updatedAddress = new GeographicAddress(); + // Set up mock service behavior + when(service.updateGeographicAddress(anyString(), any())).thenReturn(updatedAddress); + + ResponseEntity response = controller.patchGeographicAddress(addressId, updatedAddress); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(updatedAddress, response.getBody()); + } + + @Test + void testPatchGeographicAddressException() { + String addressId = "addressId"; + GeographicAddress updatedAddress = new GeographicAddress(); + // Set up mock service behavior + when(service.updateGeographicAddress(anyString(), any())).thenThrow(RuntimeException.class); + + ResponseEntity response = controller.patchGeographicAddress(addressId, updatedAddress); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + +} diff --git a/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressManagementServiceTest.java b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressManagementServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c9c21843a464b0da0025dedcd5358b9e5f2bc20f --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressManagementServiceTest.java @@ -0,0 +1,152 @@ +package org.etsi.osl.services.api.gam673; + +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicSubAddress; +import org.etsi.osl.tmf.gam673.repo.GeographicAddressManagementRepository; +import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressManagementService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.test.context.ActiveProfiles; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@AutoConfigureMockMvc +@ActiveProfiles("testing") +class GeographicAddressManagementServiceTest { + + @InjectMocks + private GeographicAddressManagementService service; + + @Mock + private GeographicAddressManagementRepository repository; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void testFindAllGeographicAddress() { + // Mock data + List addresses = List.of(new GeographicAddress(), new GeographicAddress()); + when(repository.findAll()).thenReturn(addresses); + + List result = service.findAllGeographicAddress(); + + assertEquals(addresses, result); + } + + @Test + void testFindGeographicAddressByUUID() { + // Mock data + String uuid = "123"; + GeographicAddress address = new GeographicAddress(); + address.setUuid(uuid); + when(repository.findByUuid(uuid)).thenReturn(Optional.of(address)); + + GeographicAddress result = service.findGeographicAddressByUUID(uuid); + + assertNotNull(result); + assertEquals(address, result); + } + + @Test + void testFindAllGeographicSubAddress() { + Optional address = Optional.of(new GeographicAddress()); + + GeographicSubAddress subAddress = new GeographicSubAddress(); + subAddress.setSubAddressType("test"); + subAddress.setBuildingName("test"); + subAddress.setSubUnitType("test"); + + address.get().setGeographicSubAddresses(Set.of(subAddress)); + + // Mock data + when(repository.findByUuid("test")).thenReturn(address); + + Set result = service.findAllGeographicSubAddresses("test"); + + assertEquals(address.get().getGeographicSubAddresses(), result); + } + + @Test + void testFindGeographicSubAddressByUUID() { + // Mock data + String addressId = "123"; + String subAddressId = "321"; + + GeographicSubAddress subAddress = new GeographicSubAddress(); + subAddress.setSubAddressType("test"); + subAddress.setBuildingName("test"); + subAddress.setSubUnitType("test"); + subAddress.setUuid(subAddressId); + + GeographicAddress address = new GeographicAddress(); + address.setUuid(addressId); + + address.setGeographicSubAddresses(Set.of(subAddress)); + + when(repository.findByUuid(addressId)).thenReturn(Optional.of(address)); + + GeographicSubAddress result = service.findGeographicSubAddressByUUID(addressId, subAddressId); + + assertNotNull(result); + assertEquals(subAddress, result); + } + + @Test + void testCreateGeographicAddress() { + // Mock data + GeographicAddress address = new GeographicAddress(); + when(repository.save(any())).thenReturn(address); + + GeographicAddress result = service.createGeographicAddress(address); + + assertEquals(address, result); + } + + @Test + void testUpdateGeographicAddress() { + // Mock data + String id = "123"; + GeographicAddress existingAddress = new GeographicAddress(); + GeographicAddress newAddress = new GeographicAddress(); + when(repository.findByUuid(id)).thenReturn(Optional.of(existingAddress)); + when(repository.save(any())).thenReturn(existingAddress); + + GeographicAddress result = service.updateGeographicAddress(id, newAddress); + + assertNotNull(result); + assertEquals(existingAddress, result); + // Add additional assertions for updated fields if needed + } + + @Test + void testDeleteGeographicAddressById() { + // Mock data + String id = "123"; + GeographicAddress existingAddress = new GeographicAddress(); + when(repository.findByUuid(id)).thenReturn(Optional.of(existingAddress)); + + Void result = service.deleteGeographicAddressById(id); + + assertNull(result); + verify(repository, times(1)).delete(existingAddress); + } + +} diff --git a/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressValidationManagementControllerTest.java b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressValidationManagementControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b8d2a947901083cc802dab44f7cefc7643f6f55d --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressValidationManagementControllerTest.java @@ -0,0 +1,145 @@ +package org.etsi.osl.services.api.gam673; + +import org.etsi.osl.tmf.gam673.api.GeographicAddressValidationManagementApiController; +import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation; +import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressValidationManagementService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +class GeographicAddressValidationManagementControllerTest { + + @InjectMocks + private GeographicAddressValidationManagementApiController controller; + + + @Mock + private GeographicAddressValidationManagementService service; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testRetrieveGeographicAddressValidation() { + List addresses = new ArrayList<>(); + // Add test data to address list + when(service.findAllGeographicAddressValidations()).thenReturn(addresses); + + ResponseEntity> response = controller.listGeographicAddressValidation(); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(addresses, response.getBody()); + } + + @Test + void throwExceptionTestWhenRetrieveGeographicAddressValidation(){ + when(service.findAllGeographicAddressValidations()).thenThrow(RuntimeException.class); + ResponseEntity> response = controller.listGeographicAddressValidation(); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + + } + + @Test + void testRetrieveGeographicAddressValidationByUUID() { + GeographicAddressValidation addressValidation = new GeographicAddressValidation(); + // Add test data to address list + when(service.findGeographicAddressValidationByUUID("Test")).thenReturn(addressValidation); + + ResponseEntity response = controller.retrieveGeographicAddressValidation("Test"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(addressValidation, response.getBody()); + } + + @Test + void throwExceptionTestWhenRetrieveGeographicAddressValidationByUUID(){ + when(service.findGeographicAddressValidationByUUID("test")).thenThrow(RuntimeException.class); + ResponseEntity response = controller.retrieveGeographicAddressValidation("test"); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + + } + + + @Test + void testCreateGeographicAddressValidation() { + GeographicAddressValidation address = new GeographicAddressValidation(); + + when(service.createGeographicAddressValidation(any())).thenReturn(address); + + ResponseEntity response = controller.createGeographicAddressValidation(address); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(address, response.getBody()); + } + + @Test + void testExceptionWhenCreateGeographicAddressValidation(){ + GeographicAddressValidation address = new GeographicAddressValidation(); + + when(service.createGeographicAddressValidation(any())).thenThrow(RuntimeException.class); + + ResponseEntity response = controller.createGeographicAddressValidation(address); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testDeleteGeographicAddressValidation() { + + doNothing().when(service).deleteGeographicAddressValidationById(anyString()); + ResponseEntity response = controller.deleteGeographicAddressValidation("addressId"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + void testDeleteGeographicAddressValidationException() { + + doThrow(RuntimeException.class).when(service).deleteGeographicAddressValidationById(anyString()); + ResponseEntity response = controller.deleteGeographicAddressValidation("addressId"); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testPatchGeographicAddressValidation() { + String addressId = "addressId"; + GeographicAddressValidation updatedAddress = new GeographicAddressValidation(); + // Set up mock service behavior + when(service.updateGeographicAddressValidation(anyString(), any())).thenReturn(updatedAddress); + + ResponseEntity response = controller.patchGeographicAddressValidation(addressId, updatedAddress); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(updatedAddress, response.getBody()); + } + + @Test + void testPatchGeographicAddressValidationException() { + String addressId = "addressId"; + GeographicAddressValidation updatedAddress = new GeographicAddressValidation(); + // Set up mock service behavior + when(service.updateGeographicAddressValidation(anyString(), any())).thenThrow(RuntimeException.class); + + ResponseEntity response = controller.patchGeographicAddressValidation(addressId, updatedAddress); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + +} diff --git a/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressValidationManagementServiceTest.java b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressValidationManagementServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b1c15685b34711608e60ce18186e0251b5955d4f --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/gam673/GeographicAddressValidationManagementServiceTest.java @@ -0,0 +1,120 @@ +package org.etsi.osl.services.api.gam673; + +import org.etsi.osl.tmf.gam673.model.GeographicAddress; +import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation; +import org.etsi.osl.tmf.gam673.repo.GeographicAddressValidationManagementRepository; +import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressValidationManagementService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.test.context.ActiveProfiles; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@AutoConfigureMockMvc +@ActiveProfiles("testing") +class GeographicAddressValidationManagementServiceTest { + + @InjectMocks + private GeographicAddressValidationManagementService service; + + @Mock + private GeographicAddressValidationManagementRepository repository; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void testFindAllGeographicAddressValidation() { + // Mock data + List addresses = List.of(new GeographicAddressValidation(), new GeographicAddressValidation()); + when(repository.findAll()).thenReturn(addresses); + + List result = service.findAllGeographicAddressValidations(); + + assertEquals(addresses, result); + } + + @Test + void testFindGeographicAddressValidationByUUID() { + // Mock data + String uuid = "123"; + GeographicAddressValidation address = new GeographicAddressValidation(); + address.setUuid(uuid); + when(repository.findByUuid(uuid)).thenReturn(Optional.of(address)); + + GeographicAddressValidation result = service.findGeographicAddressValidationByUUID(uuid); + + assertNotNull(result); + assertEquals(address, result); + } + + @Test + void testCreateGeographicAddressValidation() { + // Mock data + GeographicAddressValidation address = new GeographicAddressValidation(); + when(repository.save(any())).thenReturn(address); + + GeographicAddressValidation result = service.createGeographicAddressValidation(address); + + assertEquals(address, result); + } + + @Test + void testUpdateGeographicAddressValidation() { + // Mock data + String id = "123"; + + GeographicAddress newAlternativeAddress= new GeographicAddress(); + newAlternativeAddress.setUuid("1234"); + newAlternativeAddress.setLocality("localityNew"); + + GeographicAddress oldAlternativeAddress= new GeographicAddress(); + oldAlternativeAddress.setUuid("1234"); + oldAlternativeAddress.setLocality("localityOld"); + + GeographicAddressValidation existingAddress = new GeographicAddressValidation(); + existingAddress.setAlternateGeographicAddresses(Set.of(oldAlternativeAddress)); + + GeographicAddressValidation newAddress = new GeographicAddressValidation(); + newAddress.setAlternateGeographicAddresses(Set.of(newAlternativeAddress)); + + when(repository.findByUuid(id)).thenReturn(Optional.of(existingAddress)); + when(repository.save(any())).thenReturn(existingAddress); + + GeographicAddressValidation result = service.updateGeographicAddressValidation(id, newAddress); + + assertNotNull(result); + assertEquals(existingAddress, result); + // Add additional assertions for updated fields if needed + } + + @Test + void testDeleteGeographicAddressValidationById() { + // Mock data + String id = "123"; + GeographicAddressValidation existingAddress = new GeographicAddressValidation(); + when(repository.findByUuid(id)).thenReturn(Optional.of(existingAddress)); + + Void result = service.deleteGeographicAddressValidationById(id); + + assertNull(result); + verify(repository, times(1)).delete(existingAddress); + } + +}