Commit f157d349 authored by George Tziavas's avatar George Tziavas Committed by Christos Tranoris
Browse files

Resolve "Increase coverage in TMF634"

parent 966e508c
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -4,3 +4,4 @@
/.project
/.project
/.classpath
/.classpath
/.settings
/.settings
/org.etsi.osl.tmf.api.iml
+11 −0
Original line number Original line Diff line number Diff line
@@ -2,6 +2,7 @@ package org.etsi.osl.tmf;


import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
import java.util.List;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -41,4 +42,14 @@ public class JsonUtils {
		return mapper.readValue(content, valueType);
		return mapper.readValue(content, valueType);
	}
	}


	public static <T> List<T> toListOfJsonObj(String content, Class<T> valueType) throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		List<T> listOfJsonObj = mapper.readValue(
				content,
				mapper.getTypeFactory().constructCollectionType(
						List.class, valueType));
		return listOfJsonObj;
	}

}
}
+4 −0
Original line number Original line Diff line number Diff line
@@ -19,6 +19,9 @@
 */
 */
package org.etsi.osl.tmf.rcm634.api;
package org.etsi.osl.tmf.rcm634.api;


import lombok.Getter;

@Getter
@jakarta.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2021-05-29T22:34:44.143740800+03:00[Europe/Athens]")
@jakarta.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2021-05-29T22:34:44.143740800+03:00[Europe/Athens]")
public class ApiException extends Exception{
public class ApiException extends Exception{
    private int code;
    private int code;
@@ -26,4 +29,5 @@ public class ApiException extends Exception{
        super(msg);
        super(msg);
        this.code = code;
        this.code = code;
    }
    }

}
}
+187 −0
Original line number Original line Diff line number Diff line
/*-
 * ========================LICENSE_START=================================
 * org.etsi.osl.tmf.api
 * %%
 * Copyright (C) 2019 - 2024 openslice.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */

package org.etsi.osl.services.api.rcm634;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.etsi.osl.tmf.rcm634.api.ApiException;
import org.etsi.osl.tmf.rcm634.api.ApiOriginFilter;
import org.etsi.osl.tmf.rcm634.api.ApiResponseMessage;
import org.etsi.osl.tmf.rcm634.api.ImportJobApiController;
import org.etsi.osl.tmf.rcm634.api.ExportJobApiController;
import org.etsi.osl.tmf.rcm634.api.ListenerApiController;
import org.etsi.osl.tmf.rcm634.api.HubApiController;
import org.etsi.osl.tmf.rcm634.api.NotFoundException;

import org.junit.jupiter.api.Test;

import org.springframework.mock.web.MockHttpServletRequest;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

public class CommonTests {

    @Test
    public void testApiException() {
        int errorCode = 404;
        String errorMessage = "Not Found";

        ApiException exception = new ApiException(errorCode, errorMessage);

        assertEquals(errorMessage, exception.getMessage());
        assertEquals(errorCode, exception.getCode());
    }

    @Test
    public void testApiOriginFilter() throws IOException, ServletException {
        HttpServletRequest request = mock(HttpServletRequest.class);
        HttpServletResponse response = mock(HttpServletResponse.class);
        FilterChain chain = mock(FilterChain.class);

        ApiOriginFilter filter = new ApiOriginFilter();
        filter.doFilter(request, response, chain);

        verify(response).addHeader("Access-Control-Allow-Origin", "*");
        verify(response).addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        verify(response).addHeader("Access-Control-Allow-Headers", "Content-Type");
        verify(chain).doFilter(request, response);
    }

