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=========================================================
 
  25 package org.onap.appc.artifact.handler.node;
 
  27 import org.apache.commons.io.IOUtils;
 
  28 import org.json.JSONObject;
 
  29 import org.junit.Before;
 
  30 import org.junit.Ignore;
 
  31 import org.junit.Test;
 
  32 import org.mockito.Mockito;
 
  33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  34 import org.powermock.reflect.Whitebox;
 
  35 import org.onap.appc.artifact.handler.dbservices.MockDBService;
 
  36 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
 
  37 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtilTest;
 
  39 import java.util.HashMap;
 
  40 import java.nio.charset.Charset;
 
  42 import static org.junit.Assert.assertTrue;
 
  44 public class ArtifactHandlerNodeTest {
 
  46     private ArtifactHandlerNode artifactHandlerNode;
 
  49     public void setUp() throws Exception {
 
  50         artifactHandlerNode = Mockito.spy(ArtifactHandlerNode.class);
 
  51         Mockito.doReturn(true)
 
  52             .when(artifactHandlerNode)
 
  53             .updateStoreArtifacts(Mockito.any(JSONObject.class), Mockito.any(JSONObject.class));
 
  54         Mockito.doReturn(true)
 
  55             .when(artifactHandlerNode)
 
  56             .storeReferenceData(Mockito.any(JSONObject.class), Mockito.any(JSONObject.class));
 
  60     public void testProcessArtifact() throws Exception {
 
  61         SvcLogicContext ctx = new SvcLogicContext();
 
  62         ctx.setAttribute("test", "test");
 
  63         Map<String, String> inParams = new HashMap<>();
 
  64         JSONObject postData = new JSONObject();
 
  65         JSONObject input = new JSONObject();
 
  66         inParams.put("response_prefix", "prefix");
 
  67         JSONObject requestInfo = new JSONObject();
 
  68         JSONObject documentInfo = new JSONObject();
 
  69         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
 
  70                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
 
  71         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
 
  72         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
 
  73         requestInfo.put("RequestInfo", "testValue");
 
  74         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
 
  75         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
 
  76         postData.put("input", input);
 
  77         inParams.put("postData", postData.toString());
 
  78         artifactHandlerNode.processArtifact(inParams, ctx);
 
  81     @Ignore("Test is taking 60 seconds")
 
  82     @Test(expected = Exception.class)
 
  83     public void testStoreReferenceData() throws Exception {
 
  84         JSONObject documentInfo = new JSONObject();
 
  85         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
 
  86                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
 
  87         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
 
  88         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
 
  89         JSONObject requestInfo = new JSONObject();
 
  90         requestInfo.put("RequestInfo", "testStoreReferenceData");
 
  91         artifactHandlerNode.storeReferenceData(requestInfo, documentInfo);
 
  95     public void testPopulateProtocolReference() throws Exception {
 
  96         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
  98                 "{\"action\": \"TestAction\",\"action-level\": \"vnf\",\"scope\": {\"vnf-type\": \"vDBE-I\",\"vnfc-type\": null},\"template\": \"N\",\"device-protocol\": \"REST\"}";
 
  99         JSONObject content = new JSONObject(contentStr);
 
 100         MockDBService dbService = MockDBService.initialise();
 
 101         Whitebox.invokeMethod(ah, "populateProtocolReference", dbService, content);
 
 105     public void testProcessAndStoreCapablitiesArtifact() throws Exception {
 
 106         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 107         JSONObject capabilities = new JSONObject();
 
 108         JSONObject documentInfo = new JSONObject();
 
 109         MockDBService dbService = MockDBService.initialise();
 
 110         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "testuid");
 
 111         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "testDist");
 
 112         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "testName");
 
 113         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "testDesc");
 
 114         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "testRes");
 
 115         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "testResIns");
 
 116         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "testVers");
 
 117         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "testResType");
 
 118         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "testArtifactUuid");
 
 119         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "testArtifactVers");
 
 120         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "testArtifactDesc");
 
 121         Whitebox.invokeMethod(ah, "processAndStoreCapabilitiesArtifact", dbService, documentInfo, capabilities,
 
 122                 "artifactName", "someVnf");
 
 126     public void testCleanVnfcInstance() throws Exception {
 
 127         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 128         SvcLogicContext ctx = new SvcLogicContext();
 
 129         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
 
 133     @Ignore("Test is taking 60 seconds")
 
 134     @Test(expected = Exception.class)
 
 135     public void testGetArtifactIDException() throws Exception {
 
 136         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 137         String yFileName = "yFileName";
 
 138         Whitebox.invokeMethod(ah, "getArtifactID", yFileName);
 
 141     @Ignore("Test is taking 60 seconds")
 
 142     @Test(expected = Exception.class)
 
 143     public void testStoreUpdateSdcArtifacts() throws Exception {
 
 144         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 146                 "{\"request-information\":{},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
 
 147         JSONObject postData = new JSONObject(postDataStr);
 
 148         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
 
 151     @Ignore("Test is taking 60 seconds")
 
 152     @Test(expected = Exception.class)
 
 153     public void testUpdateStoreArtifacts() throws Exception {
 
 154         JSONObject documentInfo = new JSONObject();
 
 155         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
 
 156                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
 
 157         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
 
 158         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
 
 159         JSONObject requestInfo = new JSONObject();
 
 160         requestInfo.put("RequestInfo", "testupdateStoreArtifacts");
 
 161         artifactHandlerNode.updateStoreArtifacts(requestInfo, documentInfo);
 
 165     public void testCleanArtifactInstanceData() throws Exception {
 
 166         SvcLogicContext ctx = new SvcLogicContext();
 
 167         Whitebox.invokeMethod(artifactHandlerNode, "cleanArtifactInstanceData", ctx);
 
 170     @Ignore("Test is taking 60 seconds")
 
 171     @Test(expected = Exception.class)
 
 172     public void testUpdateYangContents() throws Exception {
 
 173         String artifactId = "1";
 
 174         String yangContents = "SomeContent";
 
 175         Whitebox.invokeMethod(artifactHandlerNode, "updateYangContents", artifactId, yangContents);