Update ASDC References to SDC 1/2
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / openecomp / appc / sdc / listener / SdcCallbackTest.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.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;
51
52 import java.io.File;
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;
58 import java.net.URI;
59 import java.net.URISyntaxException;
60 import java.util.ArrayList;
61 import java.util.List;
62
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({IDistributionClient.class,
65                 EventSender.class,
66                 ArtifactStorageService.class,
67                 ToscaCsarArtifactProcessor.class,
68                 ArtifactProcessorFactory.class})
69 public class SdcCallbackTest {
70
71     IDistributionClient client;
72     private EventSender eventSender;
73     private INotificationCallback sdcCallback;
74     private ArtifactStorageService storageService;
75     private ToscaCsarArtifactProcessor artifactProcessor;
76
77
78     @Before
79     public void setup() throws Exception {
80         client =  PowerMockito.mock(IDistributionClient.class);
81         eventSender = PowerMockito.mock(EventSender.class);
82         sdcCallback = new SdcCallback(null,client);
83
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);
88
89         PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject());
90         PowerMockito.doCallRealMethod().when(artifactProcessor).run();
91
92
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);
97
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());
101
102         PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString());
103
104         PowerMockito.doAnswer(new Answer<Object>() {
105             @Override
106             public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
107                 System.out.print(invocationOnMock.getArguments()[0].toString());
108                 return null;
109             }
110         }).when(storageService).storeSDCArtifact((SDCArtifact)Matchers.anyObject());
111     }
112
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());
116
117         byte[] bFile = new byte[(int) file.length()];
118         FileInputStream fileInputStream = new FileInputStream(file);
119         fileInputStream.read(bFile);
120         fileInputStream.close();
121
122         downloadResult.setArtifactPayload(bFile);
123         return downloadResult;
124     }
125
126
127 //    @Test
128     public void testSDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
129
130
131         INotificationData notificationData = getNotificationData();
132         sdcCallback.activateCallback(notificationData);
133
134 //        pause();
135     }
136
137 //    private void pause(){
138 //        try {
139 //            Thread.sleep(50000000);
140 //        } catch (InterruptedException e) {
141 //            e.printStackTrace();
142 //        }
143 //    }
144
145     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
146
147         INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
148
149         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
150
151         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
152         return notificationData;
153     }
154
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");
158
159         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
160         invokeMethod(resource,"setArtifacts",serviceArtifacts);
161         invokeMethod(resource,"setResourceName","Vnf");
162         invokeMethod(resource,"setResourceVersion","1.0");
163
164         resources.add(resource);
165         return resources;
166     }
167
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);
174             }
175         }
176     }
177
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();
182     }
183
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;
191     }
192 }