    @Test
    public void testApiResponseMessage() {
        int[] codes = {ApiResponseMessage.ERROR, ApiResponseMessage.WARNING, ApiResponseMessage.INFO, ApiResponseMessage.OK, ApiResponseMessage.TOO_BUSY, 6};

        String[] types = {"error", "warning", "info", "ok", "too busy", "unknown"};

        String[] messages = {"An error occured", "This is a warning", "Given info", "ok", "System is too busy", "unknown code"};

        for (int i = 0; i < codes.length; i++) {
            int code = codes[i];
            String type = types[i];
            String message = messages[i];

            ApiResponseMessage responseMessage = new ApiResponseMessage(code, message);

            assertEquals(message, responseMessage.getMessage());
            assertEquals(code, responseMessage.getCode());
            assertEquals(type, responseMessage.getType());

        }

        ApiResponseMessage responseMessage = new ApiResponseMessage();
        responseMessage.setMessage("Error");
        assertEquals("Error", responseMessage.getMessage());
        responseMessage.setType("ok");
        assertEquals("ok", responseMessage.getType());
        responseMessage.setCode(ApiResponseMessage.OK);
        assertEquals(ApiResponseMessage.OK, responseMessage.getCode());
    }

    @Test
    public void testExportJobApiController() {
        ObjectMapper objectMapper = new ObjectMapper();
        HttpServletRequest request = new MockHttpServletRequest();

        ExportJobApiController controller = new ExportJobApiController(objectMapper, request);

        Optional<ObjectMapper> returnedObjectMapper = controller.getObjectMapper();
        Optional<HttpServletRequest> returnedRequest = controller.getRequest();

        assertTrue(returnedObjectMapper.isPresent());
        assertTrue(returnedRequest.isPresent());

        assertEquals(objectMapper, returnedObjectMapper.get());
        assertEquals(request, returnedRequest.get());
    }

    @Test
    public void testHubApiController() {
        ObjectMapper objectMapper = new ObjectMapper();
        HttpServletRequest request = new MockHttpServletRequest();

        HubApiController controller = new HubApiController(objectMapper, request);

        Optional<ObjectMapper> returnedObjectMapper = controller.getObjectMapper();
        Optional<HttpServletRequest> returnedRequest = controller.getRequest();

        assertTrue(returnedObjectMapper.isPresent());
        assertTrue(returnedRequest.isPresent());

        assertEquals(objectMapper, returnedObjectMapper.get());
        assertEquals(request, returnedRequest.get());
    }

    @Test
    public void testImportJobApiController() {
        ObjectMapper objectMapper = new ObjectMapper();
        HttpServletRequest request = new MockHttpServletRequest();

        ImportJobApiController controller = new ImportJobApiController(objectMapper, request);

        Optional<ObjectMapper> returnedObjectMapper = controller.getObjectMapper();
        Optional<HttpServletRequest> returnedRequest = controller.getRequest();

        assertTrue(returnedObjectMapper.isPresent());
        assertTrue(returnedRequest.isPresent());

        assertEquals(objectMapper, returnedObjectMapper.get());
        assertEquals(request, returnedRequest.get());
    }

    @Test
    public void testListenerApiController() {
        ObjectMapper objectMapper = new ObjectMapper();
        HttpServletRequest request = new MockHttpServletRequest();

        ListenerApiController controller = new ListenerApiController(objectMapper, request);

        Optional<ObjectMapper> returnedObjectMapper = controller.getObjectMapper();
        Optional<HttpServletRequest> returnedRequest = controller.getRequest();

        assertTrue(returnedObjectMapper.isPresent());
        assertTrue(returnedRequest.isPresent());

        assertEquals(objectMapper, returnedObjectMapper.get());
        assertEquals(request, returnedRequest.get());
    }

    @Test
    public void testNotFoundException() {
        int errorCode = 404;
        String errorMessage = "Not Found";

        NotFoundException exception = new NotFoundException(errorCode, errorMessage);

        assertEquals(errorMessage, exception.getMessage());
        assertEquals(errorCode, exception.getCode());
    }

}
+129 −0
Original line number Original line Diff line number Diff line
package org.etsi.osl.services.api.rcm634;

