1 package org.onap.sdc.dcae.composition.impl;
3 import org.junit.Assert;
4 import org.junit.Before;
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;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.*;
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";
34 private ISdcClient sdcClientMock;
36 private ResourceDetailed templateMC;
39 ReferenceBusinessLogic classUnderTest;
43 classUnderTest.setSdcRestClient(sdcClientMock);
44 new ErrorConfigurationLoader(System.getProperty("user.dir")+"/src/main/webapp/WEB-INF");
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());
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());
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);
80 public void deleteVfcmtReference_deleteSuccess() {
81 classUnderTest.deleteVfcmtReference(userId, "", "", "", "", requestId);
82 verify(sdcClientMock).deleteExternalMonitoringReference(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
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);
97 public void deleteVfcmtReferenceBlueprint_deleteSuccess() throws Exception {
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());
106 public void deleteVfcmtReferenceBlueprint_exceptionSdcGetService() throws Exception {
107 when(sdcClientMock.getService(serviceUuid, requestId)).thenThrow(new RuntimeException(""));
109 ResponseEntity<ResponseFormat> responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId);
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());
115 public void deleteVfcmtReferenceBlueprint_exceptionSdcdeleteInstanceResourceArtifact() throws Exception {
117 doThrow(new RuntimeException("")).when(sdcClientMock).deleteInstanceResourceArtifact(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
119 ResponseEntity<ResponseFormat> responseEntity = classUnderTest.deleteVfcmtReferenceBlueprint(userId, "", monitoringComponentName, serviceUuid, vfiName, "", requestId);
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());