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 branches found
No related tags found
1 merge request!25Tmf 674 feature
Pipeline #6583 passed
......@@ -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);
......
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);
......
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