From 58d289314d8afc0e6c962920b27f017af84b3992 Mon Sep 17 00:00:00 2001 From: lpapadopoulos <lpapadopoulos@ubitech.eu> Date: Tue, 11 Jun 2024 15:37:39 +0300 Subject: [PATCH] enhance code after MR comments --- ...GeographicSiteManagementApiController.java | 47 +++++-------------- ...raphicSiteManagementApiControllerTest.java | 14 ++++++ 2 files changed, 25 insertions(+), 36 deletions(-) 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 index 76faa05..208a655 100644 --- a/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApiController.java +++ b/src/main/java/org/etsi/osl/tmf/gsm674/api/GeographicSiteManagementApiController.java @@ -55,22 +55,14 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana } } - @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN')") + @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_TESTBED_PROVIDER')") @Override public ResponseEntity<GeographicSite> retrieveGeographicSite(Principal principal, @PathVariable("id") String id) { - try { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if ( id.equals( "myuser" ) ) { - - log.debug("principal= " + principal.toString()); - - Individual ind = individualRepoService.findByUsername(principal.getName()); - - GeographicSite gs= geographicSiteManagementService.findGeographicSiteByRelatedPartyId(ind.getId()); - if (gs==null) { - gs =new GeographicSite(); + GeographicSite gs= geographicSiteManagementService.findGeographicSiteByRelatedPartyId(id); + if (gs==null) { + gs =new GeographicSite(); GeographicSubAddressValue geographicSubAddressValue=new GeographicSubAddressValue(); GeographicAddressValue geographicAddressValue=new GeographicAddressValue(); geographicAddressValue.setGeographicSubAddress(geographicSubAddressValue); @@ -78,15 +70,8 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana List<PlaceRefOrValue> placeRefOrValues=new ArrayList<>(); placeRefOrValues.add(placeRefOrValue); gs.setPlace(placeRefOrValues); - } - return new ResponseEntity<GeographicSite>(gs,HttpStatus.OK); - - } else if ( authentication.getAuthorities().contains( new SimpleGrantedAuthority( UserRoleType.ROLE_ADMIN.getValue() ) ) ){ - return new ResponseEntity<GeographicSite>(geographicSiteManagementService.findGeographicSiteByUUID(id), HttpStatus.OK); - - }else { - return new ResponseEntity< GeographicSite >(HttpStatus.FORBIDDEN ); } + return new ResponseEntity<GeographicSite>(gs,HttpStatus.OK); } catch (Exception e) { log.error(COULD_NOT_SERIALIZE, e); @@ -94,7 +79,7 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana } } - @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_TESTBED_PROVIDER')" ) @Override public ResponseEntity<GeographicSite> createGeographicSite( @Parameter(description = "The geographic site to be created", required = true) @Valid @RequestBody GeographicSite geographicSite @@ -113,7 +98,7 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana } } - @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_TESTBED_PROVIDER')" ) @Override public ResponseEntity<Void> deleteGeographicSite( @Parameter(description = "Identifier of the geographic site", required = true) @PathVariable("id") String id) { @@ -128,26 +113,16 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana } - @PreAuthorize("hasAnyAuthority('ROLE_USER')" ) + @PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_TESTBED_PROVIDER')" ) @Override public ResponseEntity<GeographicSite> patchGeographicalSite(Principal principal, @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; - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if ( id.equals( "myuser" ) ) { - - log.debug("principal= " + principal.toString()); - - Individual ind = individualRepoService.findByUsername(principal.getName()); - GeographicSite gs = geographicSiteManagementService.findGeographicSiteByRelatedPartyId(ind.getId()); - c = geographicSiteManagementService.updateGeographicSite(gs.getUuid(), geographicSite); - }else{ - c = geographicSiteManagementService.updateGeographicSite(id, geographicSite); + GeographicSite c=geographicSiteManagementService.findGeographicSiteByRelatedPartyId(id); + c = geographicSiteManagementService.updateGeographicSite(c.getUuid(), geographicSite); + return new ResponseEntity<>(c, HttpStatus.OK); - } - 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/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java index 4aea003..4c070ed 100644 --- a/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java +++ b/src/test/java/org/etsi/osl/services/api/gsm674/GeographicSiteManagementApiControllerTest.java @@ -1,7 +1,10 @@ package org.etsi.osl.services.api.gsm674; import org.etsi.osl.tmf.gsm674.api.GeographicSiteManagementApiController; +import org.etsi.osl.tmf.gsm674.model.GeographicAddressValue; import org.etsi.osl.tmf.gsm674.model.GeographicSite; +import org.etsi.osl.tmf.gsm674.model.GeographicSubAddressValue; +import org.etsi.osl.tmf.gsm674.model.PlaceRefOrValue; import org.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService; import org.junit.jupiter.api.BeforeEach; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -66,6 +69,13 @@ class GeographicSiteManagementApiControllerTest { @Test void testFetchGeographicSite() { GeographicSite site = new GeographicSite(); + GeographicSubAddressValue geographicSubAddressValue=new GeographicSubAddressValue(); + GeographicAddressValue geographicAddressValue=new GeographicAddressValue(); + geographicAddressValue.setGeographicSubAddress(geographicSubAddressValue); + PlaceRefOrValue placeRefOrValue=new PlaceRefOrValue(geographicAddressValue); + List<PlaceRefOrValue> placeRefOrValues=new ArrayList<>(); + placeRefOrValues.add(placeRefOrValue); + site.setPlace(placeRefOrValues); // Add test data to sites list when(service.findGeographicSiteByUUID("123")).thenReturn(site); // Mock SecurityContext and Authentication @@ -127,9 +137,13 @@ class GeographicSiteManagementApiControllerTest { @Test void testPatchGeographicalSite() { + String relatedPartyUuid = "relatedPartyUuid"; String siteId = "siteId"; + GeographicSite oldSite = new GeographicSite(); + oldSite.setUuid(relatedPartyUuid); GeographicSite updatedSite = new GeographicSite(); // Set up mock service behavior + when(service.findGeographicSiteByRelatedPartyId(any())).thenReturn(oldSite); when(service.updateGeographicSite(anyString(), any())).thenReturn(updatedSite); ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(null,siteId, updatedSite); -- GitLab