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