1 package org.openecomp.appc.sdc.artifacts.impl;
3 import org.junit.Assert;
4 import org.junit.Before;
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;
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;
23 public class TestArtifactProcessor {
25 AbstractArtifactProcessor abstractArtifactProcessor;
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));
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());
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);
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);
63 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
64 InstantiationException, InvocationTargetException {
66 INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
68 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
70 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
71 return notificationData;
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");
79 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
80 invokeMethod(resource,"setArtifacts",serviceArtifacts);
81 invokeMethod(resource,"setResourceName","Vnf");
82 invokeMethod(resource,"setResourceVersion","1.0");
84 resources.add(resource);
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);
98 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
99 Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
101 .filter(constructor1 -> constructor1.getParameterCount()==0)
102 .collect(Collectors.toList())
104 constructor.setAccessible(true);
105 return constructor.newInstance();
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;
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;