2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.design.services.util;
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;
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;
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;
58 public ExpectedException expectedEx = ExpectedException.none();
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);
75 public void testConstructorException() throws IOException {
76 expectedEx.expect(IOException.class);
77 new ArtifactHandlerClient();
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"));
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\"}", ""));
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");
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("", "");