Change parent version to snapshot
[appc.git] / appc-inbound / appc-design-services / provider / src / test / java / org / onap / appc / design / services / util / ArtifactHandlerClientTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.design.services.util;
23
24 import static org.hamcrest.CoreMatchers.isA;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.security.NoSuchAlgorithmException;
31 import java.util.Properties;
32 import javax.net.ssl.SSLContext;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42 import org.powermock.reflect.Whitebox;
43 import com.sun.jersey.api.client.Client;
44 import com.sun.jersey.api.client.WebResource;
45 import com.sun.jersey.api.client.WebResource.Builder;
46
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({DesignServiceConstants.class, SSLContext.class, Client.class})
50 public class ArtifactHandlerClientTest {
51     private SSLContext sslContext = PowerMockito.mock(SSLContext.class);
52     private Client client;
53     private WebResource webResource;
54     private Builder builder;
55
56
57     @Rule
58     public ExpectedException expectedEx = ExpectedException.none();
59
60     @Before
61     public void setup() throws URISyntaxException {
62         PowerMockito.mockStatic(DesignServiceConstants.class);
63         PowerMockito.mockStatic(SSLContext.class);
64         PowerMockito.mockStatic(Client.class);
65         sslContext = PowerMockito.mock(SSLContext.class);
66         client = Mockito.mock(Client.class);
67         builder = PowerMockito.mock(Builder.class);
68         webResource = PowerMockito.mock(WebResource.class);
69         PowerMockito.when(Client.create(Mockito.anyObject())).thenReturn(client);
70         webResource = PowerMockito.mock(WebResource.class);
71         PowerMockito.when(client.resource(Mockito.any(URI.class))).thenReturn(webResource);
72     }
73
74     @Test
75     public void testConstructorException() throws IOException {
76         expectedEx.expect(IOException.class);
77         new ArtifactHandlerClient();
78     }
79
80     @Test
81     public void testConstructor() throws IOException {
82         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
83             .thenReturn("src/test/resources");
84         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
85         assertTrue(ahClient instanceof ArtifactHandlerClient);
86         Properties props = Whitebox.getInternalState(ahClient, "props");
87         assertTrue(props.containsKey("appc.upload.provider.url"));
88     }
89
90     @Test
91     public void testCreateArtifactData() throws IOException {
92         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
93             .thenReturn("src/test/resources");
94         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
95         assertEquals("{\"input\": {\"request-information\":{\"request-id\":\"\",\"request-action\":"
96                 + "\"StoreSdcDocumentRequest\",\"source\":\"Design-tool\"},\"document-parameters\":"
97                 + "{\"artifact-version\":\"TEST\",\"artifact-name\":\"TEST\",\"artifact-contents\":"
98                 + "\"TEST\"}}}", ahClient.createArtifactData("{\"" + DesignServiceConstants.ARTIFACT_NAME
99                 + "\":\"TEST\", \"" + DesignServiceConstants.ARTIFACT_VERSOIN + "\":\"TEST\", \"" +
100                 DesignServiceConstants.ARTIFACT_CONTENTS + "\":\"TEST\"}", ""));
101     }
102
103     @Test
104     public void testExecuteArtifactHandlerInternalException() throws IOException, NoSuchAlgorithmException {
105         PowerMockito.when(SSLContext.getInstance("SSL")).thenReturn(sslContext);
106         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
107             .thenReturn("src/test/resources");
108         builder = Mockito.mock(Builder.class);
109         PowerMockito.when(webResource.accept("application/json")).thenReturn(builder);
110         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
111         Properties properties = new Properties();
112         properties.put("appc.upload.user", "TEST_USER");
113         properties.put("appc.upload.provider.url", "http://127.0.0.1:8080/path");
114         properties.put("appc.upload.pass", "enc:password");
115         Whitebox.setInternalState(ahClient, "props", properties);
116         expectedEx.expectCause(isA(ArtifactHandlerInternalException.class));
117         ahClient.execute("", "GET");
118     }
119
120     @Test
121     public void testExecuteIOException() throws IOException, NoSuchAlgorithmException {
122         PowerMockito.when(SSLContext.getInstance("SSL")).thenThrow(new RuntimeException());
123         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
124             .thenReturn("src/test/resources");
125         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
126         expectedEx.expect(IOException.class);
127         ahClient.execute("", "");
128     }
129 }