d4840e685e23ac5e6ab26bca8518a728a4148e83
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.http.server.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Properties;
28
29 import javax.ws.rs.core.Response;
30
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.endpoints.http.client.impl.IndexedHttpClientFactory;
36 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
37 import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory;
38 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
39 import org.onap.policy.common.utils.network.NetworkUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class HttpClientTest {
44
45     private static Logger logger = LoggerFactory.getLogger(HttpClientTest.class);
46
47     @BeforeClass
48     public static void setUp() throws InterruptedException, IOException {
49         logger.info("-- setup() --");
50
51         /* echo server */
52
53         final HttpServletServer echoServerNoAuth =
54                 IndexedHttpServletServerFactory.getInstance().build("echo", "localhost", 6666, "/", false, true);
55         echoServerNoAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
56         echoServerNoAuth.waitedStart(5000);
57
58         if (!NetworkUtil.isTcpPortOpen("localhost", echoServerNoAuth.getPort(), 5, 10000L)) {
59             throw new IllegalStateException("cannot connect to port " + echoServerNoAuth.getPort());
60         }
61
62         /* no auth echo server */
63
64         final HttpServletServer echoServerAuth =
65                 IndexedHttpServletServerFactory.getInstance().build("echo", "localhost", 6667, "/", false, true);
66         echoServerAuth.setBasicAuthentication("x", "y", null);
67         echoServerAuth.addServletPackage("/*", HttpClientTest.class.getPackage().getName());
68         echoServerAuth.waitedStart(5000);
69
70         if (!NetworkUtil.isTcpPortOpen("localhost", echoServerAuth.getPort(), 5, 10000L)) {
71             throw new IllegalStateException("cannot connect to port " + echoServerAuth.getPort());
72         }
73     }
74
75     @AfterClass
76     public static void tearDown() {
77         logger.info("-- tearDown() --");
78
79         IndexedHttpServletServerFactory.getInstance().destroy();
80         IndexedHttpClientFactory.getInstance().destroy();
81     }
82
83     @Test
84     public void testHttpNoAuthClient() throws Exception {
85         logger.info("-- testHttpNoAuthClient() --");
86
87         final HttpClient client = IndexedHttpClientFactory.getInstance().build("testHttpNoAuthClient", false, false,
88                 "localhost", 6666, "junit/echo", null, null, true);
89         final Response response = client.get("hello");
90         final String body = HttpClient.getBody(response, String.class);
91
92         assertTrue(response.getStatus() == 200);
93         assertTrue(body.equals("hello"));
94     }
95
96     @Test
97     public void testHttpAuthClient() throws Exception {
98         logger.info("-- testHttpAuthClient() --");
99
100         final HttpClient client = IndexedHttpClientFactory.getInstance().build("testHttpAuthClient", false, false,
101                 "localhost", 6667, "junit/echo", "x", "y", true);
102         final Response response = client.get("hello");
103         final String body = HttpClient.getBody(response, String.class);
104
105         assertTrue(response.getStatus() == 200);
106         assertTrue(body.equals("hello"));
107     }
108
109     @Test
110     public void testHttpAuthClient401() throws Exception {
111         logger.info("-- testHttpAuthClient401() --");
112
113         final HttpClient client = IndexedHttpClientFactory.getInstance().build("testHttpAuthClient401", false, false,
114                 "localhost", 6667, "junit/echo", null, null, true);
115         final Response response = client.get("hello");
116         assertTrue(response.getStatus() == 401);
117     }
118
119     @Test
120     public void testHttpAuthClientProps() throws Exception {
121         logger.info("-- testHttpAuthClientProps() --");
122
123         final Properties httpProperties = new Properties();
124
125         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP");
126         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP"
127                 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
128         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP"
129                 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
130         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP"
131                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
132         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP"
133                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123");
134         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP"
135                 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, RestMockHealthCheck.class.getName());
136         httpProperties.setProperty(
137                 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX,
138                 "true");
139
140         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP"
141                 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
142         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP"
143                 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
144         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP"
145                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
146         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP"
147                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123");
148         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP"
149                 + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, RestMockHealthCheck.class.getName());
150         httpProperties.setProperty(
151                 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX,
152                 "true");
153
154         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP");
155         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP"
156                 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
157         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP"
158                 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777");
159         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP"
160                 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pap/test");
161         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP"
162                 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false");
163         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP"
164                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpap");
165         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP"
166                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123");
167         httpProperties.setProperty(
168                 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX,
169                 "true");
170
171         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP"
172                 + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
173         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP"
174                 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7778");
175         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP"
176                 + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "pdp");
177         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP"
178                 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false");
179         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP"
180                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "testpdp");
181         httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP"
182                 + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "alpha123");
183         httpProperties.setProperty(
184                 PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX,
185                 "true");
186
187         final List<HttpServletServer> servers = IndexedHttpServletServerFactory.getInstance().build(httpProperties);
188         assertTrue(servers.size() == 2);
189
190         final List<HttpClient> clients = IndexedHttpClientFactory.getInstance().build(httpProperties);
191         assertTrue(clients.size() == 2);
192
193         for (final HttpServletServer server : servers) {
194             server.waitedStart(10000);
195         }
196
197         final HttpClient clientPAP = IndexedHttpClientFactory.getInstance().get("PAP");
198         final Response response = clientPAP.get();
199         assertTrue(response.getStatus() == 200);
200
201         final HttpClient clientPDP = IndexedHttpClientFactory.getInstance().get("PDP");
202         final Response response2 = clientPDP.get("test");
203         assertTrue(response2.getStatus() == 500);
204     }
205
206
207 }