DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / test / java / org / onap / sdc / dcae / composition / impl / ReferenceBusinessLogicTest.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.junit.runner.RunWith;
7 import org.mockito.InjectMocks;
8 import org.mockito.Mock;
9 import org.mockito.runners.MockitoJUnitRunner;
10 import org.onap.sdc.dcae.client.ISdcClient;
11 import org.onap.sdc.dcae.composition.restmodels.MonitoringComponent;
12 import org.onap.sdc.dcae.composition.restmodels.sdc.*;
13 import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader;
14 import org.onap.sdc.dcae.errormng.ResponseFormat;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.http.ResponseEntity;
17 import org.springframework.web.client.HttpClientErrorException;
18
19 import java.util.*;
20
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.*;
24
25 @RunWith(MockitoJUnitRunner.class)
26 public class ReferenceBusinessLogicTest {
27     private String userId = "me";
28     private String requestId = "1";
29     private String monitoringComponentName = "monitoringComponentName";
30     private String serviceUuid = "serviceUuid";
31     private String vfiName = "vfiName";
32
33     @Mock
34     private ISdcClient sdcClientMock;
35     @Mock
36     private ResourceDetailed templateMC;
37
38     @InjectMocks
39     ReferenceBusinessLogic classUnderTest;
40
41     @Before
42     public void setup(){
43         classUnderTest.setSdcRestClient(sdcClientMock);
44         new ErrorConfigurationLoader(System.getProperty("user.dir")+"/src/main/webapp/WEB-INF");
45     }
46
47     @Test
48     public void successfulFetchMonitoringComponents() throws Exception {
49         when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
50         ExternalReferencesMap refs = new ExternalReferencesMap();
51         refs.put("vfi1", Arrays.asList("a","b","c","d"));
52         refs.put("vfi2", Arrays.asList("u","v","w","x","y","z"));
53         Map<String, List<MonitoringComponent>> result = classUnderTest.fetchMonitoringComponents(refs, requestId);
54         verify(sdcClientMock,times(10)).getResource(anyString(),anyString());
55         Assert.assertEquals(1, result.size());
56         Assert.assertEquals(10, result.get("monitoringComponents").size());
57     }
58
59     @Test
60     public void partialSuccessfulFetchMonitoringComponents() throws Exception {
61         when(sdcClientMock.getResource(anyString(),anyString())).thenReturn(templateMC);
62         when(sdcClientMock.getResource(eq("no_such_uuid"),anyString())).thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
63         ExternalReferencesMap refs = new ExternalReferencesMap();
64         refs.put("vfi1", Collections.singletonList("abc"));
65         refs.put("vfi2", Collections.singletonList("xyz"));
66         refs.put("vfi3", Collections.singletonList("no_such_uuid"));
67         Map<String, List<MonitoringComponent>> result = classUnderTest.fetchMonitoringComponents(refs, requestId);
68         verify(sdcClientMock,times(3)).getResource(anyString(),anyString());
69         Assert.assertEquals(2, result.size());
70         Assert.assertEquals(2, result.get("monitoringComponents").size());
71         Assert.assertEquals(1, result.get("unavailable").size());
72     }
73
74     @Test(expected=RuntimeException.class)
75     public void deleteVfcmtReference_deleteFailed() {
76         doThrow(RuntimeException.class).when(sdcClientMock).deleteExternalMonitoringReference(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
77         classUnderTest.deleteVfcmtReference(userId, "", "", "", "", requestId);
78     }
79     @Test
80     public void deleteVfcmtReference_deleteSuccess() {
81         classUnderTest.deleteVfcmtReference(userId, "", "", "", "", requestId);
82         verify(sdcClientMock).deleteExternalMonitoringReference(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
83     }
84
85     private void mockGetService() throws Exception {
86         ServiceDetailed serviceDetailed = new ServiceDetailed();
87         ResourceInstance resourceInstance = new ResourceInstance();
88         Artifact artifact = new Artifact();
89         artifact.setArtifactName(monitoringComponentName);
90         resourceInstance.setArtifacts(Collections.singletonList(artifact));
91         resourceInstance.setResourceInstanceName(vfiName);
92         serviceDetailed.setResources(Collections.singletonList(resourceInstance));
93         when(sdcClientMock.getService(serviceUuid, requestId)).thenReturn(serviceDetailed);
94     }
95
96     @Test
97     public void deleteVfcmtReferenceBlueprint_deleteSuccess() throws Exception {
98         mockGetService();
99         ResponseEntity responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId);
100         verify(sdcClientMock).getService(serviceUuid, requestId);
101         verify(sdcClientMock).deleteInstanceResourceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
102         Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
103     }
104
105     @Test
106     public void deleteVfcmtReferenceBlueprint_exceptionSdcGetService() throws Exception {
107         when(sdcClientMock.getService(serviceUuid, requestId)).thenThrow(new RuntimeException(""));
108
109         ResponseEntity<ResponseFormat> responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId);
110
111         Assert.assertEquals("The request was partially successful. Removing the attached Blueprint from the service has failed. You must manually delete the artifact.", responseEntity.getBody().getRequestError().getServiceException().getFormattedErrorMessage());
112     }
113
114     @Test
115     public void deleteVfcmtReferenceBlueprint_exceptionSdcdeleteInstanceResourceArtifact() throws Exception {
116         mockGetService();
117         doThrow(new RuntimeException("")).when(sdcClientMock).deleteInstanceResourceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
118
119         ResponseEntity<ResponseFormat> responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId);
120
121         Assert.assertEquals("The request was partially successful. Removing the attached Blueprint from the service has failed. You must manually delete the artifact.", responseEntity.getBody().getRequestError().getServiceException().getFormattedErrorMessage());
122     }
123 }