3971ec248027b3acdfebe24a502264ec297abfc5
[appc.git] / appc-asdc-listener / appc-asdc-listener-bundle / src / test / java / org / openecomp / appc / sdc / listener / AsdcCallbackTest.java
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.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Matchers;
32 import org.mockito.Mockito;
33 import org.mockito.invocation.InvocationOnMock;
34 import org.mockito.stubbing.Answer;
35 import org.openecomp.appc.adapter.message.EventSender;
36 import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService;
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 public class AsdcCallbackTest {
72
73     private IDistributionClient client;
74     private EventSender eventSender;
75     private INotificationCallback asdcCallback;
76     private ArtifactStorageService storageService;
77     private ToscaCsarArtifactProcessor artifactProcessor;
78
79     @Before
80     public void setup() throws Exception {
81         client =  PowerMockito.mock(IDistributionClient.class);
82         eventSender = PowerMockito.mock(EventSender.class);
83         asdcCallback = new AsdcCallback(null,client);
84
85         artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(
86                 client, eventSender, getNotificationData(), getResources().get(0),
87                 getServiceArtifacts().get(0),null));
88         storageService = PowerMockito.mock(ArtifactStorageService.class);
89         Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
90
91         PowerMockito.doCallRealMethod().when(artifactProcessor)
92                 .processArtifact((IDistributionClientDownloadResult) Matchers.anyObject());
93         PowerMockito.doCallRealMethod().when(artifactProcessor).run();
94
95         PowerMockito.mockStatic(ArtifactProcessorFactory.class);
96         PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(),
97                 (EventSender)Matchers.anyObject(),
98                 (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(),
99                 (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor);
100
101         Whitebox.setInternalState(asdcCallback,"eventSender", eventSender);
102         PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject());
103         PowerMockito.doReturn(null).when(client).sendDownloadStatus(
104                 (IDistributionStatusMessage) Matchers.anyObject());
105
106         PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),
107                 Matchers.anyString(), Matchers.anyString());
108
109         PowerMockito.doAnswer(new Answer<Object>() {
110             @Override
111             public Object answer(InvocationOnMock invocationOnMock) throws Exception {
112                 System.out.print(invocationOnMock.getArguments()[0].toString());
113                 return null;
114             }
115         }).when(storageService).storeASDCArtifact((SDCArtifact)Matchers.anyObject());
116     }
117
118     private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException {
119         DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(
120                 DistributionActionResultEnum.SUCCESS,"Download success");
121         File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI());
122
123         byte[] bFile = new byte[(int) file.length()];
124         FileInputStream fileInputStream = new FileInputStream(file);
125         fileInputStream.read(bFile);
126         fileInputStream.close();
127
128         downloadResult.setArtifactPayload(bFile);
129         return downloadResult;
130     }
131
132     @Ignore
133     @Test
134     public void testASDCListener()
135             throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
136         INotificationData notificationData = getNotificationData();
137         asdcCallback.activateCallback(notificationData);
138 //        pause();
139     }
140
141 //    private void pause(){
142 //        try {
143 //            Thread.sleep(50000000);
144 //        } catch (InterruptedException e) {
145 //            e.printStackTrace();
146 //        }
147 //    }
148
149     private INotificationData getNotificationData()
150             throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
151
152         INotificationData notificationData = (INotificationData)getObject(
153                 "org.openecomp.sdc.impl.NotificationDataImpl");
154
155         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
156
157         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
158         return notificationData;
159     }
160
161     private List<IResourceInstance> getResources()
162             throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
163         List<IResourceInstance> resources = new ArrayList<>();
164         IResourceInstance resource = (IResourceInstance)getObject(
165                 "org.openecomp.sdc.impl.JsonContainerResourceInstance");
166
167         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
168         invokeMethod(resource,"setArtifacts",serviceArtifacts);
169         invokeMethod(resource,"setResourceName","Vnf");
170         invokeMethod(resource,"setResourceVersion","1.0");
171
172         resources.add(resource);
173         return resources;
174     }
175
176     private void invokeMethod(Object object, String methodName,Object... arguments)
177             throws IllegalAccessException, InvocationTargetException {
178         Method[] methods = object.getClass().getDeclaredMethods();
179         for(Method method:methods){
180             if(methodName.equalsIgnoreCase(method.getName())){
181                 method.setAccessible(true);
182                 method.invoke(object,arguments);
183             }
184         }
185     }
186
187     private Object getObject(String fqcn)
188             throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
189         Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0];
190         constructor.setAccessible(true);
191         return constructor.newInstance();
192     }
193
194     private List<IArtifactInfo> getServiceArtifacts()
195             throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
196         List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
197         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
198         invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
199         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
200         serviceArtifacts.add(artifactInfo);
201         return serviceArtifacts;
202     }
203 }