2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.artifact.handler.utils;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import java.io.IOException;
32 import java.nio.charset.Charset;
33 import org.apache.commons.io.IOUtils;
34 import org.json.JSONObject;
35 import org.junit.Test;
36 import org.junit.Ignore;
37 import org.mockito.Mockito;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.document.parameters.DocumentParameters;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.document.parameters.DocumentParametersBuilder;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.request.information.RequestInformation;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.request.information.RequestInformationBuilder;
44 import org.powermock.reflect.Whitebox;
46 public class ArtifactHandlerProviderUtilTest {
48 @Test(expected = Exception.class)
49 public void testProcessTemplate() throws Exception {
50 String artifact_conetent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
51 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
52 JSONObject obj = new JSONObject();
53 obj.put("artifact-name", "reference_JunitTestArtifact");
54 obj.put("artifact-version", "0.01");
55 obj.put("artifact-contents", artifact_conetent);
56 ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
57 ahprovider.processTemplate(obj.toString());
61 @Test(expected = Exception.class)
62 public void testcreateDummyRequestData() throws Exception {
63 String artifact_conetent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
64 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
65 JSONObject obj = new JSONObject();
66 obj.put("artifact-name", "reference_JunitTestArtifact");
67 obj.put("artifact-version", "0.01");
68 obj.put("artifact-contents", artifact_conetent);
69 ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
70 ahprovider.createDummyRequestData();
74 public void testEscapeSql() throws Exception {
75 String testStr = "Test String is 'test'";
76 ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
77 assertEquals("Test String is ''test''", ahprovider.escapeSql(testStr));
81 public void testEscapeUtils() throws Exception {
82 String str = "The Test string is 'test'";
83 assertEquals("The Test string is ''test''", EscapeUtils.escapeSql(str));
87 public void testEscapeUtilsNull() throws Exception {
89 assertNull(EscapeUtils.escapeSql(str));
93 public void testDummyData() throws IOException {
94 String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
95 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
96 ArtifactHandlerProviderUtil ahprovider = Mockito.spy(new ArtifactHandlerProviderUtil());
97 UploadartifactInputBuilder builder = new UploadartifactInputBuilder();
98 DocumentParameters mockDocumentParameters = Mockito.mock(DocumentParameters.class);
99 Mockito.doReturn(artifactContent).when(mockDocumentParameters).getArtifactContents();
100 Mockito.doReturn("ARTIFACT NAME").when(mockDocumentParameters).getArtifactName();
101 builder.setDocumentParameters(mockDocumentParameters);
102 RequestInformation mockRequestInformation = Mockito.mock(RequestInformation.class);
103 Mockito.doReturn("REQUEST ID").when(mockRequestInformation).getRequestId();
104 Mockito.doReturn(SdcArtifactHandlerConstants.DESIGN_TOOL).when(mockRequestInformation).getSource();
105 builder.setRequestInformation(mockRequestInformation);
106 UploadartifactInput uploadArtifactInput = builder.build();
107 Whitebox.setInternalState(ahprovider, "templateData", uploadArtifactInput);
108 assertTrue(ahprovider.createDummyRequestData().startsWith("{\"input\": {\"document-parameters\":{\"service-uuid\":\"TLSUUIDREQUEST ID\""));
112 public void testCreateRequestData() throws IOException {
113 DocumentParameters documentParameters = new DocumentParametersBuilder().setResourceUuid("UUID")
114 .setDistributionId("DistributionID").setServiceName("SERVICE_NAME").setArtifactName("ARTIFACT_NAME")
115 .setArtifactType("ARTIFACT_TYPE").setArtifactUuid("ARTIFACT_UUID").build();
116 RequestInformation requestInformation = new RequestInformationBuilder().setRequestId("REQUEST_ID")
117 .setSource("SOURCE").build();
118 UploadartifactInput artifactInput = new UploadartifactInputBuilder().setDocumentParameters(documentParameters)
119 .setRequestInformation(requestInformation).build();
120 ArtifactHandlerProviderUtil ahProvider = Mockito.spy(new ArtifactHandlerProviderUtil(artifactInput));
121 assertEquals("{\"input\": {\"document-parameters\":{\"service-name\":\"SERVICE_NAME\",\"service-uuid\":\"UUID\","
122 + "\"artifact-uuid\":\"ARTIFACT_UUID\",\"artifact-name\":\"ARTIFACT_NAME\",\"artifact-type\":\"ARTIFACT_TYPE\","
123 + "\"resource-uuid\":\"UUID\",\"distribution-id\":\"DistributionID\"},\"request-information\":{\"request-action\""
124 + ":\"StoreSdcDocumentRequest\",\"source\":\"SOURCE\",\"request-id\":\"REQUEST_ID\"}}}",
125 ahProvider.createRequestData());