50fcb663cf7fbfda660dbc875e54d431e740d984
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / openecomp / appc / sdc / artifacts / impl / TestArtifactProcessor.java
1 package org.openecomp.appc.sdc.artifacts.impl;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.mockito.Mockito;
7 import org.openecomp.appc.adapter.message.EventSender;
8 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
9 import org.openecomp.sdc.api.IDistributionClient;
10 import org.openecomp.sdc.api.notification.IArtifactInfo;
11 import org.openecomp.sdc.api.notification.INotificationData;
12 import org.openecomp.sdc.api.notification.IResourceInstance;
13 import org.powermock.api.mockito.PowerMockito;
14
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.stream.Collectors;
22
23 public class TestArtifactProcessor {
24
25     AbstractArtifactProcessor abstractArtifactProcessor;
26
27     @Before
28     public void setup() throws Exception{
29         IDistributionClient client =  PowerMockito.mock(IDistributionClient.class);
30         EventSender eventSender = PowerMockito.mock(EventSender.class);
31         abstractArtifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
32                 ,getServiceArtifacts().get(0),null));
33     }
34
35     @Test
36     public void testGetArtifactObject(){
37         SDCArtifact artifact=abstractArtifactProcessor.getArtifactObject("test");
38         Assert.assertEquals("abcd-efgh-ijkl",artifact.getArtifactUUID());
39         Assert.assertEquals("VF_LICENSE",artifact.getArtifactType());
40         Assert.assertEquals("Vnf",artifact.getResourceName());
41         Assert.assertEquals("1.0",artifact.getResourceVersion());
42         Assert.assertEquals("test",artifact.getArtifactContent());
43     }
44
45     @Test
46     public void testFactoryForLicense() throws Exception{
47         IDistributionClient client =  PowerMockito.mock(IDistributionClient.class);
48         EventSender eventSender = PowerMockito.mock(EventSender.class);
49         ArtifactProcessorFactory factory=new ArtifactProcessorFactory();
50         Assert.assertTrue(factory.getArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
51                 ,getServiceArtifacts().get(0),null) instanceof LicenseArtifactProcessor);
52     }
53
54     @Test
55     public void testFactoryForConfig() throws Exception{
56         IDistributionClient client =  PowerMockito.mock(IDistributionClient.class);
57         EventSender eventSender = PowerMockito.mock(EventSender.class);
58         ArtifactProcessorFactory factory=new ArtifactProcessorFactory();
59         Assert.assertTrue(factory.getArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
60                 ,getServiceArtifactsForConfig().get(0),null) instanceof ConfigArtifactProcessor);
61     }
62
63     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
64             InstantiationException, InvocationTargetException {
65
66         INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
67
68         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
69
70         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
71         return notificationData;
72     }
73
74     private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException,
75             InstantiationException, IllegalAccessException {
76         List<IResourceInstance> resources = new ArrayList<>();
77         IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
78
79         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
80         invokeMethod(resource,"setArtifacts",serviceArtifacts);
81         invokeMethod(resource,"setResourceName","Vnf");
82         invokeMethod(resource,"setResourceVersion","1.0");
83
84         resources.add(resource);
85         return resources;
86     }
87
88     private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
89         Method[] methods = object.getClass().getDeclaredMethods();
90         for(Method method:methods){
91             if(methodName.equalsIgnoreCase(method.getName())){
92                 method.setAccessible(true);
93                 method.invoke(object,arguments);
94             }
95         }
96     }
97
98     private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
99         Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
100                 .stream()
101                 .filter(constructor1 -> constructor1.getParameterCount()==0)
102                 .collect(Collectors.toList())
103                 .get(0);
104         constructor.setAccessible(true);
105         return constructor.newInstance();
106     }
107
108     private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException,
109             InstantiationException, IllegalAccessException {
110         List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
111         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
112         invokeMethod(artifactInfo,"setArtifactType","VF_LICENSE");
113         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
114         serviceArtifacts.add(artifactInfo);
115         return serviceArtifacts;
116     }
117
118     private List<IArtifactInfo> getServiceArtifactsForConfig() throws ClassNotFoundException, InvocationTargetException,
119             InstantiationException, IllegalAccessException {
120         List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
121         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
122         invokeMethod(artifactInfo,"setArtifactType","APPC_CONFIG");
123         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
124         serviceArtifacts.add(artifactInfo);
125         return serviceArtifacts;
126     }
127
128 }