1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 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
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.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package datarouter.provisioning;
26 import static org.junit.Assert.fail;
28 import java.io.IOException;
30 import javax.servlet.http.HttpServletResponse;
32 import org.apache.http.Header;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.entity.ByteArrayEntity;
37 import org.apache.http.entity.ContentType;
38 import org.apache.http.util.EntityUtils;
39 import org.json.JSONArray;
40 import org.json.JSONException;
41 import org.json.JSONObject;
42 import org.json.JSONTokener;
43 import org.junit.AfterClass;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
48 public class IntegrationTestDrFeedsPost extends IntegrationTestBase {
50 public static void setUpBeforeClass() throws Exception {
54 public static void tearDownAfterClass() throws Exception {
58 public void testNormal() {
59 JSONObject jo = buildFeedRequest();
60 testCommon(jo, HttpServletResponse.SC_CREATED);
64 public void testNormalNoCtVersion() {
65 JSONObject jo = buildFeedRequest();
66 testCommon(jo, HttpServletResponse.SC_CREATED, "application/vnd.att-dr.feed", "JUnit");
70 public void testBadContentType() {
71 JSONObject jo = buildFeedRequest();
72 testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "Junit");
76 public void testNoBehalfHeader() {
77 JSONObject jo = buildFeedRequest();
78 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, null);
82 public void testMissingName() {
83 JSONObject jo = buildFeedRequest();
89 public void testTooLongName() {
90 JSONObject jo = buildFeedRequest();
91 jo.put("name", "123456789012345678901234567890");
96 public void testMissingVersion() {
97 JSONObject jo = buildFeedRequest();
103 public void testTooLongVersion() {
104 JSONObject jo = buildFeedRequest();
105 jo.put("version", "123456789012345678901234567890");
110 public void testTooLongDescription() {
112 JSONObject jo = buildFeedRequest();
113 jo.put("description", s257);
118 public void testMissingAuthorization() {
119 JSONObject jo = buildFeedRequest();
120 jo.remove("authorization");
125 public void testMissingClassification() {
126 JSONObject jo = buildFeedRequest();
127 JSONObject j2 = jo.getJSONObject("authorization");
128 j2.remove("classification");
133 public void testTooLongClassification() {
134 JSONObject jo = buildFeedRequest();
135 JSONObject j2 = jo.getJSONObject("authorization");
136 j2.put("classification", s33);
141 public void testNoEndpointIds() {
142 JSONObject jo = buildFeedRequest();
143 JSONObject j2 = jo.getJSONObject("authorization");
144 j2.put("endpoint_ids", new JSONArray());
149 public void testBadIpAddress1() {
150 JSONObject jo = buildFeedRequest();
151 JSONObject j2 = jo.getJSONObject("authorization");
152 JSONArray ja = j2.getJSONArray("endpoint_addrs");
153 ja.put("ZZZ^&#$%@#&^%$@#&^");
158 public void testBadIpAddress2() {
159 JSONObject jo = buildFeedRequest();
160 JSONObject j2 = jo.getJSONObject("authorization");
161 JSONArray ja = j2.getJSONArray("endpoint_addrs");
162 ja.put("135.207.136.678"); // bad IPv4 addr
167 public void testBadIpAddress3() {
168 JSONObject jo = buildFeedRequest();
169 JSONObject j2 = jo.getJSONObject("authorization");
170 JSONArray ja = j2.getJSONArray("endpoint_addrs");
171 ja.put("2001:1890:1110:d000:1a29::17567"); // bad IPv6 addr
176 public void testBadNetMask() {
177 JSONObject jo = buildFeedRequest();
178 JSONObject j2 = jo.getJSONObject("authorization");
179 JSONArray ja = j2.getJSONArray("endpoint_addrs");
180 ja.put("10.10.10.10/64");
185 public void testGoodIpAddress1() {
186 JSONObject jo = buildFeedRequest();
187 JSONObject j2 = jo.getJSONObject("authorization");
188 JSONArray ja = j2.getJSONArray("endpoint_addrs");
189 ja.put("135.207.136.175"); // good IPv4 addr
194 public void testGoodIpAddress2() {
195 JSONObject jo = buildFeedRequest();
196 JSONObject j2 = jo.getJSONObject("authorization");
197 JSONArray ja = j2.getJSONArray("endpoint_addrs");
198 ja.put("2001:1890:1110:d000:1a29::175"); // good IPv6 addr
203 public void testGoodNetMask() {
204 JSONObject jo = buildFeedRequest();
205 JSONObject j2 = jo.getJSONObject("authorization");
206 JSONArray ja = j2.getJSONArray("endpoint_addrs");
207 ja.put("2001:1890:1110:d000:1a29::175/120");
211 private void testCommon(JSONObject jo, int expect) {
212 testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
215 private void testCommon(JSONObject jo, int expect, String ctype, String bhdr) {
216 String url = props.getProperty("test.host") + "/";
217 HttpPost httpPost = new HttpPost(url);
220 httpPost.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
222 String strJo = jo.toString();
223 HttpEntity body = new ByteArrayEntity(strJo.getBytes(), ContentType.create(ctype));
224 httpPost.setEntity(body);
226 HttpResponse response = httpclient.execute(httpPost);
227 ckResponse(response, expect);
229 HttpEntity entity = response.getEntity();
230 ctype = entity.getContentType().getValue().trim();
231 int code = response.getStatusLine().getStatusCode();
232 if (code == HttpServletResponse.SC_CREATED && !ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
233 fail("Got wrong content type: " + ctype);
236 if (code == HttpServletResponse.SC_CREATED) {
237 Header[] loc = response.getHeaders("Location");
239 fail("Missing Location header.");
243 // do something useful with the response body and ensure it is fully consumed
244 if (ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
245 // ck Location header!
246 JSONObject jo2 = null;
248 jo2 = new JSONObject(new JSONTokener(entity.getContent()));
249 System.err.println(jo2.toString());
250 } catch (Exception e) {
251 fail("Bad JSON: " + e.getMessage());
254 jo2.getString("publisher");
255 JSONObject jo3 = jo2.getJSONObject("links");
256 jo3.getString("self");
257 jo3.getString("publish");
258 jo3.getString("subscribe");
259 jo3.getString("log");
260 } catch (JSONException e) {
261 fail("required field missing from result: " + e.getMessage());
264 EntityUtils.consume(entity);
266 } catch (IOException e) {
267 fail(e.getMessage());
269 httpPost.releaseConnection();
273 private JSONObject buildFeedRequest() {
274 JSONObject jo = new JSONObject();
275 jo.put("name", "JunitFeed");
276 jo.put("version", "" + System.currentTimeMillis()); // make version unique
277 jo.put("description", "Sample feed used by JUnit to test");
279 JSONObject jo2 = new JSONObject();
280 jo2.put("classification", "unrestricted");
282 JSONObject jo3 = new JSONObject();
283 jo3.put("id", "id001");
284 jo3.put("password", "re1kwelj");
285 JSONObject jo4 = new JSONObject();
286 jo4.put("id", "id002");
287 jo4.put("password", "o9eqlmbd");
289 JSONArray ja = new JSONArray();
292 jo2.put("endpoint_ids", ja);
294 ja = new JSONArray();
296 ja.put("192.168.0.1");
297 ja.put("135.207.136.128/25");
298 jo2.put("endpoint_addrs", ja);
300 jo.put("authorization", jo2);
305 curl -v -X POST -H 'X-ATT-DR-ON-BEHALF-OF: tester' -H 'Content-type: application/vnd.att-dr.feed' \
306 --user publisher:tomcat \
307 --data "$data" http://127.0.0.1:8080/prov/feed/