2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.appc.sdc.artifacts.impl;
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;
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;
44 public class TestArtifactProcessor {
46 AbstractArtifactProcessor abstractArtifactProcessor;
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));
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());
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);
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);
84 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
85 InstantiationException, InvocationTargetException {
87 INotificationData notificationData = (INotificationData)getObject("org.onap.sdc.impl.NotificationDataImpl");
89 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
91 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
92 return notificationData;
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");
100 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
101 invokeMethod(resource,"setArtifacts",serviceArtifacts);
102 invokeMethod(resource,"setResourceName","Vnf");
103 invokeMethod(resource,"setResourceVersion","1.0");
105 resources.add(resource);
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);
119 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
120 Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
122 .filter(constructor1 -> constructor1.getParameterCount()==0)
123 .collect(Collectors.toList())
125 constructor.setAccessible(true);
126 return constructor.newInstance();
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;
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;