import org.etsi.osl.tmf.rcm634.model.ConnectionPointSpecificationRef;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Method;

public class ConnectionPointSpecificationRefTest {

    @Test
    void testConnectionPointSpecificationRef() {
        ConnectionPointSpecificationRef ref = new ConnectionPointSpecificationRef();
        String id = "testId";
        String href = "testHref";
        String name = "testName";
        String version = "testVersion";
        String referredType = "testReferredType";

        ref.id(id);
        ref.setUuid(id);
        ref.href(href);
        ref.name(name);
        ref.version(version);
        ref._atReferredType(referredType);

        assertEquals(id, ref.getId());
        assertEquals(href, ref.getHref());
        assertEquals(name, ref.getName());
        assertEquals(version, ref.getVersion());
        assertEquals(referredType, ref.getAtReferredType());

        ref = new ConnectionPointSpecificationRef();
        ref.setUuid(id);
        ref.setHref(href);
        ref.setName(name);
        ref.setVersion(version);
        ref.setAtReferredType(referredType);

        assertEquals(id, ref.getId());
        assertEquals(href, ref.getHref());
        assertEquals(name, ref.getName());
        assertEquals(version, ref.getVersion());
        assertEquals(referredType, ref.getAtReferredType());
    }

    @Test
    void testEquals() {
        String id = "testId";
        String href = "testHref";
        String name = "testName";
        String version = "testVersion";
        String referredType = "testReferredType";

        ConnectionPointSpecificationRef ref1 = new ConnectionPointSpecificationRef();
        ref1.setUuid(id);
        ref1.setHref(href);
        ref1.setName(name);
        ref1.setVersion(version);
        ref1.setAtReferredType(referredType);

        ConnectionPointSpecificationRef ref2 = new ConnectionPointSpecificationRef();
        ref2.setUuid(id);
        ref2.setHref(href);
        ref2.setName(name);
        ref2.setVersion(version);
        ref2.setAtReferredType(referredType);


        assertTrue(ref1.equals(ref2));
        assertEquals(ref1.hashCode(), ref2.hashCode());

        ref1.id("differentId");

        assertFalse(ref1.equals(ref2));
        assertNotEquals(ref1.hashCode(), ref2.hashCode());
    }

    @Test
    void testToString() {
        ConnectionPointSpecificationRef ref = new ConnectionPointSpecificationRef();

        String id = "testId";
        String href = "testHref";
        String name = "testName";
        String version = "testVersion";
        String baseType = "testBaseType";
        String schemaLocation = "testSchemaLocation";
        String type = "testType";
        String referredType = "testReferredType";

        ref.id(id);
        ref.setUuid(id);
        ref.setHref(href);
        ref.setName(name);
        ref.setVersion(version);
        ref.setBaseType(baseType);
        ref.setSchemaLocation(schemaLocation);
        ref.setType(type);
        ref.setAtReferredType(referredType);

        String expectedString = "class ConnectionPointSpecificationRef {\n"
                + "    id: " + id + "\n"
                + "    href: " + href + "\n"
                + "    name: " + name + "\n"
                + "    version: " + version + "\n"
                + "    _atBaseType: " + baseType + "\n"
                + "    _atSchemaLocation: " + schemaLocation + "\n"
                + "    _atType: " + type + "\n"
                + "    _atReferredType: " + referredType + "\n"
                + "}";

        assertEquals(expectedString, ref.toString());
    }

    @Test
    void testToIndentedString() throws Exception {
        ConnectionPointSpecificationRef ref = new ConnectionPointSpecificationRef();

        Method method = ConnectionPointSpecificationRef.class.getDeclaredMethod("toIndentedString", Object.class);
        method.setAccessible(true);

        String input = "Hello\nWorld";
        String expectedOutput = "Hello\n    World";

        String output = (String) method.invoke(ref, input);

        assertEquals(expectedOutput, output);
    }
}
Loading