Commit 58d28931 authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

enhance code after MR comments

parent ed6b13fc
Loading
Loading
Loading
Loading
Loading
+11 −36
Original line number Diff line number Diff line
@@ -55,20 +55,12 @@ 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());
            GeographicSite gs= geographicSiteManagementService.findGeographicSiteByRelatedPartyId(id);
            if (gs==null) {
                gs =new GeographicSite();
                GeographicSubAddressValue geographicSubAddressValue=new GeographicSubAddressValue();
@@ -81,20 +73,13 @@ public class GeographicSiteManagementApiController implements GeographicSiteMana
            }
            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 );
            }

        } catch (Exception e) {
            log.error(COULD_NOT_SERIALIZE, e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @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);

        }catch (Exception e){
            log.error(COULD_NOT_SERIALIZE, e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+14 −0
Original line number Diff line number Diff line
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);