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..239be290194fb3da0c4bd4c908d742c492c08ef9 100644 --- a/src/main/java/org/etsi/osl/tmf/configuration/SwaggerDocumentationConfig.java +++ b/src/main/java/org/etsi/osl/tmf/configuration/SwaggerDocumentationConfig.java @@ -728,7 +728,41 @@ public class SwaggerDocumentationConfig { .build(); } - + /** + * TMF 674 Geographic Site Management + * @return + */ + + + @Bean + public OpenApiCustomizer gsm674OpenAPI() { + return openApi -> openApi + .specVersion( SpecVersion.V30 ).addSecurityItem(new SecurityRequirement().addList("security_auth")) + .info(new Info() + .title("TMF 674 Geographic Site Management") + .description("## TMF API Reference: TMF674 - Geographic Site Management ") + .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 gsm674(){ + + 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-674-Geographic Site Management-v4.0.0") + .addOpenApiCustomizer( this.gsm674OpenAPI() ) + .packagesToScan("org.etsi.osl.tmf.gsm674.api") + .build(); + + } /** diff --git a/src/main/java/org/etsi/osl/tmf/gsm674/api/Generated.java b/src/main/java/org/etsi/osl/tmf/gsm674/api/Generated.java new file mode 100644 index 0000000000000000000000000000000000000000..92fbaec5167449362f97923c72969d6c4f3f655f --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gsm674/api/Generated.java @@ -0,0 +1,16 @@ +package org.etsi.osl.tmf.gsm674.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/gsm674/api/GeographicSiteManagementApi.java b/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApi.java new file mode 100644 index 0000000000000000000000000000000000000000..380afa76449809e13532ca81a60a794e04fb1925 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApi.java @@ -0,0 +1,134 @@ +package org.etsi.osl.tmf.gsm674.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.gsm674.model.GeographicSite; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +import java.io.IOException; +import java.security.Principal; +import java.util.List; +import java.util.Optional; + +@Generated +public interface GeographicSiteManagementApi { + Logger log = LoggerFactory.getLogger(GeographicSiteManagementApi.class); + + 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 'GeographicSite'", operationId = "createGeographicSite", description = "", tags={ "geographicSite", }) + @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" ) }) + @RequestMapping(value = "/geographicSite", + produces = { "application/json" }, + consumes = { "application/json" }, + method = RequestMethod.POST) + default ResponseEntity createGeographicSite(@Parameter(description = "The geographic site to be created" ,required=true ) @Valid @RequestBody GeographicSite geographicSite) { + return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + } + + @Operation(summary = "Deletes a 'GeographicSite' by Id", operationId = "deleteGeographicSite", description = "", tags={ "GeographicSite", }) + @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" ) }) + @RequestMapping(value = "/geographicSite/{id}", + produces = { "application/json" }, + method = RequestMethod.DELETE) + default ResponseEntity deleteGeographicSite(@Parameter(description = "Identifier of the Geographic site",required=true) @PathVariable("id") String id) { + return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + } + + + @Operation(summary = "List or find 'GeographicSite' objects", operationId = "listGeographicSite", description = "" , tags={ "GeographicSite", }) + @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" ) }) + @RequestMapping(value = "/geographicSite", + produces = { "application/json" }, + method = RequestMethod.GET) + default ResponseEntity> listGeographicSite() { + return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + } + + + @Operation(summary = "Updates partially a 'GeographicSite' by Id", operationId = "patchGeographicSite", description = "", tags={ "GeographicSite", }) + @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" ) }) + @RequestMapping(value = "/geographicSite/{id}", + produces = { "application/json" }, + consumes = { "application/json" }, + method = RequestMethod.PATCH) + default ResponseEntity patchGeographicalSite(@Parameter(description = "Identifier of the Geographic site",required=true) @PathVariable("id") String id,@Parameter(description = "The Service Level Specification to be updated" ,required=true ) @Valid @RequestBody GeographicSite geographicSite) { + return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + } + + + @Operation(summary = "Retrieves a 'GeographicSite' by Id", operationId = "retrieveGeographicSite", description = "" , tags={ "GeographicSite", }) + @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" ) }) + @RequestMapping(value = "/geographicSite/{id}", + produces = { "application/json" }, + method = RequestMethod.GET) + default ResponseEntity retrieveGeographicSite(@Parameter(description = "Identifier of the Geographic site",required=true) @PathVariable("id") String id) { + return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + } + +} diff --git a/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApiController.java b/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApiController.java new file mode 100644 index 0000000000000000000000000000000000000000..23f1be21eb18cf5f1e9e892df19a50fd53b100d0 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApiController.java @@ -0,0 +1,106 @@ +package org.etsi.osl.tmf.gsm674.api; + +import io.swagger.v3.oas.annotations.Parameter; +import jakarta.validation.Valid; +import org.etsi.osl.tmf.gsm674.model.GeographicSite; +import org.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService; +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("/geographicSiteManagement/v4/") +public class GeographicSiteManagementApiController implements GeographicSiteManagementApi{ + + private static final String COULD_NOT_SERIALIZE="Couldn't serialize response for content type application/json"; + private final GeographicSiteManagementService geographicSiteManagementService; + + @Autowired + public GeographicSiteManagementApiController(GeographicSiteManagementService geographicSiteManagementService) { + this.geographicSiteManagementService = geographicSiteManagementService; + } + + @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @Override + public ResponseEntity> listGeographicSite() { + + + try { + return new ResponseEntity<>(geographicSiteManagementService.findAllGeographicSites(), 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 retrieveGeographicSite(@PathVariable("id") String id) { + + + try { + return new ResponseEntity<>(geographicSiteManagementService.findGeographicSiteByUUID(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 createGeographicSite( + @Parameter(description = "The geographic site to be created", required = true) @Valid @RequestBody GeographicSite geographicSite + ) { + + try { + + GeographicSite c = geographicSiteManagementService.createGeographicSite(geographicSite); + + 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 deleteGeographicSite( + @Parameter(description = "Identifier of the geographic site", required = true) @PathVariable("id") String id) { + + try { + return new ResponseEntity<>(geographicSiteManagementService.deleteGeographicSiteById(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 patchGeographicalSite( + @Parameter(description = "Identifier of the ServiceOrder", required = true) @PathVariable("id") String id, + @Parameter(description = "The ServiceOrder to be updated", required = true) @Valid @RequestBody GeographicSite geographicSite) { + try{ + GeographicSite c = geographicSiteManagementService.updateGeographicSite(id, geographicSite); + + 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/gsm674/repo/GeographicSiteManagementRepository.java b/src/main/java/org/etsi/osl/tmf/gsm674/repo/GeographicSiteManagementRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..c65f209878f0d4524667bea677006f0b61eba100 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gsm674/repo/GeographicSiteManagementRepository.java @@ -0,0 +1,14 @@ +package org.etsi.osl.tmf.gsm674.repo; + +import org.etsi.osl.tmf.gsm674.model.GeographicSite; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface GeographicSiteManagementRepository extends CrudRepository, PagingAndSortingRepository { + Optional findByUuid(String id); + +} diff --git a/src/main/java/org/etsi/osl/tmf/gsm674/reposervices/GeographicSiteManagementService.java b/src/main/java/org/etsi/osl/tmf/gsm674/reposervices/GeographicSiteManagementService.java new file mode 100644 index 0000000000000000000000000000000000000000..0887b38e67728cd1d486bf979ded708d74e02f85 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/gsm674/reposervices/GeographicSiteManagementService.java @@ -0,0 +1,126 @@ +package org.etsi.osl.tmf.gsm674.reposervices; + +import org.etsi.osl.tmf.gsm674.model.CalendarPeriod; +import org.etsi.osl.tmf.gsm674.model.GeographicSite; +import org.etsi.osl.tmf.gsm674.model.GeographicSiteRelationship; +import org.etsi.osl.tmf.gsm674.model.PlaceRefOrValue; +import org.etsi.osl.tmf.gsm674.repo.GeographicSiteManagementRepository; +import org.etsi.osl.tmf.prm669.model.RelatedParty; +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 GeographicSiteManagementService { + private static final Logger log = LoggerFactory.getLogger(GeographicSiteManagementService.class); + + private final GeographicSiteManagementRepository geographicSiteManagementRepository; + + @Autowired + public GeographicSiteManagementService(GeographicSiteManagementRepository geographicSiteManagementRepository) { + this.geographicSiteManagementRepository = geographicSiteManagementRepository; + } + + public List findAllGeographicSites(){ + return (List) geographicSiteManagementRepository.findAll(); + } + + public GeographicSite findGeographicSiteByUUID(String uuid){ + Optional gs=geographicSiteManagementRepository.findByUuid(uuid); + return gs.orElse(null); + + } + + public GeographicSite createGeographicSite(GeographicSite geographicSite){ + log.info("Add another geographic site: {}",geographicSite); + return geographicSiteManagementRepository.save(geographicSite); + + } + + public GeographicSite updateGeographicSite(String id,GeographicSite geographicSite){ + log.info("Update geographic site with id: {}",id); + Optional gs=geographicSiteManagementRepository.findByUuid(id); + return gs.map(site -> updateFields(geographicSite, site)).orElse(null); + } + + public Void deleteGeographicSiteById(String id){ + log.info("Delete geographic site with id: {}",id); + GeographicSite gs=geographicSiteManagementRepository.findByUuid(id).orElseThrow(); + geographicSiteManagementRepository.delete(gs); + return null; + } + + private GeographicSite updateFields(GeographicSite newSite, GeographicSite existingSite){ + + if(newSite.getName()!=null) existingSite.setName(newSite.getName()); + if(newSite.getDescription()!=null) existingSite.setDescription(newSite.getDescription()); + if(newSite.getCode()!=null) existingSite.setCode(newSite.getCode()); + if (newSite.getStatus()!=null) existingSite.setStatus(newSite.getStatus()); + + if(newSite.getGeographicSiteRelationship()!=null){ + for(GeographicSiteRelationship n : newSite.getGeographicSiteRelationship()){ + if(n.getUuid()==null){ + existingSite.addGeographicSiteRelationship(n); + }else { + for (GeographicSiteRelationship oldGeographicRelationship : existingSite.getGeographicSiteRelationship()){ + if (n.getUuid().equals(oldGeographicRelationship.getUuid())){ + if (n.getRole() !=null) oldGeographicRelationship.setRole(n.getRole()); + if (n.getRelationshipType() !=null) oldGeographicRelationship.setRelationshipType(n.getRelationshipType()); + if (n.getValidFor() !=null) oldGeographicRelationship.setValidFor(n.getValidFor()); + } + } + } + } + } + + if(newSite.getCalendar()!=null){ + for(CalendarPeriod c: newSite.getCalendar()){ + if(c.getUuid()==null){ + existingSite.addCalendarPeriod(c); + } else { + for (CalendarPeriod oldCalendarPeriod: existingSite.getCalendar()){ + if (c.getUuid().equals(oldCalendarPeriod.getUuid())){ + if( c.getDay() !=null) oldCalendarPeriod.setDay(c.getDay()); + if( c.getStatus() !=null) oldCalendarPeriod.setStatus(c.getStatus()); + if( c.getTimeZone() !=null) oldCalendarPeriod.setTimeZone(c.getTimeZone()); + if( c.getHourPeriod() !=null) oldCalendarPeriod.setHourPeriod(c.getHourPeriod()); + } + } + } + } + } + + if(newSite.getPlaceRefOrValue()!=null){ + for(PlaceRefOrValue p: newSite.getPlaceRefOrValue()){ + if (p.getUuid()==null){ + existingSite.addPlaceRefOrValue(p); + } + } + } + + if(newSite.getRelatedParties()!=null){ + for(RelatedParty party: newSite.getRelatedParties()){ + if(party.getUuid()==null){ + existingSite.addRelatedParty(party); + } else { + for (RelatedParty rp: existingSite.getRelatedParties()){ + if(party.getUuid().equals(rp.getUuid())){ + if (party.getRole() !=null) rp.setRole(party.getRole()); + if (party.getName() !=null) rp.setName(party.getName()); + } + } + } + } + } + + geographicSiteManagementRepository.save(existingSite); + return existingSite; + } + +} diff --git a/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..04878fd10c9dfdf92a02d8e22c5b999a523214bd --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java @@ -0,0 +1,134 @@ +package org.etsi.osl.services.api.gsm674; + +import org.etsi.osl.tmf.gsm674.api.GeographicSiteManagementApiController; +import org.etsi.osl.tmf.gsm674.model.GeographicSite; +import org.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService; +import org.junit.jupiter.api.BeforeEach; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.test.context.ActiveProfiles; + +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 java.util.Optional; +import java.util.UUID; + +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; + +@AutoConfigureMockMvc +@ActiveProfiles("testing") +class GeographicSiteManagementApiControllerTest { + + @InjectMocks + private GeographicSiteManagementApiController controller; + + @Mock + private GeographicSiteManagementService service; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testRetrieveGeographicSites() { + List sites = new ArrayList<>(); + // Add test data to sites list + when(service.findAllGeographicSites()).thenReturn(sites); + + ResponseEntity> response = controller.listGeographicSite(); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(sites, response.getBody()); + } + + @Test + void testFetchGeographicSite() { + GeographicSite site = new GeographicSite(); + // Add test data to sites list + when(service.findGeographicSiteByUUID("123")).thenReturn(site); + + ResponseEntity response = controller.retrieveGeographicSite("123"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(site, response.getBody()); + } + + @Test + void testCreateGeographicSite() { + GeographicSite site = new GeographicSite(); + + when(service.createGeographicSite(any())).thenReturn(site); + + ResponseEntity response = controller.createGeographicSite(site); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(site, response.getBody()); + } + + @Test + void testExceptionWhenCreateGeographicSite(){ + GeographicSite site = new GeographicSite(); + + when(service.createGeographicSite(any())).thenThrow(RuntimeException.class); + + ResponseEntity response = controller.createGeographicSite(site); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testDeleteGeographicSite() { + + doNothing().when(service).deleteGeographicSiteById(anyString()); + ResponseEntity response = controller.deleteGeographicSite("siteId"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + void testDeleteGeographicSiteException() { + + doThrow(RuntimeException.class).when(service).deleteGeographicSiteById(anyString()); + ResponseEntity response = controller.deleteGeographicSite("siteId"); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testPatchGeographicalSite() { + String siteId = "siteId"; + GeographicSite updatedSite = new GeographicSite(); + // Set up mock service behavior + when(service.updateGeographicSite(anyString(), any())).thenReturn(updatedSite); + + ResponseEntity response = controller.patchGeographicalSite(siteId, updatedSite); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(updatedSite, response.getBody()); + } + + @Test + void testPatchGeographicalSiteException() { + String siteId = "siteId"; + GeographicSite updatedSite = new GeographicSite(); + // Set up mock service behavior + when(service.updateGeographicSite(anyString(), any())).thenThrow(RuntimeException.class); + + ResponseEntity response = controller.patchGeographicalSite(siteId, updatedSite); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + +} diff --git a/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementServiceTest.java b/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0ffbb2cb930558a1222221d67c7c92955e0bdb79 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementServiceTest.java @@ -0,0 +1,96 @@ +package org.etsi.osl.services.api.gsm674; +import org.etsi.osl.tmf.gsm674.model.GeographicSite; +import org.etsi.osl.tmf.gsm674.repo.GeographicSiteManagementRepository; +import org.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService; +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 static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +@AutoConfigureMockMvc +@ActiveProfiles("testing") +class GeographicSiteManagementServiceTest { + @InjectMocks + private GeographicSiteManagementService service; + + @Mock + private GeographicSiteManagementRepository repository; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void testFindAllGeographicSites() { + // Mock data + List sites = List.of(new GeographicSite(), new GeographicSite()); + when(repository.findAll()).thenReturn(sites); + + List result = service.findAllGeographicSites(); + + assertEquals(sites, result); + } + + @Test + void testFindGeographicSiteByUUID() { + // Mock data + String uuid = "123"; + GeographicSite site = new GeographicSite(); + when(repository.findByUuid(uuid)).thenReturn(Optional.of(site)); + + GeographicSite result = service.findGeographicSiteByUUID(uuid); + + assertEquals(site, result); + } + + @Test + void testCreateGeographicSite() { + // Mock data + GeographicSite site = new GeographicSite(); + when(repository.save(any())).thenReturn(site); + + GeographicSite result = service.createGeographicSite(site); + + assertEquals(site, result); + } + + @Test + void testUpdateGeographicSite() { + // Mock data + String id = "123"; + GeographicSite existingSite = new GeographicSite(); + GeographicSite newSite = new GeographicSite(); + when(repository.findByUuid(id)).thenReturn(Optional.of(existingSite)); + when(repository.save(any())).thenReturn(existingSite); + + GeographicSite result = service.updateGeographicSite(id, newSite); + + assertNotNull(result); + assertEquals(existingSite, result); + // Add additional assertions for updated fields if needed + } + + @Test + void testDeleteGeographicSiteById() { + // Mock data + String id = "123"; + GeographicSite existingSite = new GeographicSite(); + when(repository.findByUuid(id)).thenReturn(Optional.of(existingSite)); + + Void result = service.deleteGeographicSiteById(id); + + assertNull(result); + verify(repository, times(1)).delete(existingSite); + } +}