66550acea0a51b6667d6ea0609785993c4212e19
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.sdc.listener;
26
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;
53
54 import java.io.File;
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;
60 import java.net.URI;
61 import java.net.URISyntaxException;
62 import java.util.ArrayList;
63 import java.util.List;
64
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({IDistributionClient.class,
67         EventSender.class,
68         ArtifactStorageService.class,
69         ToscaCsarArtifactProcessor.class,
70         ArtifactProcessorFactory.class,
71         DependencyModelGenerator.class})
72 public class SdcCallbackTest {
73
74     IDistributionClient client;
75     private EventSender eventSender;
76     private INotificationCallback sdcCallback;
77     private ArtifactStorageService storageService;
78     private ToscaCsarArtifactProcessor artifactProcessor;
79
80
81     @Before
82     public void setup() throws Exception {
83         client =  PowerMockito.mock(IDistributionClient.class);
84         eventSender = PowerMockito.mock(EventSender.class);
85         sdcCallback = new SdcCallback(null,client);
86
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();
96
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);
101
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());
105
106         PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString());
107
108         PowerMockito.doAnswer(new Answer<Object>() {
109             @Override
110             public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
111                 System.out.print(invocationOnMock.getArguments()[0].toString());
112                 return null;
113             }
114         }).when(storageService).storeSDCArtifact((SDCArtifact)Matchers.anyObject());
115     }
116
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());
120
121         byte[] bFile = new byte[(int) file.length()];
122         FileInputStream fileInputStream = new FileInputStream(file);
123         fileInputStream.read(bFile);
124         fileInputStream.close();
125
126         downloadResult.setArtifactPayload(bFile);
127         return downloadResult;
128     }
129
130
131     @Test
132     public void testSDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
133
134
135         INotificationData notificationData = getNotificationData();
136         sdcCallback.activateCallback(notificationData);
137
138         pause();
139     }
140
141     private void pause() {
142         try {
143             Thread.sleep(5000);
144         } catch (InterruptedException e) {
145             e.printStackTrace();
146         }
147     }
148
149     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
150
151         INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
152
153         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
154
155         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
156         return notificationData;
157     }
158
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");
162
163         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
164         invokeMethod(resource,"setArtifacts",serviceArtifacts);
165         invokeMethod(resource,"setResourceName","Vnf");
166         invokeMethod(resource,"setResourceVersion","1.0");
167
168         resources.add(resource);
169         return resources;
170     }
171
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);
178             }
179         }
180     }
181
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();
186     }
187
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;
195     }
196 }