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 IntegrationTestSubscribePost extends IntegrationTestBase {
50 private int feednum = 0;
53 public static void setUpBeforeClass() throws Exception {
57 public static void tearDownAfterClass() throws Exception {
61 * This is the setUp method.
64 public void setUp() throws Exception {
67 // use the first feed to subscribe to
68 JSONArray ja = db_state.getJSONArray("feeds");
69 for (int i = 0; i < ja.length(); i++) {
70 JSONObject feed0 = ja.getJSONObject(i);
71 if (feed0 != null && !feed0.getBoolean("deleted")) {
72 feednum = feed0.getInt("feedid");
79 public void testNormal() {
80 JSONObject jo = buildSubRequest();
81 testCommon(jo, HttpServletResponse.SC_CREATED);
85 public void testMissingUrl() {
86 JSONObject jo = buildSubRequest();
87 jo.getJSONObject("delivery").remove("url");
88 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
92 public void testTooLongUrl() {
93 JSONObject jo = buildSubRequest();
94 jo.getJSONObject("delivery").put("url", "https://" + s257);
95 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
99 public void testMissingUser() {
100 JSONObject jo = buildSubRequest();
101 jo.getJSONObject("delivery").remove("user");
102 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
106 public void testTooLongUser() {
107 JSONObject jo = buildSubRequest();
108 jo.getJSONObject("delivery").put("user", s33);
109 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
113 public void testMissingPassword() {
114 JSONObject jo = buildSubRequest();
115 jo.getJSONObject("delivery").remove("password");
116 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
120 public void testTooLongPassword() {
121 JSONObject jo = buildSubRequest();
122 jo.getJSONObject("delivery").put("password", s33);
123 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
127 public void testNonBooleanMetadata() {
128 JSONObject jo = buildSubRequest();
129 jo.put("metadataOnly", s33);
130 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
133 private void testCommon(JSONObject jo, int expect) {
134 String url = props.getProperty("test.host") + "/subscribe/" + feednum;
135 HttpPost httpPost = new HttpPost(url);
137 httpPost.addHeader(SubscribeServlet.BEHALF_HEADER, "JUnit");
138 String strJo = jo.toString();
139 HttpEntity body = new ByteArrayEntity(strJo.getBytes(),
140 ContentType.create(SubscribeServlet.SUB_CONTENT_TYPE));
141 httpPost.setEntity(body);
143 HttpResponse response = httpclient.execute(httpPost);
144 ckResponse(response, expect);
146 HttpEntity entity = response.getEntity();
147 String ctype = entity.getContentType().getValue();
148 int code = response.getStatusLine().getStatusCode();
149 if (code == HttpServletResponse.SC_CREATED && !ctype.equals(SubscribeServlet.SUBFULL_CONTENT_TYPE)) {
150 fail("Got wrong content type: " + ctype);
153 // do something useful with the response body and ensure it is fully consumed
154 if (ctype.equals(FeedServlet.SUBFULL_CONTENT_TYPE)) {
155 JSONObject jo2 = null;
157 jo2 = new JSONObject(new JSONTokener(entity.getContent()));
158 } catch (Exception e) {
159 fail("Bad JSON: " + e.getMessage());
162 jo2.getString("subscriber");
163 JSONObject jo3 = jo2.getJSONObject("links");
164 jo3.getString("self");
165 jo3.getString("feed");
166 jo3.getString("log");
167 } catch (JSONException e) {
168 fail("required field missing from result: " + e.getMessage());
171 EntityUtils.consume(entity);
173 } catch (IOException e) {
174 fail(e.getMessage());
176 httpPost.releaseConnection();
180 private JSONObject buildSubRequest() {
181 JSONObject jo2 = new JSONObject();
182 jo2.put("url", "https://www.att.com/");
183 jo2.put("user", "dmr");
184 jo2.put("password", "passw0rd");
185 jo2.put("use100", true);
187 JSONObject jo = new JSONObject();
188 jo.put("delivery", jo2);
189 jo.put("metadataOnly", Boolean.FALSE);