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