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