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.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.ByteArrayEntity;
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.util.EntityUtils;
38 import org.json.JSONArray;
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import org.json.JSONTokener;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
47 import org.onap.dmaap.datarouter.provisioning.SubscribeServlet;
49 public class testSubscribePost extends testBase {
50 private int feednum = 0;
53 public static void setUpBeforeClass() throws Exception {
57 public static void tearDownAfterClass() throws Exception {
61 public void setUp() throws Exception {
64 // use the first feed to subscribe to
65 JSONArray ja = db_state.getJSONArray("feeds");
66 for (int i = 0; i < ja.length(); i++) {
67 JSONObject feed0 = ja.getJSONObject(i);
68 if (feed0 != null && !feed0.getBoolean("deleted")) {
69 feednum = feed0.getInt("feedid");
76 public void testNormal() {
77 JSONObject jo = buildSubRequest();
78 testCommon(jo, HttpServletResponse.SC_CREATED);
81 public void testMissingUrl() {
82 JSONObject jo = buildSubRequest();
83 jo.getJSONObject("delivery").remove("url");
84 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
87 public void testTooLongUrl() {
88 JSONObject jo = buildSubRequest();
89 jo.getJSONObject("delivery").put("url", "https://"+s_257);
90 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
93 public void testMissingUser() {
94 JSONObject jo = buildSubRequest();
95 jo.getJSONObject("delivery").remove("user");
96 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
99 public void testTooLongUser() {
100 JSONObject jo = buildSubRequest();
101 jo.getJSONObject("delivery").put("user", s_33);
102 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
105 public void testMissingPassword() {
106 JSONObject jo = buildSubRequest();
107 jo.getJSONObject("delivery").remove("password");
108 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
111 public void testTooLongPassword() {
112 JSONObject jo = buildSubRequest();
113 jo.getJSONObject("delivery").put("password", s_33);
114 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
117 public void testNonBooleanMetadata() {
118 JSONObject jo = buildSubRequest();
119 jo.put("metadataOnly", s_33);
120 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
122 private void testCommon(JSONObject jo, int expect) {
123 String url = props.getProperty("test.host") + "/subscribe/" + feednum;
124 HttpPost httpPost = new HttpPost(url);
126 httpPost.addHeader(SubscribeServlet.BEHALF_HEADER, "JUnit");
127 String t = jo.toString();
128 HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create(SubscribeServlet.SUB_CONTENT_TYPE));
129 httpPost.setEntity(body);
131 HttpResponse response = httpclient.execute(httpPost);
132 ckResponse(response, expect);
134 HttpEntity entity = response.getEntity();
135 String ctype = entity.getContentType().getValue();
136 int code = response.getStatusLine().getStatusCode();
137 if (code == HttpServletResponse.SC_CREATED && !ctype.equals(SubscribeServlet.SUBFULL_CONTENT_TYPE))
138 fail("Got wrong content type: "+ctype);
140 // do something useful with the response body and ensure it is fully consumed
141 if (ctype.equals(FeedServlet.SUBFULL_CONTENT_TYPE)) {
142 JSONObject jo2 = null;
144 jo2 = new JSONObject(new JSONTokener(entity.getContent()));
145 } catch (Exception e) {
146 fail("Bad JSON: "+e.getMessage());
149 jo2.getString("subscriber");
150 JSONObject jo3 = jo2.getJSONObject("links");
151 jo3.getString("self");
152 jo3.getString("feed");
153 jo3.getString("log");
154 } catch (JSONException e) {
155 fail("required field missing from result: "+e.getMessage());
158 EntityUtils.consume(entity);
160 } catch (IOException e) {
161 fail(e.getMessage());
163 httpPost.releaseConnection();
166 private JSONObject buildSubRequest() {
167 JSONObject jo = new JSONObject();
169 JSONObject jo2 = new JSONObject();
170 jo2.put("url", "https://www.att.com/");
171 jo2.put("user", "dmr");
172 jo2.put("password", "passw0rd");
173 jo2.put("use100", true);
175 jo.put("delivery", jo2);
176 jo.put("metadataOnly", Boolean.FALSE);