Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / test / java / org / onap / appc / artifact / handler / utils / ArtifactHandlerProviderUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.artifact.handler.utils;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import java.io.IOException;
31 import java.nio.charset.Charset;
32 import org.apache.commons.io.IOUtils;
33 import org.json.JSONObject;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.document.parameters.DocumentParameters;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.document.parameters.DocumentParametersBuilder;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.request.information.RequestInformation;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.request.information.RequestInformationBuilder;
43 import org.powermock.reflect.Whitebox;
44
45 public class ArtifactHandlerProviderUtilTest {
46
47     @Test(expected = Exception.class)
48     public void testProcessTemplate() throws Exception {
49         String artifact_conetent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
50                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
51         JSONObject obj = new JSONObject();
52         obj.put("artifact-name", "reference_JunitTestArtifact");
53         obj.put("artifact-version", "0.01");
54         obj.put("artifact-contents", artifact_conetent);
55         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
56         ahprovider.processTemplate(obj.toString());
57     }
58
59     // @Ignore
60     @Test(expected = Exception.class)
61     public void testcreateDummyRequestData() throws Exception {
62         String artifact_conetent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
63                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
64         JSONObject obj = new JSONObject();
65         obj.put("artifact-name", "reference_JunitTestArtifact");
66         obj.put("artifact-version", "0.01");
67         obj.put("artifact-contents", artifact_conetent);
68         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
69         String requestInfo = ahprovider.createDummyRequestData();
70     }
71
72     @Test
73     public void testEscapeSql() throws Exception {
74         String testStr = "Test String is 'test'";
75         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
76         assertEquals("Test String is ''test''", ahprovider.escapeSql(testStr));
77     }
78
79     @Test
80     public void testGetRandom() throws Exception {
81         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
82         Whitebox.invokeMethod(ahprovider, "getRandom");
83         assertTrue(true);
84     }
85
86     @Test
87     public void testEscapeUtils() throws Exception {
88         String str = "The Test string is 'test'";
89         assertEquals("The Test string is ''test''", EscapeUtils.escapeSql(str));
90     }
91
92     @Test
93     public void testEscapeUtilsNull() throws Exception {
94         String str = null;
95         assertNull(EscapeUtils.escapeSql(str));
96     }
97
98     @Test
99     public void testDummyData() throws IOException {
100         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
101                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
102         ArtifactHandlerProviderUtil ahprovider = Mockito.spy(new ArtifactHandlerProviderUtil());
103         UploadartifactInputBuilder builder = new UploadartifactInputBuilder();
104         DocumentParameters mockDocumentParameters = Mockito.mock(DocumentParameters.class);
105         Mockito.doReturn(artifactContent).when(mockDocumentParameters).getArtifactContents();
106         Mockito.doReturn("ARTIFACT NAME").when(mockDocumentParameters).getArtifactName();
107         builder.setDocumentParameters(mockDocumentParameters);
108         RequestInformation mockRequestInformation = Mockito.mock(RequestInformation.class);
109         Mockito.doReturn("REQUEST ID").when(mockRequestInformation).getRequestId();
110         Mockito.doReturn(SdcArtifactHandlerConstants.DESIGN_TOOL).when(mockRequestInformation).getSource();
111         builder.setRequestInformation(mockRequestInformation);
112         UploadartifactInput uploadArtifactInput = builder.build();
113         Whitebox.setInternalState(ahprovider, "templateData", uploadArtifactInput);
114         assertTrue(ahprovider.createDummyRequestData()
115                 .startsWith("{\"input\": {\"document-parameters\":{\"service-uuid\":\"TLSUUIDREQUEST ID\""));
116     }
117
118     @Test
119     public void testCreateRequestData() throws IOException {
120         DocumentParameters documentParameters = new DocumentParametersBuilder().setResourceUuid("UUID")
121                 .setDistributionId("DistributionID").setServiceName("SERVICE_NAME").setArtifactName("ARTIFACT_NAME")
122                 .setArtifactType("ARTIFACT_TYPE").setArtifactUuid("ARTIFACT_UUID").build();
123         RequestInformation requestInformation = new RequestInformationBuilder().setRequestId("REQUEST_ID")
124                 .setSource("SOURCE").build();
125         UploadartifactInput artifactInput = new UploadartifactInputBuilder().setDocumentParameters(documentParameters)
126                 .setRequestInformation(requestInformation).build();
127         ArtifactHandlerProviderUtil ahProvider = Mockito.spy(new ArtifactHandlerProviderUtil(artifactInput));
128         assertEquals("{\"input\": {\"document-parameters\":{\"service-name\":\"SERVICE_NAME\",\"service-uuid\":\"UUID\","
129                 + "\"artifact-uuid\":\"ARTIFACT_UUID\",\"artifact-name\":\"ARTIFACT_NAME\",\"artifact-type\":\"ARTIFACT_TYPE\","
130                 + "\"resource-uuid\":\"UUID\",\"distribution-id\":\"DistributionID\"},\"request-information\":{\"request-action\""
131                 + ":\"StoreSdcDocumentRequest\",\"source\":\"SOURCE\",\"request-id\":\"REQUEST_ID\"}}}",
132                 ahProvider.createRequestData());
133     }
134 }