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.impl.ArtifactProcessorFactory;
37 import org.openecomp.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor;
38 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
39 import org.openecomp.sdc.api.IDistributionClient;
40 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
41 import org.openecomp.sdc.api.consumer.INotificationCallback;
42 import org.openecomp.sdc.api.notification.IArtifactInfo;
43 import org.openecomp.sdc.api.notification.INotificationData;
44 import org.openecomp.sdc.api.notification.IResourceInstance;
45 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
46 import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl;
47 import org.openecomp.sdc.utils.DistributionActionResultEnum;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import org.powermock.reflect.Whitebox;
54 import java.io.FileInputStream;
55 import java.io.IOException;
56 import java.lang.reflect.Constructor;
57 import java.lang.reflect.InvocationTargetException;
58 import java.lang.reflect.Method;
60 import java.net.URISyntaxException;
61 import java.util.ArrayList;
62 import java.util.List;
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({IDistributionClient.class,
67 ArtifactStorageService.class,
68 ToscaCsarArtifactProcessor.class,
69 ArtifactProcessorFactory.class})
70 public class AsdcCallbackTest {
72 IDistributionClient client;
73 private EventSender eventSender;
74 private INotificationCallback asdcCallback;
75 private ArtifactStorageService storageService;
76 private ToscaCsarArtifactProcessor artifactProcessor;
80 public void setup() throws Exception {
81 client = PowerMockito.mock(IDistributionClient.class);
82 eventSender = PowerMockito.mock(EventSender.class);
83 asdcCallback = new AsdcCallback(null,client);
85 artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
86 ,getServiceArtifacts().get(0),null));
87 storageService = PowerMockito.mock(ArtifactStorageService.class);
88 Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
90 PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject());
91 PowerMockito.doCallRealMethod().when(artifactProcessor).run();
94 PowerMockito.mockStatic(ArtifactProcessorFactory.class);
95 PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(),
96 (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(),
97 (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor);
99 Whitebox.setInternalState(asdcCallback,"eventSender", eventSender);
100 PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject());
101 PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject());
103 PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString());
105 PowerMockito.doAnswer(new Answer<Object>() {
107 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
108 System.out.print(invocationOnMock.getArguments()[0].toString());
111 }).when(storageService).storeASDCArtifact((SDCArtifact)Matchers.anyObject());
114 private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException {
115 DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS,"Download success");
116 File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI());
118 byte[] bFile = new byte[(int) file.length()];
119 FileInputStream fileInputStream = new FileInputStream(file);
120 fileInputStream.read(bFile);
121 fileInputStream.close();
123 downloadResult.setArtifactPayload(bFile);
124 return downloadResult;
129 public void testASDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
132 INotificationData notificationData = getNotificationData();
133 asdcCallback.activateCallback(notificationData);
138 // private void pause(){
140 // Thread.sleep(50000000);
141 // } catch (InterruptedException e) {
142 // e.printStackTrace();
146 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
148 INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
150 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
152 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
153 return notificationData;
156 private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
157 List<IResourceInstance> resources = new ArrayList<>();
158 IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
160 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
161 invokeMethod(resource,"setArtifacts",serviceArtifacts);
162 invokeMethod(resource,"setResourceName","Vnf");
163 invokeMethod(resource,"setResourceVersion","1.0");
165 resources.add(resource);
169 private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
170 Method[] methods = object.getClass().getDeclaredMethods();
171 for(Method method:methods){
172 if(methodName.equalsIgnoreCase(method.getName())){
173 method.setAccessible(true);
174 method.invoke(object,arguments);
179 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
180 Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0];
181 constructor.setAccessible(true);
182 return constructor.newInstance();
185 private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
186 List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
187 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
188 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
189 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
190 serviceArtifacts.add(artifactInfo);
191 return serviceArtifacts;