X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FEventCallbackTest.java;fp=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FEventCallbackTest.java;h=0cc183d663de938bd7afb0a11e4c23049baca3b9;hb=7b1f813441f94261f43ec4f5bb0944ad2570fbdf;hp=b545ea247639e136ac5e646d27d2dd0f565d9150;hpb=4dd316529148d07059d844197cdb676806bdc0c6;p=aai%2Fmodel-loader.git diff --git a/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java b/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java index b545ea2..0cc183d 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java @@ -20,591 +20,100 @@ */ package org.onap.aai.modelloader.notification; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.InstanceCreator; +import java.io.IOException; +import java.util.List; +import java.util.Properties; +import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Mock; +import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; +import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.consumer.INotificationCallback; -import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.INotificationData; -import org.openecomp.sdc.api.notification.IResourceInstance; -import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; -import org.openecomp.sdc.api.results.IDistributionClientResult; -import org.openecomp.sdc.impl.DistributionClientFactory; -import org.openecomp.sdc.impl.DistributionClientImpl; -import org.openecomp.sdc.utils.DistributionActionResultEnum; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; +/** + * Tests {@link EventCallback} + */ +@RunWith(PowerMockRunner.class) public class EventCallbackTest { - ModelLoaderConfig config; - DistributionClientImpl client; - EventCallback callBack; - - @Before - public void init(){ - MockitoAnnotations.initMocks(this); - Properties props = new Properties(); - props.setProperty("ml.distribution.ARTIFACT_TYPES", - "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); - config = new ModelLoaderConfig(props, null); - client = Mockito.spy(DistributionClientImpl.class); - callBack = new EventCallback(client, config); - } - - @Test - public void testActivateCallBack_PublishFailure(){ - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - NotificationDataInstance notification = gson.fromJson(getNotificationWithMultipleResources(), - NotificationDataInstance.class); - - TestConfiguration testConfig = new TestConfiguration(); - Mockito.when(client.getConfiguration()).thenReturn(testConfig); - callBack.activateCallback(notification); - } - - @Test - public void testActivateCallBack_PublishSuccess(){ - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - NotificationDataInstance notification = gson.fromJson(getNotificationWithMultipleResources(), - NotificationDataInstance.class); - - TestConfiguration testConfig = new TestConfiguration(); - Mockito.when(client.download(Mockito.any(IArtifactInfo.class))).thenReturn(buildSuccessResult()); - Mockito.when(client.getConfiguration()).thenReturn(testConfig); - callBack.activateCallback(notification); - } - - private static IDistributionClientDownloadResult buildSuccessResult() { - return new IDistributionClientDownloadResult() { - - @Override - public byte[] getArtifactPayload() { - return new byte[0]; - } - - @Override - public String getArtifactName() { - return ""; - } - - @Override - public String getArtifactFilename() { - return ""; - } - - @Override - public String getDistributionMessageResult() { - return ""; - } - - @Override - public DistributionActionResultEnum getDistributionActionResult() { - return DistributionActionResultEnum.SUCCESS; - } - }; - } - - private String getNotificationWithMultipleResources(){ - return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + - " \"serviceName\" : \"Testnotificationser1\",\r\n" + - " \"serviceVersion\" : \"1.0\",\r\n" + - " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + - " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + - " \"resources\" : [{\r\n" + - " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + - " \"resourceName\" : \"TestNotificationVF1\",\r\n" + - " \"resourceVersion\" : \"1.0\",\r\n" + - " \"resoucreType\" : \"VF\",\r\n" + - " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + - " \"artifacts\" : [{\r\n" + - " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + - " \"artifactType\" : \"YANG_XML\",\r\n" + - " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/" + - " resourceInstances/testnotificationvf11/artifacts/" + - " sample-xml-alldata-1-1.xml\",\r\n" + - " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + - " \"artifactDescription\" : \"MyYang\",\r\n" + - " \"artifactTimeout\" : 0,\r\n" + - " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + - " \"artifactVersion\" : \"1\"\r\n" + - " }" + - " ]\r\n" + - " },\r\n" + - " {\r\n" + - " \"resourceInstanceName\" : \"testnotificationvf12\",\r\n" + - " \"resourceName\" : \"TestNotificationVF1\",\r\n" + - " \"resourceVersion\" : \"1.0\",\r\n" + - " \"resoucreType\" : \"VF\",\r\n" + - " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2e\",\r\n" + - " \"artifacts\" : [{\r\n" + - " \"artifactName\" : \"heat.yaml\",\r\n" + - " \"artifactType\" : \"HEAT\",\r\n" + - " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/" + - " resourceInstances/testnotificationvf11/artifacts/" + - " heat.yaml\",\r\n" + - " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + - " \"artifactDescription\" : \"heat\",\r\n" + - " \"artifactTimeout\" : 60,\r\n" + - " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + - " \"artifactVersion\" : \"1\"\r\n" + - " }" + - " ]\r\n" + - " }\r\n" + - " ]}"; - } -} - -class NotificationDataInstance implements INotificationData{ - - private String distributionID; - private String serviceName; - private String serviceVersion; - private String serviceUUID; - private String serviceDescription; - private String serviceInvariantUUID; - private List resources; - private List serviceArtifacts; - private String workloadContext; - - @Override - public String getDistributionID() { - return distributionID; - } - - @Override - public String getServiceName() { - return serviceName; - } - - @Override - public String getServiceVersion() { - return serviceVersion; - } - - @Override - public String getServiceUUID() { - return serviceUUID; - } - - public void setDistributionID(String distributionID) { - this.distributionID = distributionID; - } - - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - } - - public void setServiceVersion(String serviceVersion) { - this.serviceVersion = serviceVersion; - } - - public void setServiceUUID(String serviceUUID) { - this.serviceUUID = serviceUUID; - } - - - - public String getServiceDescription() { - return serviceDescription; - } - - public void setServiceDescription(String serviceDescription) { - this.serviceDescription = serviceDescription; - } - - public String getWorkloadContext() { - return workloadContext; - } - - public void setWorkloadContext(String workloadContext) { - this.workloadContext = workloadContext; - } - - @Override - public String toString() { - return "NotificationDataImpl [distributionID=" + distributionID + ", serviceName=" + serviceName - + ", serviceVersion=" + serviceVersion + ", serviceUUID=" + serviceUUID + ", serviceDescription=" - + serviceDescription + ", serviceInvariantUUID=" + serviceInvariantUUID + ", resources=" + resources - + ", serviceArtifacts=" + serviceArtifacts + ", workloadContext=" + workloadContext + "]"; - } - - @Override - public List getResources() { - List ret = new ArrayList(); - if( resources != null ){ - ret.addAll(resources); - } - return ret; - } - - public void setResources(List resources){ - this.resources = JsonContainerResourceInstance.convertToJsonContainer(resources); - } - - public List getResourcesImpl(){ - return resources; - } - - List getServiceArtifactsImpl(){ - return serviceArtifacts; - } - - @Override - public List getServiceArtifacts() { - - List temp = new ArrayList(); - if( serviceArtifacts != null ){ - temp.addAll(serviceArtifacts); - } - return temp; - } - - void setServiceArtifacts(List relevantServiceArtifacts) { - serviceArtifacts = relevantServiceArtifacts; - - } - - @Override - public String getServiceInvariantUUID() { - return serviceInvariantUUID; - } - - - public void setServiceInvariantUUID(String serviceInvariantUUID) { - this.serviceInvariantUUID = serviceInvariantUUID; - } - @Override - public IArtifactInfo getArtifactMetadataByUUID(String artifactUUID){ - IArtifactInfo ret = findArtifactInfoByUUID(artifactUUID, serviceArtifacts); - if( ret == null && resources != null ){ - for( JsonContainerResourceInstance currResourceInstance : resources ){ - ret = findArtifactInfoByUUID(artifactUUID, currResourceInstance.getArtifactsImpl()); - if( ret != null ){ - break; - } - } - } - return ret; - - } - - private IArtifactInfo findArtifactInfoByUUID(String artifactUUID, List listToCheck) { - IArtifactInfo ret = null; - if( listToCheck != null ){ - for(IArtifactInfo curr: listToCheck ){ - if(curr.getArtifactUUID().equals(artifactUUID) ){ - ret = curr; - break; - } - } - } - return ret; - } -} - -class ArtifactInfoImpl implements IArtifactInfo{ - - private String artifactName; - private String artifactType; - private String artifactURL; - private String artifactChecksum; - private String artifactDescription; - private Integer artifactTimeout; - private String artifactVersion; - private String artifactUUID; - private String generatedFromUUID; - private IArtifactInfo generatedArtifact; - private List relatedArtifacts; - private List relatedArtifactsInfo; - ArtifactInfoImpl(){} - - private ArtifactInfoImpl(IArtifactInfo iArtifactInfo){ - artifactName = iArtifactInfo.getArtifactName(); - artifactType = iArtifactInfo.getArtifactType(); - artifactURL = iArtifactInfo.getArtifactURL(); - artifactChecksum = iArtifactInfo.getArtifactChecksum(); - artifactDescription = iArtifactInfo.getArtifactDescription(); - artifactTimeout = iArtifactInfo.getArtifactTimeout(); - artifactVersion = iArtifactInfo.getArtifactVersion(); - artifactUUID = iArtifactInfo.getArtifactUUID(); - generatedArtifact = iArtifactInfo.getGeneratedArtifact(); - relatedArtifactsInfo = iArtifactInfo.getRelatedArtifacts(); - relatedArtifacts = fillRelatedArtifactsUUID(relatedArtifactsInfo); - - } - - - private List fillRelatedArtifactsUUID(List relatedArtifactsInfo) { - List relatedArtifactsUUID = null; - if( relatedArtifactsInfo != null && !relatedArtifactsInfo.isEmpty()){ - relatedArtifactsUUID = new ArrayList<>(); - for(IArtifactInfo curr: relatedArtifactsInfo){ - relatedArtifactsUUID.add(curr.getArtifactUUID()); - } - } - return relatedArtifactsUUID; - } - - public static List convertToArtifactInfoImpl(List list){ - List ret = new ArrayList(); - if( list != null ){ - for(IArtifactInfo artifactInfo : list ){ - ret.add(new ArtifactInfoImpl(artifactInfo)); - } - } - return ret; - } - - public String getArtifactName() { - return artifactName; - } - - public void setArtifactName(String artifactName) { - this.artifactName = artifactName; - } - - public String getArtifactType() { - return artifactType; - } - - public void setArtifactType(String artifactType) { - this.artifactType = artifactType; - } - - public String getArtifactURL() { - return artifactURL; - } - - public void setArtifactURL(String artifactURL) { - this.artifactURL = artifactURL; - } - - public String getArtifactChecksum() { - return artifactChecksum; - } - - public void setArtifactChecksum(String artifactChecksum) { - this.artifactChecksum = artifactChecksum; - } - - public String getArtifactDescription() { - return artifactDescription; - } - - public void setArtifactDescription(String artifactDescription) { - this.artifactDescription = artifactDescription; - } - - public Integer getArtifactTimeout() { - return artifactTimeout; - } - - public void setArtifactTimeout(Integer artifactTimeout) { - this.artifactTimeout = artifactTimeout; - } - - @Override - public String toString() { - return "BaseArtifactInfoImpl [artifactName=" + artifactName - + ", artifactType=" + artifactType + ", artifactURL=" - + artifactURL + ", artifactChecksum=" + artifactChecksum - + ", artifactDescription=" + artifactDescription - + ", artifactVersion=" + artifactVersion - + ", artifactUUID=" + artifactUUID - + ", artifactTimeout=" + artifactTimeout + "]"; - } - - public String getArtifactVersion() { - return artifactVersion; - } - - public void setArtifactVersion(String artifactVersion) { - this.artifactVersion = artifactVersion; - } - - public String getArtifactUUID() { - return artifactUUID; - } - - public void setArtifactUUID(String artifactUUID) { - this.artifactUUID = artifactUUID; - } - - public String getGeneratedFromUUID() { - return generatedFromUUID; - } - - public void setGeneratedFromUUID(String generatedFromUUID) { - this.generatedFromUUID = generatedFromUUID; - } - - public IArtifactInfo getGeneratedArtifact() { - return generatedArtifact; - } - - public void setGeneratedArtifact(IArtifactInfo generatedArtifact) { - this.generatedArtifact = generatedArtifact; - } - - public List getRelatedArtifacts(){ - List temp = new ArrayList(); - if( relatedArtifactsInfo != null ){ - temp.addAll(relatedArtifactsInfo); - } - return temp; - } - - public void setRelatedArtifacts(List relatedArtifacts) { - this.relatedArtifacts = relatedArtifacts; - } - - public void setRelatedArtifactsInfo(List relatedArtifactsInfo) { - this.relatedArtifactsInfo = relatedArtifactsInfo; - } + private static final String CONFIG_FILE = "model-loader.properties"; - public List getRelatedArtifactsUUID(){ - return relatedArtifacts; - } -} - -class JsonContainerResourceInstance implements IResourceInstance{ - JsonContainerResourceInstance (){} - private String resourceInstanceName; - private String resourceCustomizationUUID; - private String resourceName; - private String resourceVersion; - private String resoucreType; - private String resourceUUID; - private String resourceInvariantUUID; - private String category; - private String subcategory; - private List artifacts; + private ModelLoaderConfig config; + private Properties configProperties; + private EventCallback eventCallback; - private JsonContainerResourceInstance(IResourceInstance resourceInstance){ - resourceInstanceName = resourceInstance.getResourceInstanceName(); - resourceCustomizationUUID = resourceInstance.getResourceCustomizationUUID(); - resourceName = resourceInstance.getResourceName(); - resourceVersion = resourceInstance.getResourceVersion(); - resoucreType = resourceInstance.getResourceType(); - resourceUUID = resourceInstance.getResourceUUID(); - resourceInvariantUUID = resourceInstance.getResourceInvariantUUID(); - category = resourceInstance.getCategory(); - subcategory = resourceInstance.getSubcategory(); - artifacts = ArtifactInfoImpl.convertToArtifactInfoImpl(resourceInstance.getArtifacts()); - } - - public static List convertToJsonContainer(List resources){ - List buildResources = new ArrayList(); - if( resources != null ){ - for( IResourceInstance resourceInstance : resources ){ - buildResources.add(new JsonContainerResourceInstance(resourceInstance)); - } - } - return buildResources; - } + private ArtifactDeploymentManager mockArtifactDeploymentManager; + private ArtifactDownloadManager mockArtifactDownloadManager; + private IDistributionClient mockDistributionClient; - @Override - public String getResourceInstanceName() { - return resourceInstanceName; - } - - public void setResourceInstanceName(String resourceInstanceName) { - this.resourceInstanceName = resourceInstanceName; - } - - @Override - public String getResourceName() { - return resourceName; - } - - public void setResourceName(String resourceName) { - this.resourceName = resourceName; - } - - @Override - public String getResourceVersion() { - return resourceVersion; - } - - public void setResourceVersion(String resourceVersion) { - this.resourceVersion = resourceVersion; - } + @Before + public void setup() throws IOException { + configProperties = new Properties(); + configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); + config = new ModelLoaderConfig(configProperties, null); - @Override - public String getResourceType() { - return resoucreType; - } + mockArtifactDeploymentManager = PowerMockito.mock(ArtifactDeploymentManager.class); + mockArtifactDownloadManager = PowerMockito.mock(ArtifactDownloadManager.class); + mockDistributionClient = PowerMockito.mock(IDistributionClient.class); - public void setResoucreType(String resoucreType) { - this.resoucreType = resoucreType; - } - - @Override - public String getResourceUUID() { - return resourceUUID; - } + eventCallback = new EventCallback(mockDistributionClient, config); - public void setResourceUUID(String resourceUUID) { - this.resourceUUID = resourceUUID; + Whitebox.setInternalState(eventCallback, mockArtifactDeploymentManager); + Whitebox.setInternalState(eventCallback, mockArtifactDownloadManager); } - @Override - public List getArtifacts() { - List temp = new ArrayList(); - if( artifacts != null ){ - temp.addAll(artifacts); - } - return temp; + @After + public void tearDown() { + config = null; + configProperties = null; + eventCallback = null; + mockArtifactDeploymentManager = null; + mockArtifactDownloadManager = null; + mockDistributionClient = null; } - public void setArtifacts(List artifacts) { - this.artifacts = artifacts; - } + @Test + @SuppressWarnings("unchecked") + public void activateCallback_downloadFails() { + INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - public List getArtifactsImpl(){ - return artifacts; - } + PowerMockito.when(mockArtifactDownloadManager.downloadArtifacts(Mockito.any(INotificationData.class), + Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class))) + .thenReturn(false); - @Override - public String getResourceInvariantUUID() { - return resourceInvariantUUID; - } + eventCallback.activateCallback(data); - public void setResourceInvariantUUID(String resourceInvariantUUID) { - this.resourceInvariantUUID = resourceInvariantUUID; - } - public String getResourceCustomizationUUID() { - return resourceCustomizationUUID; + Mockito.verify(mockArtifactDownloadManager).downloadArtifacts(Mockito.any(INotificationData.class), + Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)); + Mockito.verifyZeroInteractions(mockArtifactDeploymentManager); } - public void setResourceCustomizationUUID(String resourceCustomizationUUID) { - this.resourceCustomizationUUID = resourceCustomizationUUID; - } + @SuppressWarnings("unchecked") + @Test + public void activateCallback() throws BabelArtifactParsingException { + INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - public String getCategory() { - return category; - } + PowerMockito.when(mockArtifactDownloadManager.downloadArtifacts(Mockito.any(INotificationData.class), + Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class))) + .thenReturn(true); - public void setCategory(String category) { - this.category = category; - } + PowerMockito + .when(mockArtifactDeploymentManager.deploy(Mockito.any(INotificationData.class), + Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class))) + .thenReturn(true); - public String getSubcategory() { - return subcategory; - } + eventCallback.activateCallback(data); - public void setSubcategory(String subcategory) { - this.subcategory = subcategory; + Mockito.verify(mockArtifactDownloadManager).downloadArtifacts(Mockito.any(INotificationData.class), + Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)); + Mockito.verify(mockArtifactDeploymentManager).deploy(Mockito.any(INotificationData.class), + Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)); } }