Test coverage in artifact-handler-utils package
[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-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
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  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.artifact.handler.utils;
27
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.Test;
35 import org.junit.Ignore;
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.UploadartifactOutput;
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.request.information.RequestInformation;
42 import org.powermock.reflect.Whitebox;
43
44 public class ArtifactHandlerProviderUtilTest {
45
46     @Test(expected = Exception.class)
47     public void testProcessTemplate() throws Exception {
48         String artifact_conetent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
49                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
50         JSONObject obj = new JSONObject();
51         obj.put("artifact-name", "reference_JunitTestArtifact");
52         obj.put("artifact-version", "0.01");
53         obj.put("artifact-contents", artifact_conetent);
54         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
55         ahprovider.processTemplate(obj.toString());
56     }
57
58     @Ignore
59     @Test(expected = Exception.class)
60     public void testcreateDummyRequestData() throws Exception {
61         String artifact_conetent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
62                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
63         JSONObject obj = new JSONObject();
64         obj.put("artifact-name", "reference_JunitTestArtifact");
65         obj.put("artifact-version", "0.01");
66         obj.put("artifact-contents", artifact_conetent);
67         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
68         String requestInfo = ahprovider.createDummyRequestData();
69     }
70
71     @Test
72     public void testEscapeSql() throws Exception {
73         String testStr = "Test String is 'test'";
74         ArtifactHandlerProviderUtil ahprovider = new ArtifactHandlerProviderUtil();
75         ahprovider.escapeSql(testStr);
76         assertTrue(true);
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         EscapeUtils.escapeSql(str);
90         assertTrue(true);
91     }
92
93     @Test
94     public void testEscapeUtilsNull() throws Exception {
95         String str = null;
96         assertNull(EscapeUtils.escapeSql(str));
97     }
98
99     @Test
100     public void testDummyData() throws IOException {
101         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
102                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
103         ArtifactHandlerProviderUtil ahprovider = Mockito.spy(new ArtifactHandlerProviderUtil());
104         UploadartifactInputBuilder builder = new UploadartifactInputBuilder();
105         DocumentParameters mockDocumentParameters = Mockito.mock(DocumentParameters.class);
106         Mockito.doReturn(artifactContent).when(mockDocumentParameters).getArtifactContents();
107         Mockito.doReturn("ARTIFACT NAME").when(mockDocumentParameters).getArtifactName();
108         builder.setDocumentParameters(mockDocumentParameters);
109         RequestInformation mockRequestInformation = Mockito.mock(RequestInformation.class);
110         Mockito.doReturn("REQUEST ID").when(mockRequestInformation).getRequestId();
111         Mockito.doReturn(SdcArtifactHandlerConstants.DESIGN_TOOL).when(mockRequestInformation).getSource();
112         builder.setRequestInformation(mockRequestInformation);
113         UploadartifactInput uploadArtifactInput = builder.build();
114         Whitebox.setInternalState(ahprovider, "templateData", uploadArtifactInput);
115         assertTrue(ahprovider.createDummyRequestData().startsWith("{\"input\": {\"document-parameters\":{\"service-uuid\":\"TLSUUIDREQUEST ID\""));
116     }
117
118     @Test
119     public void testRequestData() throws IOException {
120         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
121                 .getResourceAsStream("templates/reference_template.json"), Charset.defaultCharset());
122         ArtifactHandlerProviderUtil ahprovider = Mockito.spy(new ArtifactHandlerProviderUtil());
123         UploadartifactInputBuilder builder = new UploadartifactInputBuilder();
124         DocumentParameters mockDocumentParameters = Mockito.mock(DocumentParameters.class);
125         Mockito.doReturn(artifactContent).when(mockDocumentParameters).getArtifactContents();
126         Mockito.doReturn("ARTIFACT NAME").when(mockDocumentParameters).getArtifactName();
127         builder.setDocumentParameters(mockDocumentParameters);
128         RequestInformation mockRequestInformation = Mockito.mock(RequestInformation.class);
129         Mockito.doReturn("REQUEST ID").when(mockRequestInformation).getRequestId();
130         Mockito.doReturn(SdcArtifactHandlerConstants.DESIGN_TOOL).when(mockRequestInformation).getSource();
131         builder.setRequestInformation(mockRequestInformation);
132         UploadartifactInput uploadArtifactInput = builder.build();
133         Whitebox.setInternalState(ahprovider, "templateData", uploadArtifactInput);
134         assertTrue(ahprovider.createDummyRequestData().startsWith("{\"input\": {\"document-parameters\":{\"service-uuid\":\"TLSUUIDREQUEST ID\""));
135     }
136 }
137