Commit 879bfc92 authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

Fix unit tests

parent 5adf8261
Loading
Loading
Loading
Loading
Loading
+54 −36
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@ import org.etsi.osl.tmf.gsm674.model.GeographicSite;
import org.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.test.context.ActiveProfiles;

import org.junit.jupiter.api.Test;
@@ -16,8 +19,11 @@ import org.springframework.http.ResponseEntity;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -25,8 +31,11 @@ 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.mock;
import static org.mockito.Mockito.when;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
@AutoConfigureMockMvc
@ActiveProfiles("testing")
class GeographicSiteManagementApiControllerTest {
@@ -54,17 +63,26 @@ class GeographicSiteManagementApiControllerTest {
        assertEquals(sites, response.getBody());
    }

//    @Test
//    void testFetchGeographicSite() {
//        GeographicSite site = new GeographicSite();
//        // Add test data to sites list
//        when(service.findGeographicSiteByUUID("123")).thenReturn(site);
//
//        ResponseEntity<GeographicSite> response = controller.retrieveGeographicSite("123");
//
//        assertEquals(HttpStatus.OK, response.getStatusCode());
//        assertEquals(site, response.getBody());
//    }
    @Test
    void testFetchGeographicSite() {
        GeographicSite site = new GeographicSite();
        // Add test data to sites list
        when(service.findGeographicSiteByUUID("123")).thenReturn(site);
// Mock SecurityContext and Authentication
        SecurityContext securityContext = mock(SecurityContext.class);
        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        Authentication authentication = new UsernamePasswordAuthenticationToken("user", "password", authorities);

        SecurityContextHolder.setContext(securityContext);

        when(securityContext.getAuthentication()).thenReturn(authentication);

        ResponseEntity<GeographicSite> response = controller.retrieveGeographicSite(null,"123");

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

    @Test
    void testCreateGeographicSite() {
@@ -107,29 +125,29 @@ class GeographicSiteManagementApiControllerTest {
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
    }

//    @Test
//    void testPatchGeographicalSite() {
//        String siteId = "siteId";
//        GeographicSite updatedSite = new GeographicSite();
//        // Set up mock service behavior
//        when(service.updateGeographicSite(anyString(), any())).thenReturn(updatedSite);
//
//        ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(siteId, updatedSite);
//
//        assertEquals(HttpStatus.OK, response.getStatusCode());
//        assertEquals(updatedSite, response.getBody());
//    }
//
//    @Test
//    void testPatchGeographicalSiteException() {
//        String siteId = "siteId";
//        GeographicSite updatedSite = new GeographicSite();
//        // Set up mock service behavior
//        when(service.updateGeographicSite(anyString(), any())).thenThrow(RuntimeException.class);
//
//        ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(siteId, updatedSite);
//
//        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
//    }
    @Test
    void testPatchGeographicalSite() {
        String siteId = "siteId";
        GeographicSite updatedSite = new GeographicSite();
        // Set up mock service behavior
        when(service.updateGeographicSite(anyString(), any())).thenReturn(updatedSite);

        ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(null,siteId, updatedSite);

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

    @Test
    void testPatchGeographicalSiteException() {
        String siteId = "siteId";
        GeographicSite updatedSite = new GeographicSite();
        // Set up mock service behavior
        when(service.updateGeographicSite(anyString(), any())).thenThrow(RuntimeException.class);

        ResponseEntity<GeographicSite> response = controller.patchGeographicalSite(null,siteId, updatedSite);

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

}