summaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/mso/MsoBusinessLogicImplTest.java
blob: e0e3c363456362317661a8be15bf5ee65270edc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.onap.vid.mso;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.changeManagement.RequestDetailsWrapper;
import org.onap.vid.controllers.MsoController;
import org.onap.vid.mso.rest.RequestDetails;
import org.onap.vid.properties.Features;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.onap.vid.controllers.MsoController.SVC_INSTANCE_ID;
import static org.onap.vid.mso.MsoBusinessLogicImpl.validateEndpointPath;

@ContextConfiguration(classes = {SystemProperties.class})
@WebAppConfiguration
public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests {

    private static final String SERVICE_INSTANCE_ID = "1";
    private static final String VNF_INSTANCE_ID = "1";
    private static final String EXPECTED_SCALE_OUT_PATH = "/serviceInstantiation/v7/serviceInstances/1/vnfs/1/vfModules/scaleOut";
    private static final Path PATH_TO_NOT_PROCESSED_SCALE_OUT_REQUEST = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayload.json");
    private static final Path PATH_TO_FINAL_SCALE_OUT_REQUEST = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayloadToMso.json");
    private static final ObjectMapper OBJECT_MAPPER=new ObjectMapper();

    @InjectMocks
    private MsoBusinessLogicImpl msoBusinessLogic;

    @Mock
    private FeatureManager featureManagerMock;

    @Mock
    private MsoInterface msoInterfaceMock;


    @BeforeTest
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void validateEndpointPath_endPointIsNotEmptyAndVaild_returnProperty() {
        System.setProperty("TestEnv", "123");
        String foundEndPoint = validateEndpointPath("TestEnv");
        Assert.assertEquals("123", foundEndPoint);
    }

    @Test(expectedExceptions = RuntimeException.class)
    public void validateEndpointPath_endPointIsNull_throwRuntimeException() {
        validateEndpointPath("NotExists");
    }

    @Test(expectedExceptions = RuntimeException.class)
    public void validateEndpointPath_endPointIsNotEmptyButDoesntExists_throwRuntimeException() {
        System.setProperty("EmptyEndPoint", "");
        validateEndpointPath("EmptyEndPoint");
    }


    //@Test(dataProvider = "unAssignOrDeleteParams")
    public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOffOrUnAssignFlagIsFalse(boolean isAssignFlag, String status) {
        Mockito.reset(msoInterfaceMock);
        String endpoint = validateEndpointPath(isAssignFlag ? MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE : MsoProperties.MSO_REST_API_SVC_INSTANCE);
        RequestDetails requestDetails = new RequestDetails();

        when(featureManagerMock.isActive(Features.FLAG_UNASSIGN_SERVICE)).thenReturn(isAssignFlag);

        msoBusinessLogic.deleteSvcInstance(requestDetails, "tempId", status);

        verify(msoInterfaceMock).deleteSvcInstance(requestDetails, endpoint + "/tempId");
    }

    @DataProvider
    public Object[][] unAssignOrDeleteParams() {
        return new Object[][]{
                {Boolean.FALSE, "active"},
                {Boolean.FALSE, "created"},
                {Boolean.TRUE, "Active"},
                {Boolean.TRUE, "unexpected-status"},
        };
    }

    //@Test(dataProvider = "unAssignStatus")
    public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOnAndUnAssignFlagIsTrue(String status) {
        Mockito.reset(msoInterfaceMock);
        // in the test Features.FLAG_UNASSIGN_SERVICE is active so the endpoint should be MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE
        String endpoint = validateEndpointPath(MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE);
        RequestDetails requestDetails = new RequestDetails();

        when(featureManagerMock.isActive(Features.FLAG_UNASSIGN_SERVICE)).thenReturn(true);

        msoBusinessLogic.deleteSvcInstance(requestDetails, "tempId", status);

        verify(msoInterfaceMock).unassignSvcInstance(requestDetails, endpoint + "/tempId/unassign");
    }

    @DataProvider
    public Object[][] unAssignStatus() {
        return new Object[][]{
                {"Created"},
                {"Pendingdelete"},
                {"pending-Delete"},
                {"Assigned"}
        };
    }

    @Test
    public void deleteVnf_verifyEndPointPathConstructing() {
        String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE);
        RequestDetails requestDetails = new RequestDetails();

        String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, "serviceInstanceTempId");

        msoBusinessLogic.deleteVnf(requestDetails, "serviceInstanceTempId", "vnfInstanceTempId");
        verify(msoInterfaceMock).deleteVnf(requestDetails, vnf_endpoint + "/vnfInstanceTempId");
    }

    @Test
    public void deleteVfModule_verifyEndPointPathConstructing() {
        String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
        RequestDetails requestDetails = new RequestDetails();

        String vf__modules_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, "serviceInstanceTempId").replaceFirst(MsoController.VNF_INSTANCE_ID, "vnfInstanceTempId");

        msoBusinessLogic.deleteVfModule(requestDetails, "serviceInstanceTempId", "vnfInstanceTempId", "vfModuleTempId");
        verify(msoInterfaceMock).deleteVfModule(requestDetails, vf__modules_endpoint + "/vfModuleTempId");
    }


    @Test
    public void shouldSendProperScaleOutRequest() throws IOException {
        ArgumentCaptor<RequestDetailsWrapper> requestDetailsWrapperArgumentCaptor = ArgumentCaptor.forClass(RequestDetailsWrapper.class);
        org.onap.vid.changeManagement.RequestDetails requestDetails = getScaleOutRequest();
        RequestDetailsWrapper expectedRequestWrapper = getExpectedRequestWrapper();

        msoBusinessLogic.scaleOutVfModuleInstance(requestDetails, SERVICE_INSTANCE_ID, VNF_INSTANCE_ID);

        verify(msoInterfaceMock).scaleOutVFModuleInstance(requestDetailsWrapperArgumentCaptor.capture(), eq(EXPECTED_SCALE_OUT_PATH));
        RequestDetailsWrapper actual = requestDetailsWrapperArgumentCaptor.getAllValues().get(0);

        assertThat(expectedRequestWrapper.requestDetails).isEqualTo(actual.requestDetails);
    }

    private org.onap.vid.changeManagement.RequestDetails getScaleOutRequest() throws IOException {
        return OBJECT_MAPPER.readValue(PATH_TO_NOT_PROCESSED_SCALE_OUT_REQUEST.toFile(), org.onap.vid.changeManagement.RequestDetails.class);
    }

    private RequestDetailsWrapper getExpectedRequestWrapper() throws IOException {
        return OBJECT_MAPPER.readValue(PATH_TO_FINAL_SCALE_OUT_REQUEST.toFile(), new TypeReference<RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails>>() {
        });
    }
}