Commit b75e188e authored by Konstantinos Poulakakis's avatar Konstantinos Poulakakis
Browse files

Add TMF 673 unit tests. Remove unnecessary log configuration.

parent 20c6e168
Loading
Loading
Loading
Loading
+20 −35
Original line number Diff line number Diff line
package org.etsi.osl.tmf.gam673.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@@ -11,22 +9,20 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.etsi.osl.tmf.gam673.model.GeographicAddress;
import org.etsi.osl.tmf.gam673.model.GeographicSubAddress;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Generated
public interface GeographicAddressManagementApi {
    Logger log = LoggerFactory.getLogger(GeographicAddressManagementApi.class);

    default Optional<ObjectMapper> getObjectMapper() {
        return Optional.empty();
    }
@@ -49,10 +45,9 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress",
    @PostMapping(value = "/geographicAddress",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.POST)
            consumes = { "application/json" })
    ResponseEntity<GeographicAddress> createGeographicAddress(@Parameter(description = "The geographic address to be created", required = true) @Valid @RequestBody GeographicAddress geographicAddress);

    @Operation(summary = "Deletes a 'GeographicAddress' by Id", operationId = "deleteGeographicAddress", description = "", tags={ "GeographicAddress", })
@@ -65,10 +60,9 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress/{id}",
    @DeleteMapping(value = "/geographicAddress/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.DELETE)
            consumes = { "application/json" })
    ResponseEntity<Void> deleteGeographicAddress(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id);


@@ -82,10 +76,8 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    @GetMapping(value = "/geographicAddress",
            produces = { "application/json" })
    ResponseEntity<List<GeographicAddress>> listGeographicAddress();


@@ -99,10 +91,9 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress/{id}",
    @PatchMapping(value = "/geographicAddress/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.PATCH)
            consumes = { "application/json" })
    ResponseEntity<GeographicAddress> patchGeographicAddress(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id, @Parameter(description = "The Service Level Specification to be updated", required = true) @Valid @RequestBody GeographicAddress geographicAddress);


@@ -116,10 +107,8 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    @GetMapping(value = "/geographicAddress/{id}",
            produces = { "application/json" })
    ResponseEntity<GeographicAddress> retrieveGeographicAddress(@Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id);

    @Operation(summary = "Retrieves a 'GeographicSubAddress' by Id", operationId = "retrieveGeographicSubAddress", description = "" , tags={ "GeographicSubAddress", })
@@ -132,10 +121,8 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress/{id}/geographicSubAddress",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    @GetMapping(value = "/geographicAddress/{id}/geographicSubAddress",
            produces = { "application/json" })
    ResponseEntity<Set<GeographicSubAddress>> retrieveGeographicSubAddress(
            @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id);

