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