Updated jersey from com.sun.jersey to org.glassfish.jersey
[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 static org.mockito.Matchers.anyObject;
28 import static org.mockito.Mockito.when;
29 import static org.mockito.Matchers.eq;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Mockito.doReturn;
33 import java.io.IOException;
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.security.NoSuchAlgorithmException;
37 import java.util.Properties;
38 import javax.net.ssl.SSLContext;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mockito;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.powermock.reflect.Whitebox;
49 import javax.ws.rs.client.Client;
50 import javax.ws.rs.client.WebTarget;
51 import javax.ws.rs.client.Invocation;
52 import javax.ws.rs.core.Response;
53 import javax.ws.rs.client.Entity;
54 import javax.ws.rs.client.ClientBuilder;
55 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
56
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({DesignServiceConstants.class, SSLContext.class, Client.class,ClientBuilder.class,HttpAuthenticationFeature.class})
59 public class ArtifactHandlerClientTest {
60     private SSLContext sslContext = PowerMockito.mock(SSLContext.class);
61     private Client client;
62     private WebTarget webResource;
63     private Invocation.Builder builder;
64     private ClientBuilder clientBuilder;
65
66
67     @Rule
68     public ExpectedException expectedEx = ExpectedException.none();
69
70     @Before
71     public void setup() throws URISyntaxException {
72         PowerMockito.mockStatic(DesignServiceConstants.class);
73         PowerMockito.mockStatic(SSLContext.class);
74         PowerMockito.mockStatic(Client.class);
75         PowerMockito.mockStatic(ClientBuilder.class);
76         PowerMockito.mockStatic(HttpAuthenticationFeature.class);
77
78         sslContext = PowerMockito.mock(SSLContext.class);
79         client = Mockito.mock(Client.class);
80         builder = PowerMockito.mock(Invocation.Builder.class);
81         clientBuilder = Mockito.mock(ClientBuilder.class);
82         webResource = PowerMockito.mock(WebTarget.class);
83
84         PowerMockito.when(client.target(Mockito.any(URI.class))).thenReturn(webResource);
85         PowerMockito.when(ClientBuilder.newBuilder()).thenReturn(clientBuilder);
86         doReturn(clientBuilder).when(clientBuilder).sslContext(any());
87         doReturn(clientBuilder).when(clientBuilder).hostnameVerifier(any());
88         PowerMockito.when(clientBuilder.build()).thenReturn(client);
89
90
91     }
92
93     @Test
94     public void testConstructorException() throws IOException {
95         expectedEx.expect(IOException.class);
96         new ArtifactHandlerClient();
97     }
98
99     @Test
100     public void testConstructor() throws IOException {
101         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
102             .thenReturn("src/test/resources");
103         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
104         assertTrue(ahClient instanceof ArtifactHandlerClient);
105         Properties props = Whitebox.getInternalState(ahClient, "props");
106         assertTrue(props.containsKey("appc.upload.provider.url"));
107     }
108
109     @Test
110     public void testCreateArtifactData() throws IOException {
111         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
112             .thenReturn("src/test/resources");
113         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
114         assertEquals("{\"input\": {\"request-information\":{\"request-id\":\"\",\"request-action\":"
115                 + "\"StoreSdcDocumentRequest\",\"source\":\"Design-tool\"},\"document-parameters\":"
116                 + "{\"artifact-version\":\"TEST\",\"artifact-name\":\"TEST\",\"artifact-contents\":"
117                 + "\"TEST\"}}}", ahClient.createArtifactData("{\"" + DesignServiceConstants.ARTIFACT_NAME
118                 + "\":\"TEST\", \"" + DesignServiceConstants.ARTIFACT_VERSOIN + "\":\"TEST\", \"" +
119                 DesignServiceConstants.ARTIFACT_CONTENTS + "\":\"TEST\"}", ""));
120     }
121
122     @Test
123     public void testExecuteArtifactHandlerInternalException() throws IOException, NoSuchAlgorithmException {
124         PowerMockito.when(SSLContext.getInstance("SSL")).thenReturn(sslContext);
125         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
126             .thenReturn("src/test/resources");
127         builder = Mockito.mock(Invocation.Builder.class);
128         PowerMockito.when(webResource.request("application/json")).thenReturn(builder);
129         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
130         Properties properties = new Properties();
131         properties.put("appc.upload.user", "TEST_USER");
132         properties.put("appc.upload.provider.url", "http://127.0.0.1:8080/path");
133         properties.put("appc.upload.pass", "enc:password");
134         Whitebox.setInternalState(ahClient, "props", properties);
135         expectedEx.expectCause(isA(ArtifactHandlerInternalException.class));
136         ahClient.execute("", "GET");
137     }
138
139     @Test
140     public void testExecuteIOException() throws IOException, NoSuchAlgorithmException {
141         PowerMockito.when(SSLContext.getInstance("SSL")).thenThrow(new RuntimeException());
142         PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR))
143             .thenReturn("src/test/resources");
144         ArtifactHandlerClient ahClient = new ArtifactHandlerClient();
145         expectedEx.expect(IOException.class);
146         ahClient.execute("", "");
147     }
148 }