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