Refactor ArtifactHandlerNode
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / test / java / org / onap / 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.onap.appc.artifact.handler.node;
26
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;
38 import java.util.Map;
39 import java.util.HashMap;
40 import java.nio.charset.Charset;
41
42 import static org.junit.Assert.assertTrue;
43
44 public class ArtifactHandlerNodeTest {
45
46     private ArtifactHandlerNode artifactHandlerNode;
47
48     @Before
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));
57     }
58
59     @Test
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);
79     }
80
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);
92     }
93
94     @Test
95     public void testPopulateProtocolReference() throws Exception {
96         ArtifactHandlerNode ah = new ArtifactHandlerNode();
97         String contentStr =
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);
102     }
103
104     @Test
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");
123     }
124
125     @Test
126     public void testCleanVnfcInstance() throws Exception {
127         ArtifactHandlerNode ah = new ArtifactHandlerNode();
128         SvcLogicContext ctx = new SvcLogicContext();
129         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
130         assertTrue(true);
131     }
132
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);
139     }
140
141     @Ignore("Test is taking 60 seconds")
142     @Test(expected = Exception.class)
143     public void testStoreUpdateSdcArtifacts() throws Exception {
144         ArtifactHandlerNode ah = new ArtifactHandlerNode();
145         String postDataStr =
146                 "{\"request-information\":{},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
147         JSONObject postData = new JSONObject(postDataStr);
148         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
149     }
150
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);
162     }
163
164     @Test
165     public void testCleanArtifactInstanceData() throws Exception {
166         SvcLogicContext ctx = new SvcLogicContext();
167         Whitebox.invokeMethod(artifactHandlerNode, "cleanArtifactInstanceData", ctx);
168     }
169
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);
176     }
177     
178 }