summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/ServiceBusinessLogicTest.java
blob: 671927cd6e5fbfc323694389d6528d12892dd382 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package org.onap.sdc.dcae.composition.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.sdc.dcae.client.ISdcClient;
import org.onap.sdc.dcae.composition.restmodels.AttachVFCMTServiceRequest;
import org.onap.sdc.dcae.composition.restmodels.DcaeMinimizedService;
import org.onap.sdc.dcae.composition.restmodels.MessageResponse;
import org.onap.sdc.dcae.composition.restmodels.ruleeditor.ActionDeserializer;
import org.onap.sdc.dcae.composition.restmodels.ruleeditor.BaseAction;
import org.onap.sdc.dcae.composition.restmodels.ruleeditor.BaseCondition;
import org.onap.sdc.dcae.composition.restmodels.ruleeditor.ConditionDeserializer;
import org.onap.sdc.dcae.composition.restmodels.sdc.*;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader;
import org.onap.sdc.dcae.errormng.ResponseFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;


import java.util.*;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.testng.Assert.assertNotEquals;

public class ServiceBusinessLogicTest {

    private String uuid = "b632b1da-e6ab-419d-8853-420e259097d9";
    private String userId = "gc786h";
    private String requestId = "1";
    private String monitoringComponentName = "monitoringComponentName";
    private String serviceUuid = "serviceUuid";
    private String vfiName = "vfiName";
    private String vfcmtUuid = "26e8d4b5-f087-4821-a75a-0b9514b5a7ab";
    private ResourceDetailed vfcmt = Mockito.mock(ResourceDetailed.class);
    private String categoryName = "Template";
    private String resourceType = "VFCMT";
    private ISdcClient sdcClientMock = Mockito.mock(ISdcClient.class);


    private static Gson gson = new GsonBuilder()
            .registerTypeAdapter(BaseAction.class, new ActionDeserializer())
            .registerTypeAdapter(BaseCondition.class, new ConditionDeserializer()).create();

    @InjectMocks
    ServiceBusinessLogic serviceBusinessLogic = new ServiceBusinessLogic();


    @Before
    public void setUp() {

        MockitoAnnotations.initMocks(this);
        serviceBusinessLogic.setSdcRestClient(sdcClientMock);

        new ErrorConfigurationLoader(System.getProperty("user.dir") + "/src/main/webapp/WEB-INF");

        setServicesMock();
        setVfcmtMock();
    }

