Skip to content
Snippets Groups Projects
Commit 58d28931 authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

enhance code after MR comments

parent ed6b13fc
No related tags found
1 merge request!25Tmf 674 feature
Pipeline #6583 passed
...@@ -55,22 +55,14 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana ...@@ -55,22 +55,14 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana
} }
} }
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN')") @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_TESTBED_PROVIDER')")
@Override @Override
public ResponseEntity<GeographicSite> retrieveGeographicSite(Principal principal, @PathVariable("id") String id) { public ResponseEntity<GeographicSite> retrieveGeographicSite(Principal principal, @PathVariable("id") String id) {
try { try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); GeographicSite gs= geographicSiteManagementService.findGeographicSiteByRelatedPartyId(id);
if ( id.equals( "myuser" ) ) { if (gs==null) {
gs =new GeographicSite();
log.debug("principal= " + principal.toString());
Individual ind = individualRepoService.findByUsername(principal.getName());
GeographicSite gs= geographicSiteManagementService.findGeographicSiteByRelatedPartyId(ind.getId());
if (gs==null) {
gs =new GeographicSite();
GeographicSubAddressValue geographicSubAddressValue=new GeographicSubAddressValue(); GeographicSubAddressValue geographicSubAddressValue=new GeographicSubAddressValue();
GeographicAddressValue geographicAddressValue=new GeographicAddressValue(); GeographicAddressValue geographicAddressValue=new GeographicAddressValue();
geographicAddressValue.setGeographicSubAddress(geographicSubAddressValue); geographicAddressValue.setGeographicSubAddress(geographicSubAddressValue);
...@@ -78,15 +70,8 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana ...@@ -78,15 +70,8 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana
List<PlaceRefOrValue> placeRefOrValues=new ArrayList<>(); List<PlaceRefOrValue> placeRefOrValues=new ArrayList<>();
placeRefOrValues.add(placeRefOrValue); placeRefOrValues.add(placeRefOrValue);
gs.setPlace(placeRefOrValues); 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) { } catch (Exception e) {
log.error(COULD_NOT_SERIALIZE, e); log.error(COULD_NOT_SERIALIZE, e);
...@@ -94,7 +79,7 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana ...@@ -94,7 +79,7 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana
} }
} }
@PreAuthorize("hasAnyAuthority('ROLE_USER')" ) @PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_TESTBED_PROVIDER')" )
@Override @Override
public ResponseEntity<GeographicSite> createGeographicSite( public ResponseEntity<GeographicSite> createGeographicSite(
@Parameter(description = "The geographic site to be created", required = true) @Valid @RequestBody GeographicSite geographicSite @Parameter(description = "The geographic site to be created", required = true) @Valid @RequestBody GeographicSite geographicSite
...@@ -113,7 +98,7 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana ...@@ -113,7 +98,7 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana
} }
} }
@PreAuthorize("hasAnyAuthority('ROLE_USER')" ) @PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_TESTBED_PROVIDER')" )
@Override @Override
public ResponseEntity<Void> deleteGeographicSite( public ResponseEntity<Void> deleteGeographicSite(
@Parameter(description = "Identifier of the geographic site", required = true) @PathVariable("id") String id) { @Parameter(description = "Identifier of the geographic site", required = true) @PathVariable("id") String id) {
...@@ -128,26 +113,16 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana ...@@ -128,26 +113,16 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana
} }
@PreAuthorize("hasAnyAuthority('ROLE_USER')" ) @PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_TESTBED_PROVIDER')" )
@Override @Override
public ResponseEntity<GeographicSite> patchGeographicalSite(Principal principal, public ResponseEntity<GeographicSite> patchGeographicalSite(Principal principal,
@Parameter(description = "Identifier of the ServiceOrder", required = true) @PathVariable("id") String id, @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) { @Parameter(description = "The ServiceOrder to be updated", required = true) @Valid @RequestBody GeographicSite geographicSite) {
try{ try{
GeographicSite c; GeographicSite c=geographicSiteManagementService.findGeographicSiteByRelatedPartyId(id);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); c = geographicSiteManagementService.updateGeographicSite(c.getUuid(), geographicSite);
if ( id.equals( "myuser" ) ) { return new ResponseEntity<>(c, HttpStatus.OK);
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);
}
return new ResponseEntity<>(c, HttpStatus.OK);
}catch (Exception e){ }catch (Exception e){
log.error(COULD_NOT_SERIALIZE, e); log.error(COULD_NOT_SERIALIZE, e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
......
package org.etsi.osl.services.api.gsm674; package org.etsi.osl.services.api.gsm674;
import org.etsi.osl.tmf.gsm674.api.GeographicSiteManagementApiController; 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.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.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
...@@ -66,6 +69,13 @@ class GeographicSiteManagementApiControllerTest { ...@@ -66,6 +69,13 @@ class GeographicSiteManagementApiControllerTest {
@Test @Test
void testFetchGeographicSite() { void testFetchGeographicSite() {
GeographicSite site = new GeographicSite(); 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 // Add test data to sites list
when(service.findGeographicSiteByUUID("123")).thenReturn(site); when(service.findGeographicSiteByUUID("123")).thenReturn(site);
// Mock SecurityContext and Authentication // Mock SecurityContext and Authentication
...@@ -127,9 +137,13 @@ class GeographicSiteManagementApiControllerTest { ...@@ -127,9 +137,13 @@ class GeographicSiteManagementApiControllerTest {
@Test @Test
void testPatchGeographicalSite() { void testPatchGeographicalSite() {
String relatedPartyUuid = "relatedPartyUuid";
String siteId = "siteId"; String siteId = "siteId";
GeographicSite oldSite = new GeographicSite();
oldSite.setUuid(relatedPartyUuid);
GeographicSite updatedSite = new GeographicSite(); GeographicSite updatedSite = new GeographicSite();
// Set up mock service behavior // Set up mock service behavior
when(service.findGeographicSiteByRelatedPartyId(any())).thenReturn(oldSite);
when(service.updateGeographicSite(anyString(), any())).thenReturn(updatedSite); when(service.updateGeographicSite(anyString(), any())).thenReturn(updatedSite);
ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(null,siteId, updatedSite); ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(null,siteId, updatedSite);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment