Junit Cases for Artifact Handler.
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / test / java / org / openecomp / appc / artifact / handler / node / ArtifactHandlerNodeTest.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
25 package org.openecomp.appc.artifact.handler.node;
26
27 import java.io.InputStream;
28 import java.nio.charset.Charset;
29 import java.util.Enumeration;
30 import java.util.Properties;
31
32 import org.apache.commons.io.IOUtils;
33 import org.json.JSONObject;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.powermock.reflect.Whitebox;
38 import static org.junit.Assert.assertTrue;
39 import org.openecomp.appc.artifact.handler.dbservices.DBService;
40 import org.openecomp.appc.artifact.handler.dbservices.MockDBService;
41 import org.openecomp.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
42 import org.openecomp.appc.artifact.handler.utils.ArtifactHandlerProviderUtilTest;
43
44 import java.util.Map;
45 import java.util.HashMap;
46
47 public class ArtifactHandlerNodeTest {
48
49     @Test
50     public void testProcessArtifact() throws Exception {
51         SvcLogicContext ctx = new SvcLogicContext();
52         ctx.setAttribute("test", "test");
53         MockArtifactHandlerNode ah = new MockArtifactHandlerNode();
54         Map<String, String> inParams = new HashMap<String, String>();
55         JSONObject postData = new JSONObject();
56         JSONObject input = new JSONObject();
57         inParams.put("response_prefix", "prefix");
58         JSONObject requestInfo = new JSONObject();
59         JSONObject documentInfo = new JSONObject();
60         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
61                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
62         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
63         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
64         requestInfo.put("RequestInfo", "testValue");
65         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
66         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
67         postData.put("input", input);
68         inParams.put("postData", postData.toString());
69         ah.processArtifact(inParams, ctx);
70     }
71
72     @Test(expected = Exception.class)
73     public void testStoreReferenceData() throws Exception {
74         MockArtifactHandlerNode ah = new MockArtifactHandlerNode();
75         JSONObject documentInfo = new JSONObject();
76         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
77                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
78         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
79         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
80         JSONObject requestInfo = new JSONObject();
81         requestInfo.put("RequestInfo", "testStoreReferenceData");
82         ah.storeReferenceData(requestInfo, documentInfo);
83     }
84
85     @Test
86     public void testPopulateProtocolReference() throws Exception {
87         ArtifactHandlerNode ah = new ArtifactHandlerNode();
88         String contentStr =
89                 "{\"action\": \"TestAction\",\"action-level\": \"vnf\",\"scope\": {\"vnf-type\": \"vDBE-I\",\"vnfc-type\": null},\"template\": \"N\",\"device-protocol\": \"REST\"}";
90         JSONObject content = new JSONObject(contentStr);
91         MockDBService dbService = MockDBService.initialise();
92         Whitebox.invokeMethod(ah, "populateProtocolReference", dbService, content);
93     }
94
95     @Test
96     public void testProcessAndStoreCapablitiesArtifact() throws Exception {
97         ArtifactHandlerNode ah = new ArtifactHandlerNode();
98         JSONObject capabilities = new JSONObject();
99         JSONObject documentInfo = new JSONObject();
100         MockDBService dbService = MockDBService.initialise();
101         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "testuid");
102         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "testDist");
103         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "testName");
104         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "testDesc");
105         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "testRes");
106         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "testResIns");
107         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSOIN, "testVers");
108         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "testResType");
109         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "testArtifactUuid");
110         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSOIN, "testArtifactVers");
111         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "testArtifactDesc");
112         Whitebox.invokeMethod(ah, "processAndStoreCapablitiesArtifact", dbService, documentInfo, capabilities,
113                 "artifactName", "someVnf");
114     }
115
116     @Test
117     public void testCleanVnfcInstance() throws Exception {
118         ArtifactHandlerNode ah = new ArtifactHandlerNode();
119         SvcLogicContext ctx = new SvcLogicContext();
120         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
121         assertTrue(true);
122     }
123
124     @Test(expected = Exception.class)
125     public void testGetArtifactIDException() throws Exception {
126         ArtifactHandlerNode ah = new ArtifactHandlerNode();
127         String yFileName = "yFileName";
128         Whitebox.invokeMethod(ah, "getArtifactID", yFileName);
129     }
130
131     @Test(expected = Exception.class)
132     public void testStoreUpdateSdcArtifacts() throws Exception {
133         ArtifactHandlerNode ah = new ArtifactHandlerNode();
134         String postDataStr =
135                 "{\"request-information\":{},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
136         JSONObject postData = new JSONObject(postDataStr);
137         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
138     }
139
140     @Test(expected = Exception.class)
141     public void testUpdateStoreArtifacts() throws Exception {
142         MockArtifactHandlerNode ah = new MockArtifactHandlerNode();
143         JSONObject documentInfo = new JSONObject();
144         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
145                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
146         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
147         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
148         JSONObject requestInfo = new JSONObject();
149         requestInfo.put("RequestInfo", "testupdateStoreArtifacts");
150         ah.updateStoreArtifacts(requestInfo, documentInfo);
151     }
152
153     @Test
154     public void testCleanArtifactInstanceData() throws Exception {
155         MockArtifactHandlerNode ah = new MockArtifactHandlerNode();
156         SvcLogicContext ctx = new SvcLogicContext();
157         Whitebox.invokeMethod(ah, "cleanArtifactInstanceData", ctx);
158     }
159
160     @Test(expected = Exception.class)
161     public void testUpdateYangContents() throws Exception {
162         MockArtifactHandlerNode ah = new MockArtifactHandlerNode();
163         String artifactId = "1";
164         String yangContents = "SomeContent";
165         Whitebox.invokeMethod(ah, "updateYangContents", artifactId, yangContents);
166     }
167     
168 }