2 * ============LICENSE_START=======================================================
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
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.sdc.artifacts.impl;
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;
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;
51 import static org.mockito.Matchers.anyObject;
52 import static org.mockito.Matchers.anyString;
53 import static org.mockito.Mockito.verify;
55 @RunWith(PowerMockRunner.class)
56 public class TestLicenseArtifactProcessor {
58 private LicenseArtifactProcessor artifactProcessor;
59 private ArtifactStorageService storageService;
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());
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);
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());
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());
101 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
102 InstantiationException, InvocationTargetException {
104 org.openecomp.sdc.api.notification.INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
106 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
108 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
109 return notificationData;
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");
117 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
118 invokeMethod(resource,"setArtifacts",serviceArtifacts);
119 invokeMethod(resource,"setResourceName","Vnf");
120 invokeMethod(resource,"setResourceVersion","1.0");
122 resources.add(resource);
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);
136 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
137 Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
139 .filter(constructor1 -> constructor1.getParameterCount()==0)
140 .collect(Collectors.toList())
142 constructor.setAccessible(true);
143 return constructor.newInstance();
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;