@@ -149,12 +136,10 @@ public interface GeographicAddressManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddress/{id}/geographicSubAddress/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    @GetMapping(value = "/geographicAddress/{geoAddressId}/geographicSubAddress/{geoSubAddressId}",
            produces = { "application/json" })
    ResponseEntity<GeographicSubAddress> retrieveGeographicSubAddress(
            @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String geoAddressId,
            @Parameter(description = "Identifier of the geographic sub address", required = true) @PathVariable("id") String geoSubAddressId);
            @Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("geoAddressId") String geoAddressId,
            @Parameter(description = "Identifier of the geographic sub address", required = true) @PathVariable("geoSubAddressId") String geoSubAddressId);

}
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ public class GeographicAddressManagementApiController implements GeographicAddre
        this.geographicAddressManagementService = geographicAddressManagementService;
    }

    //    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @Override
    public ResponseEntity<List<GeographicAddress>> listGeographicAddress() {
        try {
+14 −23
Original line number Diff line number Diff line
package org.etsi.osl.tmf.gam673.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@@ -11,18 +9,18 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Optional;

@Generated
public interface GeographicAddressValidationManagementApi {
    Logger log = LoggerFactory.getLogger(GeographicAddressValidationManagementApi.class);

    default Optional<ObjectMapper> getObjectMapper() {
        return Optional.empty();
    }
@@ -45,10 +43,9 @@ public interface GeographicAddressValidationManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddressValidation",
    @PostMapping(value = "/geographicAddressValidation",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.POST)
            consumes = { "application/json" })
    ResponseEntity<GeographicAddressValidation> createGeographicAddressValidation(@Parameter(description = "The geographic address to be created", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidation);

    @Operation(summary = "Deletes a 'GeographicAddressValidation' by Id", operationId = "deleteGeographicAddressValidation", description = "", tags={ "GeographicAddressValidation", })
@@ -61,10 +58,9 @@ public interface GeographicAddressValidationManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddressValidation/{id}",
    @DeleteMapping(value = "/geographicAddressValidation/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.DELETE)
            consumes = { "application/json" })
    ResponseEntity<Void> deleteGeographicAddressValidation(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id);


@@ -78,10 +74,8 @@ public interface GeographicAddressValidationManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddressValidation",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    @GetMapping(value = "/geographicAddressValidation",
            produces = { "application/json" })
    ResponseEntity<List<GeographicAddressValidation>> listGeographicAddressValidation();


@@ -95,10 +89,9 @@ public interface GeographicAddressValidationManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddressValidation/{id}",
    @PatchMapping(value = "/geographicAddressValidation/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.PATCH)
            consumes = { "application/json" })
    ResponseEntity<GeographicAddressValidation> patchGeographicAddressValidation(@Parameter(description = "Identifier of the Geographic address", required = true) @PathVariable("id") String id, @Parameter(description = "The Service Level Specification to be updated", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidation);


@@ -112,10 +105,8 @@ public interface GeographicAddressValidationManagementApi {
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicAddressValidation/{id}",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    @GetMapping(value = "/geographicAddressValidation/{id}",
            produces = { "application/json" })
    ResponseEntity<GeographicAddressValidation> retrieveGeographicAddressValidation(@Parameter(description = "Identifier of the geographic address", required = true) @PathVariable("id") String id);

}
+2 −6
Original line number Diff line number Diff line
@@ -3,10 +3,7 @@ package org.etsi.osl.tmf.gam673.api;

import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import org.etsi.osl.tmf.gam673.model.GeographicAddress;
import org.etsi.osl.tmf.gam673.model.GeographicAddressValidation;
import org.etsi.osl.tmf.gam673.model.GeographicSubAddress;
import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressManagementService;
import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressValidationManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +17,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/geographicAddressValidationManagement/v4/")
@@ -50,10 +46,10 @@ public class GeographicAddressValidationManagementApiController implements Geogr
    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @Override
    public ResponseEntity<GeographicAddressValidation> createGeographicAddressValidation(
            @Parameter(description = "The geographic address validation to be created", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidatio
            @Parameter(description = "The geographic address validation to be created", required = true) @Valid @RequestBody GeographicAddressValidation geographicAddressValidation
    ) {
        try{
            GeographicAddressValidation c = geographicAddressValidationManagementService.createGeographicAddressValidation(geographicAddress);
            GeographicAddressValidation c = geographicAddressValidationManagementService.createGeographicAddressValidation(geographicAddressValidation);
            return new ResponseEntity<>(c, HttpStatus.OK);
        } catch (Exception e) {
            log.error(COULD_NOT_SERIALIZE, e);
+123 −0
Original line number Diff line number Diff line
package org.etsi.osl.services.api.gam673;

import org.etsi.osl.tmf.gam673.api.GeographicAddressManagementApiController;
import org.etsi.osl.tmf.gam673.model.GeographicAddress;
import org.etsi.osl.tmf.gam673.reposervices.GeographicAddressManagementService;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

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;

class GeographicAddressManagementControllerTest {

    @InjectMocks
    private GeographicAddressManagementApiController controller;


    @Mock
    private GeographicAddressManagementService service;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void testRetrieveGeographicAddresses() {
        List<GeographicAddress> addresses = new ArrayList<>();
        // Add test data to address list
        when(service.findAllGeographicAddresss()).thenReturn(addresses);

        ResponseEntity<List<GeographicAddress>> response = controller.listGeographicAddress();

        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertEquals(addresses, response.getBody());
    }

    @Test
    void throwExceptionTestWhenRetrieveGeographicAddresses(){
        when(service.findAllGeographicAddresss()).thenThrow(RuntimeException.class);
        ResponseEntity<List<GeographicAddress>> response = controller.listGeographicAddress();
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());

    }
    @Test
    void testCreateGeographicAddress() {
        GeographicAddress address = new GeographicAddress();

        when(service.createGeographicAddress(any())).thenReturn(address);

        ResponseEntity<GeographicAddress> response = controller.createGeographicAddress(address);

        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertEquals(address, response.getBody());
    }

    @Test
    void testExceptionWhenCreateGeographicAddress(){
        GeographicAddress address = new GeographicAddress();

        when(service.createGeographicAddress(any())).thenThrow(RuntimeException.class);

        ResponseEntity<GeographicAddress> response = controller.createGeographicAddress(address);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
    }

    @Test
    void testDeleteGeographicAddress() {

        doNothing().when(service).deleteGeographicAddressById(anyString());
        ResponseEntity<Void> response = controller.deleteGeographicAddress("addressId");

        assertEquals(HttpStatus.OK, response.getStatusCode());
    }

    @Test
    void testDeleteGeographicAddressException() {

        doThrow(RuntimeException.class).when(service).deleteGeographicAddressById(anyString());
        ResponseEntity<Void> response = controller.deleteGeographicAddress("addressId");

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
    }

    @Test
    void testPatchGeographicAddress() {
        String addressId = "addressId";
        GeographicAddress updatedAddress = new GeographicAddress();
        // Set up mock service behavior
        when(service.updateGeographicAddress(anyString(), any())).thenReturn(updatedAddress);

        ResponseEntity<GeographicAddress> response = controller.patchGeographicAddress(addressId, updatedAddress);

        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertEquals(updatedAddress, response.getBody());
    }

    @Test
    void testPatchGeographicAddressException() {
        String addressId = "addressId";
        GeographicAddress updatedAddress = new GeographicAddress();
        // Set up mock service behavior
        when(service.updateGeographicAddress(anyString(), any())).thenThrow(RuntimeException.class);

        ResponseEntity<GeographicAddress> response = controller.patchGeographicAddress(addressId, updatedAddress);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
    }

}
Loading