2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.sdc.listener;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Matchers;
31 import org.mockito.Mockito;
32 import org.onap.appc.sdc.artifacts.helper.ArtifactStorageService;
33 import org.onap.appc.sdc.artifacts.helper.DependencyModelGenerator;
34 import org.onap.appc.sdc.artifacts.impl.ArtifactProcessorFactory;
35 import org.onap.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor;
36 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
37 import org.onap.appc.sdc.artifacts.object.SDCReference;
38 import org.onap.appc.srvcomm.messaging.event.EventSender;
39 import org.onap.sdc.api.IDistributionClient;
40 import org.onap.sdc.api.consumer.INotificationCallback;
41 import org.onap.sdc.api.notification.IArtifactInfo;
42 import org.onap.sdc.api.notification.INotificationData;
43 import org.onap.sdc.api.notification.IResourceInstance;
44 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
45 import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
46 import org.onap.sdc.utils.DistributionActionResultEnum;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50 import org.powermock.reflect.Whitebox;
53 import java.io.FileInputStream;
54 import java.io.IOException;
55 import java.lang.reflect.Constructor;
56 import java.lang.reflect.InvocationTargetException;
57 import java.lang.reflect.Method;
58 import java.net.URISyntaxException;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.List;
62 import java.util.stream.Collectors;
64 import static org.mockito.Matchers.anyObject;
65 import static org.mockito.Matchers.anyString;
66 import static org.junit.Assert.assertNotNull;
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({IDistributionClient.class,
71 ArtifactStorageService.class,
72 ToscaCsarArtifactProcessor.class,
73 ArtifactProcessorFactory.class,
74 DependencyModelGenerator.class})
75 public class SdcCallbackTest {
77 private INotificationCallback sdcCallback;
78 private ArtifactStorageService storageService;
79 private ToscaCsarArtifactProcessor artifactProcessor;
80 private String resourceContent;
84 public void setup() throws Exception {
85 IDistributionClient client = PowerMockito.mock(IDistributionClient.class);
86 EventSender eventSender = PowerMockito.mock(EventSender.class);
87 sdcCallback = new SdcCallback(null, client);
89 readInput("/output/resource-ResourceAppc-template.yml").replaceAll(System.lineSeparator(), "");
90 artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(
91 client, eventSender, getNotificationData(), getResources().get(0), getServiceArtifacts().get(0), null));
92 storageService = PowerMockito.mock(ArtifactStorageService.class);
93 Whitebox.setInternalState(artifactProcessor, "artifactStorageService", storageService);
94 DependencyModelGenerator dependencyModelGeneratorMock = PowerMockito.mock(DependencyModelGenerator.class);
95 PowerMockito.when(dependencyModelGeneratorMock.getDependencyModel(anyString(), anyString()))
96 .thenReturn("Dependency_model");
97 Whitebox.setInternalState(artifactProcessor, "dependencyModelGenerator", dependencyModelGeneratorMock);
99 PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact(anyObject());
100 PowerMockito.doCallRealMethod().when(artifactProcessor).run();
102 //PowerMockito.mockStatic(ArtifactProcessorFactory.class);
103 ArtifactProcessorFactory artifactProcessorFactory = PowerMockito.mock(ArtifactProcessorFactory.class);
104 PowerMockito.when(artifactProcessorFactory.getArtifactProcessor(
105 /* (IDistributionClient) */ anyObject(), /* (EventSender) */ anyObject(),
106 /* (INotificationData) */ anyObject(), /* (IResourceInstance) */ anyObject(),
107 /* (IArtifactInfo) */ anyObject(), /* (URI) */ anyObject()))
108 .thenReturn(artifactProcessor);
110 Whitebox.setInternalState(sdcCallback, "eventSender", eventSender);
111 PowerMockito.doReturn(readDownloadResult()).when(client).download(/* (IArtifactInfo) */ anyObject());
112 PowerMockito.doReturn(null).when(client).sendDownloadStatus(/* (IDistributionStatusMessage) */ anyObject());
114 PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(anyString(), anyString(), anyString());
116 PowerMockito.doAnswer(invocationOnMock -> {
117 System.out.print(invocationOnMock.getArguments()[0].toString());
119 }).when(storageService).storeSDCArtifact(/* (SDCArtifact) */ anyObject());
122 private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException {
123 DistributionClientDownloadResultImpl downloadResult =
124 new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "Download success");
125 File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI());
127 byte[] bFile = new byte[(int) file.length()];
128 try (FileInputStream fileInputStream = new FileInputStream(file)) {
129 fileInputStream.read(bFile);
130 } catch (Exception e){
134 downloadResult.setArtifactPayload(bFile);
135 return downloadResult;
140 public void testSDCListener()
141 throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
142 INotificationData notificationData = getNotificationData();
143 sdcCallback.activateCallback(notificationData);
145 assertNotNull(notificationData);
149 public void testArtifacts() throws Exception {
150 PowerMockito.doAnswer(invocationOnMock -> {
151 SDCArtifact artifact = (SDCArtifact) invocationOnMock.getArguments()[0];
152 SDCReference reference = (SDCReference) invocationOnMock.getArguments()[1];
153 Assert.assertEquals("abcd-efgh-ijkl", artifact.getArtifactUUID());
154 Assert.assertEquals("Resource-APPC", reference.getVnfType());
155 Assert.assertEquals(resourceContent.trim(),
156 artifact.getArtifactContent().replaceAll(System.lineSeparator(), ""));
158 }).doAnswer(invocation -> {
159 SDCArtifact artifact = (SDCArtifact) invocation.getArguments()[0];
160 SDCReference reference = (SDCReference) invocation.getArguments()[1];
161 Assert.assertEquals("Resource-APPC", reference.getVnfType());
162 Assert.assertEquals("tosca_dependency_model", reference.getFileCategory());
163 Assert.assertEquals("Dependency_model", artifact.getArtifactContent());
164 Assert.assertEquals("Resource-APPC", artifact.getResourceName());
166 }).when(storageService).storeSDCArtifactWithReference(anyObject(), anyObject());
168 artifactProcessor.processArtifact(readDownloadResult());
171 private void pause(){
174 } catch (InterruptedException e) {
178 private String readInput(String inputFile) throws URISyntaxException {
179 File file = new File(this.getClass().getResource(inputFile).toURI());
180 byte[] bFile = new byte[(int) file.length()];
181 try (FileInputStream fileInputStream = new FileInputStream(file)) {
182 fileInputStream.read(bFile);
183 } catch (Exception e){
186 return new String(bFile);
189 private INotificationData getNotificationData()
190 throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
192 INotificationData notificationData =
193 (INotificationData) getObject("org.onap.sdc.impl.NotificationDataImpl");
195 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
197 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
198 return notificationData;
201 private List<IResourceInstance> getResources()
202 throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
203 List<IResourceInstance> resources = new ArrayList<>();
204 IResourceInstance resource =
205 (IResourceInstance) getObject("org.onap.sdc.impl.JsonContainerResourceInstance");
207 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
208 invokeMethod(resource, "setArtifacts", serviceArtifacts);
209 invokeMethod(resource, "setResourceName", "Vnf");
210 invokeMethod(resource, "setResourceVersion", "1.0");
212 resources.add(resource);
216 private void invokeMethod(Object object, String methodName, Object... arguments)
217 throws IllegalAccessException, InvocationTargetException {
218 Method[] methods = object.getClass().getDeclaredMethods();
219 for(Method method : methods) {
220 if(methodName.equalsIgnoreCase(method.getName())){
221 method.setAccessible(true);
222 method.invoke(object, arguments);
227 private Object getObject(String fqcn)
228 throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
229 Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
231 .filter(constructor1 -> constructor1.getParameterCount() == 0)
232 .collect(Collectors.toList())
234 constructor.setAccessible(true);
235 return constructor.newInstance();
238 private List<IArtifactInfo> getServiceArtifacts()
239 throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
240 List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
241 IArtifactInfo artifactInfo = (IArtifactInfo) getObject("org.onap.sdc.impl.ArtifactInfoImpl");
242 invokeMethod(artifactInfo, "setArtifactType", "TOSCA_CSAR");
243 invokeMethod(artifactInfo, "setArtifactUUID", "abcd-efgh-ijkl");
244 serviceArtifacts.add(artifactInfo);
245 return serviceArtifacts;