DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / test / java / org / onap / sdc / dcae / composition / impl / VfcmtBusinessLogicTest.java
1 package org.onap.sdc.dcae.composition.impl;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.mockito.Mockito;
7 import org.mockito.MockitoAnnotations;
8 import org.onap.sdc.dcae.catalog.asdc.ASDCException;
9 import org.onap.sdc.dcae.client.ISdcClient;
10 import org.onap.sdc.dcae.client.SdcRestClient;
11 import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;
12 import org.onap.sdc.dcae.composition.restmodels.ImportVFCMTRequest;
13 import org.onap.sdc.dcae.composition.restmodels.MonitoringComponent;
14 import org.onap.sdc.dcae.composition.restmodels.VfcmtData;
15 import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
16 import org.onap.sdc.dcae.composition.restmodels.sdc.ExternalReferencesMap;
17 import org.onap.sdc.dcae.composition.restmodels.sdc.Resource;
18 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
19 import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
20 import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader;
21 import org.onap.sdc.dcae.errormng.PolicyException;
22 import org.onap.sdc.dcae.errormng.RequestError;
23 import org.onap.sdc.dcae.errormng.ResponseFormat;
24 import org.springframework.http.HttpStatus;
25 import org.springframework.http.ResponseEntity;
26 import org.springframework.web.client.HttpClientErrorException;
27
28 import java.util.*;
29
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Matchers.eq;
33 import static org.mockito.Mockito.*;
34 import static org.onap.sdc.dcae.composition.util.DcaeBeConstants.LifecycleStateEnum.CERTIFIED;
35 import static org.onap.sdc.dcae.composition.util.DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT;
36
37 public class VfcmtBusinessLogicTest {
38
39         private ISdcClient sdcClientMock = Mockito.mock(SdcRestClient.class);
40         private ResourceDetailed templateMC = Mockito.mock(ResourceDetailed.class);
41
42         private VfcmtBusinessLogic vfcmtBusinessLogic = new VfcmtBusinessLogic();
43         private ImportVFCMTRequest request = new ImportVFCMTRequest();
44
45         private String userId = "me";
46         private String requestId = "1";
47
48         @Before
49         public void setup(){
50                 MockitoAnnotations.initMocks(this);
51                 new ErrorConfigurationLoader(System.getProperty("user.dir")+"/src/main/webapp/WEB-INF");
52                 vfcmtBusinessLogic.setSdcRestClient(sdcClientMock);
53                 request.setTemplateUuid("577");
54                 request.setVfiName("vfi_XX");
55                 request.setDescription("description");
56                 request.setFlowType("SNMP");
57                 request.setName("newVfcmt");
58                 request.setServiceUuid("service99999");
59                 request.setContextType("services");
60         }
61
62         @Test
63         public void sdcIsDown_creatingVfcmt_gotResponseWithError500() throws Exception{
64                 RequestError requestError = new RequestError();
65                 requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null));
66                 when(sdcClientMock.createResource(userId,request,requestId)).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError));
67
68                 ResponseEntity res = vfcmtBusinessLogic.createMcFromTemplate(userId,request,requestId);
69                 verify(sdcClientMock).getResource("577",requestId);
70                 verify(sdcClientMock,times(0)).getResourceArtifact(anyString(),anyString(),anyString());
71                 Assert.assertEquals(500, res.getStatusCodeValue());
72         }
73
74         @Test
75         public void uploadCloneCdumpFailed_creatingVfcmt_createVfcmtRolledBack() throws Exception {
76                 RequestError requestError = new RequestError();
77                 requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null));
78                 when(sdcClientMock.createResourceArtifact(anyString(),anyString(),any(),anyString())).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError));
79                 when(sdcClientMock.createResource(userId,request,requestId)).thenReturn(templateMC);
80                 when(sdcClientMock.getResourceArtifact(anyString(), anyString(), anyString())).thenReturn("{\"flowType\":\"don't override\"");
81                 when(templateMC.getUuid()).thenReturn("3");
82         when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
83         emulateListOfArtifactsWithCompositionYml();
84
85         vfcmtBusinessLogic.createMcFromTemplate(userId, request, requestId);
86
87                 // making sure rollback is performed if exception is thrown
88                 verify(sdcClientMock).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString());
89         }
90
91         // happy happy joy joy
92         @Test
93         public void successfulCreationAndAttachmentOfVfcmt() throws Exception {
94                 when(templateMC.getUuid()).thenReturn("3");
95                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
96                 ResourceDetailed mockedVfcmt = Mockito.mock(ResourceDetailed.class);
97                 when(mockedVfcmt.getUuid()).thenReturn("5");
98                 when(sdcClientMock.createResource(anyString(),any(),anyString())).thenReturn(mockedVfcmt);
99                 when(sdcClientMock.getResourceArtifact(anyString(),anyString(),anyString())).thenReturn("3243324");
100
101                 emulateListOfArtifactsWithCompositionYml();
102
103                 vfcmtBusinessLogic.createMcFromTemplate(userId, request,requestId);
104
105                 verify(sdcClientMock).createResource(userId, request,requestId);
106                 verify(sdcClientMock).getResource(anyString(),anyString());
107                 verify(sdcClientMock).getResourceArtifact(anyString(),anyString(),anyString());
108                 verify(sdcClientMock, times(2)).createResourceArtifact(anyString(),anyString(),any(),anyString());
109                 verify(sdcClientMock).addExternalMonitoringReference(anyString(),any(),any(),anyString());
110                 verify(sdcClientMock).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString());
111         }
112
113         @Test
114         public void successfulImportAndAttachmentOfVfcmtAlreadyConnectedWithoutEditDoCheckin() throws Exception {
115                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
116                 when(sdcClientMock.getResourceArtifact(anyString(),anyString(),anyString())).thenReturn("{\"flowType\":\"don't override\"}");
117                 when(templateMC.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKOUT");
118                 emulateListOfArtifactsWithCompositionYmlAndSvcRef();
119                 request.setCloneVFCMT(false);
120                 request.setUpdateFlowType(false);
121                 vfcmtBusinessLogic.importMC(userId, request, requestId);
122
123                 verify(sdcClientMock, times(0)).createResource(userId, request, requestId);
124                 verify(sdcClientMock).getResource(anyString(),anyString());
125                 verify(sdcClientMock).getResourceArtifact(anyString(),anyString(),anyString());
126                 verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString());
127                 verify(sdcClientMock, times(0)).updateResourceArtifact(anyString(), anyString(), any(), anyString());
128                 verify(sdcClientMock).addExternalMonitoringReference(anyString(),any(),any(),anyString());
129                 verify(sdcClientMock).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString());
130         }
131
132
133         @Test
134         public void successfulImportAndAttachmentOfVfcmtAlreadyConnectedUpdateFlowTypeCheckoutCheckin() throws Exception {
135                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
136                 when(templateMC.getUuid()).thenReturn("3");
137                 when(sdcClientMock.changeResourceLifecycleState(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(templateMC);
138                 when(sdcClientMock.updateResourceArtifact(anyString(), anyString(), any(), anyString())).thenReturn(new Artifact());
139                 when(sdcClientMock.getResourceArtifact(anyString(),anyString(),anyString())).thenReturn("{\"cid\":\"xsssdaerrwr\"}");
140                 when(templateMC.getLifecycleState()).thenReturn("NOT_CERTIFIED_CHECKIN").thenReturn("NOT_CERTIFIED_CHECKOUT");
141                 emulateListOfArtifactsWithCompositionYmlAndSvcRef();
142                 request.setCloneVFCMT(false);
143                 request.setUpdateFlowType(true);
144                 vfcmtBusinessLogic.importMC(userId, request, requestId);
145
146                 verify(sdcClientMock, times(0)).createResource(userId, request, requestId);
147                 verify(sdcClientMock).getResource(anyString(),anyString());
148                 verify(sdcClientMock).getResourceArtifact(anyString(),anyString(),anyString());
149                 verify(sdcClientMock, times(0)).createResourceArtifact(anyString(),anyString(),any(),anyString());
150                 verify(sdcClientMock, times(1)).updateResourceArtifact(anyString(), anyString(), any(), anyString());
151                 verify(sdcClientMock).addExternalMonitoringReference(anyString(),any(),any(),anyString());
152                 verify(sdcClientMock, times(2)).changeResourceLifecycleState(anyString(),anyString(),anyString(),anyString(),anyString());
153         }
154
155
156         @Test
157         public void successfulFetchVfcmtDataFull() throws Exception {
158                 String templateUuid = "3";
159                 when(templateMC.getUuid()).thenReturn(templateUuid);
160                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
161                 emulateListOfArtifactsWithCompositionYmlAndSvcRef();
162                 when(sdcClientMock.getResourceArtifact(templateUuid, "svcRefArtifactUuid", requestId)).thenReturn("thisIsTheServiceId/resources/thisIsTheVfiName");
163                 when(sdcClientMock.getResourceArtifact(templateUuid, "compositionArtifactUuid", requestId)).thenReturn("\"flowType\":\"Syslog\"");
164                 ResponseEntity<VfcmtData> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId);
165                 verify(sdcClientMock).getResource(anyString(),anyString());
166                 verify(sdcClientMock,times(2)).getResourceArtifact(anyString(),anyString(),anyString());
167                 Assert.assertEquals(200, result.getStatusCodeValue());
168                 Assert.assertEquals("Syslog", result.getBody().getFlowType());
169                 Assert.assertEquals("thisIsTheServiceId", result.getBody().getServiceUuid());
170                 Assert.assertEquals("thisIsTheVfiName", result.getBody().getVfiName());
171         }
172
173         @Test
174         public void successfulFetchVfcmtDataPartial() throws Exception {
175                 String templateUuid = "3";
176                 when(templateMC.getUuid()).thenReturn(templateUuid);
177                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
178                 emulateListOfArtifactsWithCompositionYml();
179                 when(sdcClientMock.getResourceArtifact(templateUuid, "compositionArtifactUuid", requestId)).thenReturn("\"flowType\":\"Syslog\"");
180                 ResponseEntity<VfcmtData> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId);
181                 verify(sdcClientMock).getResource(anyString(),anyString());
182                 verify(sdcClientMock,times(1)).getResourceArtifact(anyString(),anyString(),anyString());
183                 Assert.assertEquals(200, result.getStatusCodeValue());
184                 Assert.assertEquals("Syslog", result.getBody().getFlowType());
185                 Assert.assertEquals(null, result.getBody().getServiceUuid());
186                 Assert.assertEquals(null, result.getBody().getVfiName());
187         }
188
189         @Test
190         public void successfulFetchVfcmtDataEmpty() throws Exception {
191
192                 String templateUuid = "3";
193                 when(templateMC.getUuid()).thenReturn(templateUuid);
194                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
195                 emulateListOfArtifactsWithCompositionYml();
196                 when(sdcClientMock.getResourceArtifact(templateUuid, "compositionArtifactUuid", requestId)).thenReturn("");
197                 ResponseEntity<VfcmtData> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId);
198                 verify(sdcClientMock).getResource(anyString(),anyString());
199                 verify(sdcClientMock,times(1)).getResourceArtifact(anyString(),anyString(),anyString());
200                 Assert.assertEquals(200, result.getStatusCodeValue());
201                 Assert.assertEquals(null, result.getBody().getFlowType());
202                 Assert.assertEquals(null, result.getBody().getServiceUuid());
203                 Assert.assertEquals(null, result.getBody().getVfiName());
204         }
205
206         @Test
207         public void fetchVfcmtDataNoCompositionFound() throws Exception {
208
209                 String templateUuid = "3";
210                 when(templateMC.getUuid()).thenReturn(templateUuid);
211                 when(templateMC.getName()).thenReturn(templateUuid);
212                 when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
213                 ResponseEntity<ResponseFormat> result = vfcmtBusinessLogic.getVfcmtReferenceData(templateUuid, requestId);
214                 verify(sdcClientMock).getResource(anyString(),anyString());
215                 verify(sdcClientMock,times(0)).getResourceArtifact(anyString(),anyString(),anyString());
216                 Assert.assertEquals(404, result.getStatusCodeValue());
217                 Assert.assertEquals("Error – Could not read component 3 details.", result.getBody().getRequestError().getServiceException().getFormattedErrorMessage());
218
219         }
220
221         @Test
222         public void getVfcmtsForMigration() throws Exception {
223                 ExternalReferencesMap connectedVfcmts = new ExternalReferencesMap();
224                 connectedVfcmts.put("11",Arrays.asList("Red", "Blue", "Yellow"));
225                 connectedVfcmts.put("22",Arrays.asList("Ibiza", "Bora Bora", "Mykonos"));
226                 connectedVfcmts.put("33",Arrays.asList("Large", "Medium", "Small"));
227                 connectedVfcmts.put("44",Arrays.asList("Basket", "Foot", "Volley"));
228
229                 when(sdcClientMock.getMonitoringReferences(anyString(),anyString(),anyString(),anyString())).thenReturn(connectedVfcmts);
230
231                 Resource myRedResource = new Resource();
232                 myRedResource.setUuid("Red");
233                 myRedResource.setLastUpdaterUserId("me");
234                 myRedResource.setLifecycleState(NOT_CERTIFIED_CHECKOUT.name());
235
236                 Resource herRaphaelResource = new Resource();
237                 herRaphaelResource.setUuid("Raphael");
238                 herRaphaelResource.setLastUpdaterUserId("her");
239                 herRaphaelResource.setLifecycleState(NOT_CERTIFIED_CHECKOUT.name());
240
241                 Resource myMediumResource = new Resource();
242                 myMediumResource.setUuid("Medium");
243                 myMediumResource.setLastUpdaterUserId("me");
244
245                 Resource herDonateloResource = new Resource();
246                 herDonateloResource.setUuid("Donatelo");
247                 herDonateloResource.setLastUpdaterUserId("her");
248                 herDonateloResource.setVersion("1.0");
249
250                 Resource hisMykonosResource = new Resource();
251                 hisMykonosResource.setUuid("Mykonos");
252                 hisMykonosResource.setLastUpdaterUserId("his");
253                 hisMykonosResource.setLifecycleState(NOT_CERTIFIED_CHECKOUT.name());
254
255                 Resource hisMichaelangeloResource = new Resource();
256                 hisMichaelangeloResource.setUuid("Michaelangelo");
257                 hisMichaelangeloResource.setLastUpdaterUserId("his");
258                 hisMykonosResource.setLifecycleState(CERTIFIED.name());
259                 hisMykonosResource.setVersion("1.1");
260
261         // Versions and connectivity to service shouldn't be part of this test as these are passed to SDC to be
262         // filtered by SDC requests (getMonitoringReference and getResource)
263
264                 List<Resource> theVfcmts = Arrays.asList(myRedResource,herRaphaelResource,myMediumResource,herDonateloResource,hisMykonosResource,hisMichaelangeloResource);
265
266                 when(sdcClientMock.getResources(anyString(),anyString(),anyString(),anyString())).thenReturn(theVfcmts);
267
268                 ResponseEntity<List<Resource>> response = vfcmtBusinessLogic.getVfcmtsForMigration(userId,"service","5544","1.0",requestId);
269
270                 Assert.assertEquals(2, response.getBody().size());
271                 Assert.assertEquals(200, response.getStatusCodeValue());
272         }
273
274         private void emulateListOfArtifactsWithCompositionYml() {
275                 List<Artifact> listOfArtifactCompositionYml = new ArrayList<>();
276                 Artifact compositionArtifact = Mockito.mock(Artifact.class);
277                 when(compositionArtifact.getArtifactName()).thenReturn(DcaeBeConstants.Composition.fileNames.COMPOSITION_YML);
278                 when(compositionArtifact.getArtifactUUID()).thenReturn("compositionArtifactUuid");
279                 when(compositionArtifact.getPayloadData()).thenReturn("{\"flowType\":\"don't override\"}");
280                 listOfArtifactCompositionYml.add(compositionArtifact);
281                 when(templateMC.getArtifacts()).thenReturn(listOfArtifactCompositionYml);
282         }
283
284         private void emulateListOfArtifactsWithCompositionYmlAndSvcRef() {
285                 List<Artifact> listOfArtifactCompositionYml = new ArrayList<>();
286                 Artifact compositionArtifact = Mockito.mock(Artifact.class);
287                 Artifact svcRefArtifact = Mockito.mock(Artifact.class);
288                 when(compositionArtifact.getArtifactName()).thenReturn(DcaeBeConstants.Composition.fileNames.COMPOSITION_YML);
289                 when(compositionArtifact.getArtifactUUID()).thenReturn("compositionArtifactUuid");
290                 when(compositionArtifact.getPayloadData()).thenReturn("{\"flowType\":\"don't override\"}");
291                 when(svcRefArtifact.getArtifactName()).thenReturn(DcaeBeConstants.Composition.fileNames.SVC_REF);
292                 when(svcRefArtifact.getArtifactUUID()).thenReturn("svcRefArtifactUuid");
293                 listOfArtifactCompositionYml.add(compositionArtifact);
294                 listOfArtifactCompositionYml.add(svcRefArtifact);
295                 when(templateMC.getArtifacts()).thenReturn(listOfArtifactCompositionYml);
296         }
297
298         @Test
299         public void uiHasABug_creatingVfcmtWithBadRequestNoServiceUuid_gotResponseWithError400() throws Exception{
300                 RequestError requestError = new RequestError();
301                 requestError.setPolicyException(new PolicyException("POL5000", "Error: Internal Server Error. Please try again later.", null));
302                 when(sdcClientMock.createResource(userId,request,requestId)).thenThrow(new ASDCException(HttpStatus.INTERNAL_SERVER_ERROR, requestError));
303                 CreateVFCMTRequest req = new CreateVFCMTRequest();
304                 req.setServiceUuid(null);
305                 ResponseEntity res = vfcmtBusinessLogic.createMcFromTemplate(userId,req,requestId);
306                 verify(sdcClientMock,times(0)).getResource(anyString(),anyString());
307                 verify(sdcClientMock,times(0)).getResourceArtifact(anyString(),anyString(),anyString());
308                 Assert.assertEquals(400, res.getStatusCodeValue());
309         }
310 }