    private void setVfcmtMock() {

        when(vfcmt.getResourceType()).thenReturn(resourceType);
        when(vfcmt.getCategory()).thenReturn(categoryName);
        when(vfcmt.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKIN");
        when(vfcmt.getUuid()).thenReturn(vfcmtUuid);
        List<Artifact> artifactList = new ArrayList<>();
        Artifact artifact = Mockito.mock(Artifact.class);//gson.fromJson(artifactJson, Artifact.class);
        artifactList.add(artifact);
        when(vfcmt.getArtifacts()).thenReturn(artifactList);

        when(artifact.getArtifactName()).thenReturn(DcaeBeConstants.Composition.fileNames.SVC_REF);

        when(serviceBusinessLogic.getSdcRestClient().getResource(anyString(), anyString())).thenReturn(vfcmt);
        when(serviceBusinessLogic.getSdcRestClient().changeResourceLifecycleState(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(vfcmt);

    }

    private void setServicesMock() {

        ServiceDetailed serviceDetailed = new ServiceDetailed();
        ResourceInstance resourceInstance = new ResourceInstance();
        Artifact artifact = new Artifact();
        artifact.setArtifactName("." + monitoringComponentName + "." + DcaeBeConstants.Composition.fileNames.EVENT_PROC_BP_YAML);
        resourceInstance.setArtifacts(Collections.singletonList(artifact));
        resourceInstance.setResourceInstanceName(vfiName);
        resourceInstance.setResoucreType("VF");
        serviceDetailed.setResources(Collections.singletonList(resourceInstance));
        when(serviceBusinessLogic.getSdcRestClient().getService(anyString(), anyString())).thenReturn(serviceDetailed);

        String serviceJson = "{\n\"lastUpdaterFullName\":\"GALCOHEN\",\n\"resources\":[{\n\"resourceInstanceName\":\"LiavVf0\",\n\"resourceName\":\"LiavVf\",\n\"resourceInvariantUUID\":\"47d5c3d6-83d8-4cbc-831c-1c7e52bd2964\",\n\"resourceVersion\":\"0.1\",\n\"resoucreType\":\"VF\",\n\"resourceUUID\":\"6e3a2db2-213b-41a4-b9eb-afab3c3b1463\",\n\"artifacts\":null\n}],\n\"artifacts\":null,\n\"distributionStatus\":\"DISTRIBUTION_NOT_APPROVED\",\n\"uuid\":\"b632b1da-e6ab-419d-8853-420e259097d9\",\n\"invariantUUID\":\"4cc1f274-107c-48e7-a7c7-9768c88043f7\",\n\"name\":\"Rony7777777\",\n\"version\":\"0.2\",\n\"toscaModelURL\":\"/sdc/v1/catalog/services/b632b1da-e6ab-419d-8853-420e259097d9/toscaModel\",\n\"lastUpdaterUserId\":\"gc786h\",\n\"category\":\"NetworkL1-3\",\n\"lifecycleState\":\"NOT_CERTIFIED_CHECKOUT\"\n}\n";
        Service service = gson.fromJson(serviceJson, Service.class);
        List<Service> serviceList = new ArrayList<>();
        serviceList.add(service);
        when(serviceBusinessLogic.getSdcRestClient().getServices(requestId)).thenReturn(serviceList);

    }

    @Test
    public void test_Service() {

        ResponseEntity<ServiceDetailed> result = serviceBusinessLogic.service(uuid, requestId);

        assertEquals(HttpStatus.OK, result.getStatusCode());
        assertEquals("vfiName", result.getBody().getResources().get(0).getResourceInstanceName());
        assertEquals("VF", result.getBody().getResources().get(0).getResoucreType());
    }

    @Test
    public void test_Services() {

        ResponseEntity<List<Service>> result = serviceBusinessLogic.services(userId, vfcmtUuid, requestId);
        assertEquals(HttpStatus.OK, result.getStatusCode());
        assertNotEquals(0, result.getBody().size());
        assertEquals(uuid, result.getBody().get(0).getUuid());

    }

    @Test
    public void test_AttachService() {

        AttachVFCMTServiceRequest request = new AttachVFCMTServiceRequest();
        request.setServiceUuid(serviceUuid);
        request.setInstanceName(vfiName);


        ResponseEntity<MessageResponse> result = serviceBusinessLogic.attachService(vfcmtUuid, userId, request, requestId);
        assertEquals(HttpStatus.OK, result.getStatusCode());
        assertThat(result.getBody().getSuccessResponse()).isEqualTo("Artifact updated");
    }

    @Test
    public void test_AttachService_verifyVfiExists_Exception() {

        AttachVFCMTServiceRequest request = new AttachVFCMTServiceRequest();
        request.setServiceUuid(serviceUuid);
        request.setInstanceName("WrongName");
        when(serviceBusinessLogic.checkinVfcmt(userId, uuid, requestId)).thenReturn(null);

        ResponseEntity<ResponseFormat> result = serviceBusinessLogic.attachService(vfcmtUuid, userId, request, requestId);
        assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
        assertEquals("SVC6039", result.getBody().getRequestError().getServiceException().getMessageId());
    }

    @Test
    public void test_GetAttachedService() {

        when(serviceBusinessLogic.getSdcRestClient().getResourceArtifact(any(), any(), any())).thenReturn("artifact data");

        ResponseEntity<MessageResponse> result = serviceBusinessLogic.getAttachedService(vfcmtUuid, requestId);
        assertEquals(HttpStatus.OK, result.getStatusCode());
        assertThat(result.getBody().getSuccessResponse()).isEqualTo("artifact data");
    }


    @Test
    public void test_parseAndFliterServicesByUser_nullServices_TBD() {
//		fail("TODO Auto-generated method stub");
    }


    @Test
    public void test_parseAndFliterServicesByUser_emptyList_emptyList() {
        // arrange
        String user_id = "test";
        String lastUpdaterUserId = "test";
        List<LinkedHashMap<String, String>> services = new ArrayList<LinkedHashMap<String, String>>();
        // act
        List<DcaeMinimizedService> result = serviceBusinessLogic.parseAndFilterServicesByUser(lastUpdaterUserId, services, user_id);
        // assert
        assertThat(result).isEqualTo(new ArrayList<DcaeMinimizedService>());
    }


    @Test
    public void test_parseAndFliterServicesByUser_singleServicesAsMap_singleServiceParsed() {
        // arrange
        String user_id = "test";
        String lastUpdaterUserId = user_id;
        String uuid = "a";
        String invariantUUID = "1";
        String lifecycleState = DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
        String version = "0.1";
        String serviceName = "TestService";

        LinkedHashMap<String, String> service = createServiceAsMap(lastUpdaterUserId, uuid, invariantUUID,
                lifecycleState, version, serviceName);
        List<LinkedHashMap<String, String>> services = new ArrayList<LinkedHashMap<String, String>>(
                Arrays.asList(service));

        DcaeMinimizedService expected = new DcaeMinimizedService(uuid, serviceName, lastUpdaterUserId, lifecycleState,
                version, invariantUUID);
        // act
        List<DcaeMinimizedService> result = serviceBusinessLogic.parseAndFilterServicesByUser(lastUpdaterUserId, services, user_id);
        // assert
        assertThat(result).usingRecursiveFieldByFieldElementComparator().contains(expected);
    }


    @Test
    public void test_parseAndFliterServicesByUser_unsortedServices_sortedServices() {
        // arrange
        String user_id = "test";
        String lastUpdaterUserId = user_id;
        String uuid = "a";
        String lifecycleState = DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
        String version = "0.1";

        List<LinkedHashMap<String, String>> unsortedServices = Arrays.asList("d", "a", "c", "b").stream()
                .map(x -> createServiceAsMap(lastUpdaterUserId, uuid, UUID.randomUUID().toString(), lifecycleState, version, x))
                .collect(Collectors.toList());


        // act
        List<DcaeMinimizedService> result = serviceBusinessLogic.parseAndFilterServicesByUser(lastUpdaterUserId, unsortedServices,
                user_id);
        // assert
        assertThat(result).extracting("name").containsExactly("a", "b", "c", "d");
    }


    @Test
    public void test_parseAndFliterServicesByUser_allOptionsForLastUpdaterAndIsCheckout_allOptionsButIsCheckoutAndNotLastUpdater() {
        // ------------user == last_updater
        // -----------------True----False--
        // isCheckout----------------------
        // --------True------V--------X----
        // --------False-----V--------V----
        // --------------------------------
//		fail("TODO Auto-generated method stub");
    }


    @Test
    public void test_parseAndFliterServicesByUser_singleServiceWithMultiVersions_singleServiceWithLatestVersion() {
        // arrange
        String user_id = "test";
        String lastUpdaterUserId = user_id;
        String uuid = "a";
        String invariantUUID = "1";
        String lifecycleState = DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
        String serviceName = "TestService";

        List<LinkedHashMap<String, String>> singleServiceWithMultiVersions = Arrays.asList("1.0", "0.3", "11.0", "2.0", "1.8").stream()
                .map(x -> createServiceAsMap(lastUpdaterUserId, uuid, invariantUUID, lifecycleState, x, serviceName))
                .collect(Collectors.toList());

        // act
        List<DcaeMinimizedService> result = serviceBusinessLogic.parseAndFilterServicesByUser(lastUpdaterUserId, singleServiceWithMultiVersions, user_id);

        // assert
        assertThat(result).extracting("version").containsExactly("11.0");
    }


    private static LinkedHashMap<String, String> createServiceAsMap(String lastUpdaterUserId, String uuid,
                                                                    String invariantUUID, String lifecycleState, String version, String serviceName) {

        LinkedHashMap<String, String> service = new LinkedHashMap<String, String>() {
            {
                put("invariantUUID", invariantUUID);
                put("uuid", uuid);
                put("name", serviceName);
                put("lastUpdaterUserId", lastUpdaterUserId);
                put("lifecycleState", lifecycleState);
                put("version", version);
            }
        };

        return service;
    }
}