2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.sdc.listener;
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.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.openecomp.appc.adapter.message.EventSender;
35 import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService;
36 import org.openecomp.appc.sdc.artifacts.helper.DependencyModelGenerator;
37 import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory;
38 import org.openecomp.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor;
39 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
40 import org.openecomp.sdc.api.IDistributionClient;
41 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
42 import org.openecomp.sdc.api.consumer.INotificationCallback;
43 import org.openecomp.sdc.api.notification.IArtifactInfo;
44 import org.openecomp.sdc.api.notification.INotificationData;
45 import org.openecomp.sdc.api.notification.IResourceInstance;
46 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
47 import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl;
48 import org.openecomp.sdc.utils.DistributionActionResultEnum;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52 import org.powermock.reflect.Whitebox;
55 import java.io.FileInputStream;
56 import java.io.IOException;
57 import java.lang.reflect.Constructor;
58 import java.lang.reflect.InvocationTargetException;
59 import java.lang.reflect.Method;
61 import java.net.URISyntaxException;
62 import java.util.ArrayList;
63 import java.util.List;
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({IDistributionClient.class,
68 ArtifactStorageService.class,
69 ToscaCsarArtifactProcessor.class,
70 ArtifactProcessorFactory.class,
71 DependencyModelGenerator.class})
72 public class SdcCallbackTest {
74 IDistributionClient client;
75 private EventSender eventSender;
76 private INotificationCallback sdcCallback;
77 private ArtifactStorageService storageService;
78 private ToscaCsarArtifactProcessor artifactProcessor;
82 public void setup() throws Exception {
83 client = PowerMockito.mock(IDistributionClient.class);
84 eventSender = PowerMockito.mock(EventSender.class);
85 sdcCallback = new SdcCallback(null,client);
87 artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
88 ,getServiceArtifacts().get(0),null));
89 storageService = PowerMockito.mock(ArtifactStorageService.class);
90 Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
91 DependencyModelGenerator dependencyModelGeneratorMock=PowerMockito.mock(DependencyModelGenerator.class);
92 Whitebox.setInternalState(artifactProcessor,"dependencyModelGenerator",dependencyModelGeneratorMock);
93 PowerMockito.when(dependencyModelGeneratorMock.getDependencyModel(Matchers.anyString(),Matchers.anyString())).thenReturn("Dependency_Model");
94 PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject());
95 PowerMockito.doCallRealMethod().when(artifactProcessor).run();
97 PowerMockito.mockStatic(ArtifactProcessorFactory.class);
98 PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(),
99 (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(),
100 (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor);
102 Whitebox.setInternalState(sdcCallback,"eventSender", eventSender);
103 PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject());
104 PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject());
106 PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString());
108 PowerMockito.doAnswer(new Answer<Object>() {
110 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
111 System.out.print(invocationOnMock.getArguments()[0].toString());
114 }).when(storageService).storeSDCArtifact((SDCArtifact)Matchers.anyObject());
117 private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException {
118 DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS,"Download success");
119 File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI());
121 byte[] bFile = new byte[(int) file.length()];
122 FileInputStream fileInputStream = new FileInputStream(file);
123 fileInputStream.read(bFile);
124 fileInputStream.close();
126 downloadResult.setArtifactPayload(bFile);
127 return downloadResult;
132 public void testSDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
135 INotificationData notificationData = getNotificationData();
136 sdcCallback.activateCallback(notificationData);
141 private void pause() {
144 } catch (InterruptedException e) {
149 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
151 INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
153 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
155 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
156 return notificationData;
159 private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
160 List<IResourceInstance> resources = new ArrayList<>();
161 IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
163 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
164 invokeMethod(resource,"setArtifacts",serviceArtifacts);
165 invokeMethod(resource,"setResourceName","Vnf");
166 invokeMethod(resource,"setResourceVersion","1.0");
168 resources.add(resource);
172 private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
173 Method[] methods = object.getClass().getDeclaredMethods();
174 for(Method method:methods){
175 if(methodName.equalsIgnoreCase(method.getName())){
176 method.setAccessible(true);
177 method.invoke(object,arguments);
182 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
183 Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0];
184 constructor.setAccessible(true);
185 return constructor.newInstance();
188 private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
189 List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
190 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
191 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
192 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
193 serviceArtifacts.add(artifactInfo);
194 return serviceArtifacts;