1 package org.openecomp.appc.sdc.artifacts.impl;
 
   3 import org.junit.Before;
 
   5 import org.junit.runner.RunWith;
 
   6 import org.mockito.Matchers;
 
   7 import org.mockito.Mockito;
 
   8 import org.openecomp.appc.adapter.message.EventSender;
 
   9 import org.openecomp.appc.exceptions.APPCException;
 
  10 import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService;
 
  11 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
 
  12 import org.openecomp.sdc.api.IDistributionClient;
 
  13 import org.openecomp.sdc.api.notification.IArtifactInfo;
 
  14 import org.openecomp.sdc.api.notification.INotificationData;
 
  15 import org.openecomp.sdc.api.notification.IResourceInstance;
 
  16 import org.powermock.api.mockito.PowerMockito;
 
  17 import org.powermock.modules.junit4.PowerMockRunner;
 
  18 import org.powermock.reflect.Whitebox;
 
  20 import java.lang.reflect.Constructor;
 
  21 import java.lang.reflect.InvocationTargetException;
 
  22 import java.lang.reflect.Method;
 
  23 import java.util.ArrayList;
 
  24 import java.util.Arrays;
 
  25 import java.util.List;
 
  26 import java.util.stream.Collectors;
 
  28 import static org.mockito.Matchers.anyObject;
 
  29 import static org.mockito.Matchers.anyString;
 
  30 import static org.mockito.Mockito.verify;
 
  32 @RunWith(PowerMockRunner.class)
 
  33 public class TestLicenseArtifactProcessor {
 
  35     private LicenseArtifactProcessor artifactProcessor;
 
  36     private ArtifactStorageService storageService;
 
  39     public void setup() throws Exception{
 
  40         IDistributionClient client =  PowerMockito.mock(IDistributionClient.class);
 
  41         EventSender eventSender = PowerMockito.mock(EventSender.class);
 
  42         storageService = PowerMockito.mock(ArtifactStorageService.class);
 
  43         artifactProcessor = Mockito.spy(new LicenseArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
 
  44                 ,getServiceArtifacts().get(0),null));
 
  45         Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
 
  46         PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((SDCArtifact)Matchers.anyObject());
 
  47         PowerMockito.doNothing().when(storageService).storeSDCArtifact(Matchers.anyObject());
 
  50     @Test(expected = org.openecomp.appc.exceptions.APPCException.class)
 
  51     public void testProcessArtifactWithMissingData() throws APPCException {
 
  52         SDCArtifact artifact=new SDCArtifact();
 
  53         artifact.setResourceVersion("RESOURCE VERSION");
 
  54         artifact.setArtifactUUID("123-456-789");
 
  55         artifactProcessor.processArtifact(artifact);
 
  58     public void testProcessArtifact() throws APPCException {
 
  59         PowerMockito.when(storageService.retrieveSDCArtifact(anyString(),anyString(),anyString())).thenReturn(null);
 
  60         SDCArtifact artifact=new SDCArtifact();
 
  61         artifact.setResourceVersion("RESOURCE VERSION");
 
  62         artifact.setArtifactUUID("123-456-789");
 
  63         artifact.setResourceName("Resource Name");
 
  64         artifactProcessor.processArtifact(artifact);
 
  65         verify(storageService,Mockito.times(1)).storeSDCArtifact(anyObject());
 
  68     public void testProcessArtifactWithDuplicateArtifact() throws APPCException {
 
  69         SDCArtifact artifact=new SDCArtifact();
 
  70         artifact.setResourceVersion("RESOURCE VERSION");
 
  71         artifact.setArtifactUUID("123-456-789");
 
  72         artifact.setResourceName("Resource Name");
 
  73         PowerMockito.when(storageService.retrieveSDCArtifact(anyString(),anyString(),anyString())).thenReturn(artifact);
 
  74         artifactProcessor.processArtifact(artifact);
 
  75         verify(storageService,Mockito.times(0)).storeSDCArtifact(anyObject());
 
  78     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
 
  79             InstantiationException, InvocationTargetException {
 
  81         org.openecomp.sdc.api.notification.INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
 
  83         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
 
  85         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
 
  86         return notificationData;
 
  89     private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException,
 
  90             InstantiationException, IllegalAccessException {
 
  91         List<IResourceInstance> resources = new ArrayList<>();
 
  92         IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
 
  94         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
 
  95         invokeMethod(resource,"setArtifacts",serviceArtifacts);
 
  96         invokeMethod(resource,"setResourceName","Vnf");
 
  97         invokeMethod(resource,"setResourceVersion","1.0");
 
  99         resources.add(resource);
 
 103     private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
 
 104         Method[] methods = object.getClass().getDeclaredMethods();
 
 105         for(Method method:methods){
 
 106             if(methodName.equalsIgnoreCase(method.getName())){
 
 107                 method.setAccessible(true);
 
 108                 method.invoke(object,arguments);
 
 113     private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
 
 114         Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
 
 116                 .filter(constructor1 -> constructor1.getParameterCount()==0)
 
 117                 .collect(Collectors.toList())
 
 119         constructor.setAccessible(true);
 
 120         return constructor.newInstance();
 
 123     private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException,
 
 124             InstantiationException, IllegalAccessException {
 
 125         List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
 
 126         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
 
 127         invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
 
 128         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
 
 129         serviceArtifacts.add(artifactInfo);
 
 130         return serviceArtifacts;