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.runner.RunWith;
29 import org.mockito.Matchers;
30 import org.mockito.Mockito;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.openecomp.appc.adapter.message.EventSender;
34 import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService;
35 import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory;
36 import org.openecomp.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor;
37 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
38 import org.openecomp.sdc.api.IDistributionClient;
39 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
40 import org.openecomp.sdc.api.consumer.INotificationCallback;
41 import org.openecomp.sdc.api.notification.IArtifactInfo;
42 import org.openecomp.sdc.api.notification.INotificationData;
43 import org.openecomp.sdc.api.notification.IResourceInstance;
44 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
45 import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl;
46 import org.openecomp.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;
59 import java.net.URISyntaxException;
60 import java.util.ArrayList;
61 import java.util.List;
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({IDistributionClient.class,
66 ArtifactStorageService.class,
67 ToscaCsarArtifactProcessor.class,
68 ArtifactProcessorFactory.class})
69 public class SdcCallbackTest {
71 IDistributionClient client;
72 private EventSender eventSender;
73 private INotificationCallback sdcCallback;
74 private ArtifactStorageService storageService;
75 private ToscaCsarArtifactProcessor artifactProcessor;
79 public void setup() throws Exception {
80 client = PowerMockito.mock(IDistributionClient.class);
81 eventSender = PowerMockito.mock(EventSender.class);
82 sdcCallback = new SdcCallback(null,client);
84 artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
85 ,getServiceArtifacts().get(0),null));
86 storageService = PowerMockito.mock(ArtifactStorageService.class);
87 Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
89 PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject());
90 PowerMockito.doCallRealMethod().when(artifactProcessor).run();
93 PowerMockito.mockStatic(ArtifactProcessorFactory.class);
94 PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(),
95 (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(),
96 (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor);
98 Whitebox.setInternalState(sdcCallback,"eventSender", eventSender);
99 PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject());
100 PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject());
102 PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString());
104 PowerMockito.doAnswer(new Answer<Object>() {
106 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
107 System.out.print(invocationOnMock.getArguments()[0].toString());
110 }).when(storageService).storeSDCArtifact((SDCArtifact)Matchers.anyObject());
113 private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException {
114 DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS,"Download success");
115 File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI());
117 byte[] bFile = new byte[(int) file.length()];
118 FileInputStream fileInputStream = new FileInputStream(file);
119 fileInputStream.read(bFile);
120 fileInputStream.close();
122 downloadResult.setArtifactPayload(bFile);
123 return downloadResult;
128 public void testSDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
131 INotificationData notificationData = getNotificationData();
132 sdcCallback.activateCallback(notificationData);
137 // private void pause(){
139 // Thread.sleep(50000000);
140 // } catch (InterruptedException e) {
141 // e.printStackTrace();
145 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
147 INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
149 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
151 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
152 return notificationData;
155 private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
156 List<IResourceInstance> resources = new ArrayList<>();
157 IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
159 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
160 invokeMethod(resource,"setArtifacts",serviceArtifacts);
161 invokeMethod(resource,"setResourceName","Vnf");
162 invokeMethod(resource,"setResourceVersion","1.0");
164 resources.add(resource);
168 private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
169 Method[] methods = object.getClass().getDeclaredMethods();
170 for(Method method:methods){
171 if(methodName.equalsIgnoreCase(method.getName())){
172 method.setAccessible(true);
173 method.invoke(object,arguments);
178 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
179 Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0];
180 constructor.setAccessible(true);
181 return constructor.newInstance();
184 private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
185 List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
186 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
187 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
188 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
189 serviceArtifacts.add(artifactInfo);
190 return serviceArtifacts;