Commit 0c2608e7 authored by Eduardo Santos's avatar Eduardo Santos
Browse files

Started testing ServiceRepoService

parent 114a8307
Loading
Loading
Loading
Loading
+167 −0
Original line number Diff line number Diff line
/**
 * @Author: Eduardo Santos
 * @Date:   2024-05-01 19:42:14
 * @Last Modified by:   Eduardo Santos
 * @Last Modified time: 2024-05-08 14:01:04
 */
package org.etsi.osl.services.services;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Optional;

import org.etsi.osl.tmf.OpenAPISpringBoot;
import org.etsi.osl.tmf.sim638.model.Service;
import org.etsi.osl.tmf.sim638.model.ServiceUpdate;
import org.etsi.osl.tmf.sim638.repo.ServiceActionQueueRepository;
import org.etsi.osl.tmf.sim638.repo.ServiceRepository;
import org.etsi.osl.tmf.sim638.service.ServiceRepoService;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.persistence.EntityManagerFactory;

@RunWith(SpringRunner.class)
@ActiveProfiles("testing")
@SpringBootTest(classes = OpenAPISpringBoot.class)
//@DataJpaTest
public class ServiceRepoServiceTest {

    @Mock
    private ServiceRepository serviceRepository;

    @SpyBean
    private ServiceRepoService serviceRepoService;

    private static Service initialService;

    private static Service finalService;

    private static ServiceUpdate servUpd;

    private static ObjectMapper mapper;

    @BeforeClass
    public static void setupBeforeClass() {
        try {
            mapper = new ObjectMapper();

            initialService = mapper.readValue(
                    new File(
                            "src/test/resources/ServiceRepoServiceTests/18:53:38.012079607/initial_service.json"),
                            Service.class
                            );

            finalService = mapper.readValue(
                    new File(
                            "src/test/resources/ServiceRepoServiceTests/18:53:38.012079607/final_service.json"),
                            Service.class
                            );

            servUpd = mapper.readValue(
                    new File(
                            "src/test/resources/ServiceRepoServiceTests/18:53:38.012079607/supd.json"),
                            ServiceUpdate.class
                            );

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        assertNotNull(initialService);
    }

    @Before
    public void setupBefore() {
        when(serviceRepoService.getServiceEager(anyString())).thenReturn(initialService);
    }

    @Test
    public void testUpdateServiceWhenServiceNotFound() {
        // Setup the expectation
        when(serviceRepoService.getServiceEager(anyString())).thenReturn(null);

        // Execute the method to be tested
        Service result = serviceRepoService.updateService("1f54476f-f8d2-43fc-85e8-45eab9273e75", servUpd, false, null, null);

        // Assert the expected outcome
        assertNull(result);
    }

    @Test
    public void testUpdateServiceWhenServiceFound() {
        // Execute the method to be tested
        Service result = serviceRepoService.updateService("1f54476f-f8d2-43fc-85e8-45eab9273e75", servUpd, false, null, null);

        // Assert the expected outcome
        assertNotNull(result);
    }

    @Test
    public void testVerifyGetServiceEagerIsCalled() {
        // Execute the method to be tested
        serviceRepoService.updateService("1f54476f-f8d2-43fc-85e8-45eab9273e75", servUpd, false, null, null);
        serviceRepoService.getServiceEager("1f54476f-f8d2-43fc-85e8-45eab9273e75");

        // Verify the expected outcome
        verify(serviceRepoService, times(2)).getServiceEager(anyString());
    }

    


    //@Test
    //public void testWhenInitialServiceThenReturnFinalService() {
    //     // Setup the expectation
    //     when(serviceRepoService.updateService(
    //                                "123", 
    //                                servUpd, 
    //                                false,
    //                                null, 
    //                                null)
    //                            ).thenReturn(finalService);
    //    
    //    // When method getServiceEager is called, it will retrieve 
    //    // an initialService, but the updateService should retrieve
    //    // a finalService
    //    Service result = serviceRepoService.updateService("123", servUpd, false, null, null);
//
    //    // Assert that the result is equal to the finalService content
    //    assertEquals(result, finalService);
    //}

    //@Test
    //public void testAddPrimitiveCharacteristicValuesToArrayList (){
    //    Service result = serviceRepoService.updateService("123", servUpd, false, null, null);
//
    //    System.out.println("The result is: " + result);
//
    //    assertNotNull(result);
    //}
    

}