Third part of onap rename
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / artifacts / impl / TestLicenseArtifactProcessor.java
1 package org.onap.appc.sdc.artifacts.impl;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.mockito.Matchers;
7 import org.mockito.Mockito;
8 import org.onap.appc.adapter.message.EventSender;
9 import org.onap.appc.exceptions.APPCException;
10 import org.onap.appc.sdc.artifacts.helper.ArtifactStorageService;
11 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
12 import org.openecomp.sdc.api.IDistributionClient;
13 import org.openecomp.sdc.api.notification.IArtifactInfo;
14 import org.openecomp.sdc.api.notification.INotificationData;
15 import org.openecomp.sdc.api.notification.IResourceInstance;
16 import org.powermock.api.mockito.PowerMockito;
17 import org.powermock.modules.junit4.PowerMockRunner;
18 import org.powermock.reflect.Whitebox;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Mockito.verify;
31
32 @RunWith(PowerMockRunner.class)
33 public class TestLicenseArtifactProcessor {
34
35     private LicenseArtifactProcessor artifactProcessor;
36     private ArtifactStorageService storageService;
37
38     @Before
39     public void setup() throws Exception{
40         IDistributionClient client =  PowerMockito.mock(IDistributionClient.class);
41         EventSender eventSender = PowerMockito.mock(EventSender.class);
42         storageService = PowerMockito.mock(ArtifactStorageService.class);
43         artifactProcessor = Mockito.spy(new LicenseArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
44                 ,getServiceArtifacts().get(0),null));
45         Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
46         PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((SDCArtifact)Matchers.anyObject());
47         PowerMockito.doNothing().when(storageService).storeSDCArtifact(Matchers.anyObject());
48     }
49
50     @Test(expected = org.onap.appc.exceptions.APPCException.class)
51     public void testProcessArtifactWithMissingData() throws APPCException {
52         SDCArtifact artifact=new SDCArtifact();
53         artifact.setResourceVersion("RESOURCE VERSION");
54         artifact.setArtifactUUID("123-456-789");
55         artifactProcessor.processArtifact(artifact);
56     }
57     @Test
58     public void testProcessArtifact() throws APPCException {
59         PowerMockito.when(storageService.retrieveSDCArtifact(anyString(),anyString(),anyString())).thenReturn(null);
60         SDCArtifact artifact=new SDCArtifact();
61         artifact.setResourceVersion("RESOURCE VERSION");
62         artifact.setArtifactUUID("123-456-789");
63         artifact.setResourceName("Resource Name");
64         artifactProcessor.processArtifact(artifact);
65         verify(storageService,Mockito.times(1)).storeSDCArtifact(anyObject());
66     }
67     @Test
68     public void testProcessArtifactWithDuplicateArtifact() throws APPCException {
69         SDCArtifact artifact=new SDCArtifact();
70         artifact.setResourceVersion("RESOURCE VERSION");
71         artifact.setArtifactUUID("123-456-789");
72         artifact.setResourceName("Resource Name");
73         PowerMockito.when(storageService.retrieveSDCArtifact(anyString(),anyString(),anyString())).thenReturn(artifact);
74         artifactProcessor.processArtifact(artifact);
75         verify(storageService,Mockito.times(0)).storeSDCArtifact(anyObject());
76     }
77
78     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
79             InstantiationException, InvocationTargetException {
80
81         org.openecomp.sdc.api.notification.INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
82
83         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
84
85         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
86         return notificationData;
87     }
88
89     private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException,
90             InstantiationException, IllegalAccessException {
91         List<IResourceInstance> resources = new ArrayList<>();
92         IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
93
94         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
95         invokeMethod(resource,"setArtifacts",serviceArtifacts);
96         invokeMethod(resource,"setResourceName","Vnf");
97         invokeMethod(resource,"setResourceVersion","1.0");
98
99         resources.add(resource);
100         return resources;
101     }
102
103     private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
104         Method[] methods = object.getClass().getDeclaredMethods();
105         for(Method method:methods){
106             if(methodName.equalsIgnoreCase(method.getName())){
107                 method.setAccessible(true);
108                 method.invoke(object,arguments);
109             }
110         }
111     }
112
113     private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
114         Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
115                 .stream()
116                 .filter(constructor1 -> constructor1.getParameterCount()==0)
117                 .collect(Collectors.toList())
118                 .get(0);
119         constructor.setAccessible(true);
120         return constructor.newInstance();
121     }
122
123     private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException,
124             InstantiationException, IllegalAccessException {
125         List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
126         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
127         invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
128         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
129         serviceArtifacts.add(artifactInfo);
130         return serviceArtifacts;
131     }
132 }