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;
47 import com.att.research.datarouter.provisioning.FeedServlet;
48 import com.att.research.datarouter.provisioning.SubscribeServlet;
50 public class testSubscribePost extends testBase {
51 private int feednum = 0;
54 public static void setUpBeforeClass() throws Exception {
58 public static void tearDownAfterClass() throws Exception {
62 public void setUp() throws Exception {
65 // use the first feed to subscribe to
66 JSONArray ja = db_state.getJSONArray("feeds");
67 for (int i = 0; i < ja.length(); i++) {
68 JSONObject feed0 = ja.getJSONObject(i);
69 if (feed0 != null && !feed0.getBoolean("deleted")) {
70 feednum = feed0.getInt("feedid");
77 public void testNormal() {
78 JSONObject jo = buildSubRequest();
79 testCommon(jo, HttpServletResponse.SC_CREATED);
82 public void testMissingUrl() {
83 JSONObject jo = buildSubRequest();
84 jo.getJSONObject("delivery").remove("url");
85 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
88 public void testTooLongUrl() {
89 JSONObject jo = buildSubRequest();
90 jo.getJSONObject("delivery").put("url", "https://"+s_257);
91 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
94 public void testMissingUser() {
95 JSONObject jo = buildSubRequest();
96 jo.getJSONObject("delivery").remove("user");
97 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
100 public void testTooLongUser() {
101 JSONObject jo = buildSubRequest();
102 jo.getJSONObject("delivery").put("user", s_33);
103 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
106 public void testMissingPassword() {
107 JSONObject jo = buildSubRequest();
108 jo.getJSONObject("delivery").remove("password");
109 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
112 public void testTooLongPassword() {
113 JSONObject jo = buildSubRequest();
114 jo.getJSONObject("delivery").put("password", s_33);
115 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
118 public void testNonBooleanMetadata() {
119 JSONObject jo = buildSubRequest();
120 jo.put("metadataOnly", s_33);
121 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
123 private void testCommon(JSONObject jo, int expect) {
124 String url = props.getProperty("test.host") + "/subscribe/" + feednum;
125 HttpPost httpPost = new HttpPost(url);
127 httpPost.addHeader(SubscribeServlet.BEHALF_HEADER, "JUnit");
128 String t = jo.toString();
129 HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create(SubscribeServlet.SUB_CONTENT_TYPE));
130 httpPost.setEntity(body);
132 HttpResponse response = httpclient.execute(httpPost);
133 ckResponse(response, expect);
135 HttpEntity entity = response.getEntity();
136 String ctype = entity.getContentType().getValue();
137 int code = response.getStatusLine().getStatusCode();
138 if (code == HttpServletResponse.SC_CREATED && !ctype.equals(SubscribeServlet.SUBFULL_CONTENT_TYPE))
139 fail("Got wrong content type: "+ctype);
141 // do something useful with the response body and ensure it is fully consumed
142 if (ctype.equals(FeedServlet.SUBFULL_CONTENT_TYPE)) {
143 JSONObject jo2 = null;
145 jo2 = new JSONObject(new JSONTokener(entity.getContent()));
146 } catch (Exception e) {
147 fail("Bad JSON: "+e.getMessage());
150 jo2.getString("subscriber");
151 JSONObject jo3 = jo2.getJSONObject("links");
152 jo3.getString("self");
153 jo3.getString("feed");
154 jo3.getString("log");
155 } catch (JSONException e) {
156 fail("required field missing from result: "+e.getMessage());
159 EntityUtils.consume(entity);
161 } catch (IOException e) {
162 fail(e.getMessage());
164 httpPost.releaseConnection();
167 private JSONObject buildSubRequest() {
168 JSONObject jo = new JSONObject();
170 JSONObject jo2 = new JSONObject();
171 jo2.put("url", "https://www.att.com/");
172 jo2.put("user", "dmr");
173 jo2.put("password", "passw0rd");
174 jo2.put("use100", true);
176 jo.put("delivery", jo2);
177 jo.put("metadataOnly", Boolean.FALSE);