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.*;
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;
47 import com.att.research.datarouter.provisioning.FeedServlet;
49 public class testDRFeedsPost extends testBase {
51 public static void setUpBeforeClass() throws Exception {
55 public static void tearDownAfterClass() throws Exception {
59 public void testNormal() {
60 JSONObject jo = buildFeedRequest();
61 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");
69 public void testBadContentType() {
70 JSONObject jo = buildFeedRequest();
71 testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "Junit");
74 public void testNoBehalfHeader() {
75 JSONObject jo = buildFeedRequest();
76 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, null);
79 public void testMissingName() {
80 JSONObject jo = buildFeedRequest();
85 public void testTooLongName() {
86 JSONObject jo = buildFeedRequest();
87 jo.put("name", "123456789012345678901234567890");
91 public void testMissingVersion() {
92 JSONObject jo = buildFeedRequest();
97 public void testTooLongVersion() {
98 JSONObject jo = buildFeedRequest();
99 jo.put("version", "123456789012345678901234567890");
103 public void testTooLongDescription() {
105 JSONObject jo = buildFeedRequest();
106 jo.put("description", s_257);
110 public void testMissingAuthorization() {
111 JSONObject jo = buildFeedRequest();
112 jo.remove("authorization");
116 public void testMissingClassification() {
117 JSONObject jo = buildFeedRequest();
118 JSONObject j2 = jo.getJSONObject("authorization");
119 j2.remove("classification");
123 public void testTooLongClassification() {
124 JSONObject jo = buildFeedRequest();
125 JSONObject j2 = jo.getJSONObject("authorization");
126 j2.put("classification", s_33);
130 public void testNoEndpointIds() {
131 JSONObject jo = buildFeedRequest();
132 JSONObject j2 = jo.getJSONObject("authorization");
133 j2.put("endpoint_ids", new JSONArray());
137 public void testBadIPAddress1() {
138 JSONObject jo = buildFeedRequest();
139 JSONObject j2 = jo.getJSONObject("authorization");
140 JSONArray ja = j2.getJSONArray("endpoint_addrs");
141 ja.put("ZZZ^&#$%@#&^%$@#&^");
145 public void testBadIPAddress2() {
146 JSONObject jo = buildFeedRequest();
147 JSONObject j2 = jo.getJSONObject("authorization");
148 JSONArray ja = j2.getJSONArray("endpoint_addrs");
149 ja.put("135.207.136.678"); // bad IPv4 addr
153 public void testBadIPAddress3() {
154 JSONObject jo = buildFeedRequest();
155 JSONObject j2 = jo.getJSONObject("authorization");
156 JSONArray ja = j2.getJSONArray("endpoint_addrs");
157 ja.put("2001:1890:1110:d000:1a29::17567"); // bad IPv6 addr
161 public void testBadNetMask() {
162 JSONObject jo = buildFeedRequest();
163 JSONObject j2 = jo.getJSONObject("authorization");
164 JSONArray ja = j2.getJSONArray("endpoint_addrs");
165 ja.put("10.10.10.10/64");
169 public void testGoodIPAddress1() {
170 JSONObject jo = buildFeedRequest();
171 JSONObject j2 = jo.getJSONObject("authorization");
172 JSONArray ja = j2.getJSONArray("endpoint_addrs");
173 ja.put("135.207.136.175"); // good IPv4 addr
177 public void testGoodIPAddress2() {
178 JSONObject jo = buildFeedRequest();
179 JSONObject j2 = jo.getJSONObject("authorization");
180 JSONArray ja = j2.getJSONArray("endpoint_addrs");
181 ja.put("2001:1890:1110:d000:1a29::175"); // good IPv6 addr
185 public void testGoodNetMask() {
186 JSONObject jo = buildFeedRequest();
187 JSONObject j2 = jo.getJSONObject("authorization");
188 JSONArray ja = j2.getJSONArray("endpoint_addrs");
189 ja.put("2001:1890:1110:d000:1a29::175/120");
192 private void testCommon(JSONObject jo, int expect) {
193 testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
195 private void testCommon(JSONObject jo, int expect, String ctype, String bhdr) {
196 String url = props.getProperty("test.host") + "/";
197 HttpPost httpPost = new HttpPost(url);
200 httpPost.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
201 String t = jo.toString();
202 HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create(ctype));
203 httpPost.setEntity(body);
205 HttpResponse response = httpclient.execute(httpPost);
206 ckResponse(response, expect);
208 HttpEntity entity = response.getEntity();
209 ctype = entity.getContentType().getValue().trim();
210 int code = response.getStatusLine().getStatusCode();
211 if (code == HttpServletResponse.SC_CREATED && !ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE))
212 fail("Got wrong content type: "+ctype);
214 if (code == HttpServletResponse.SC_CREATED) {
215 Header[] loc = response.getHeaders("Location");
217 fail("Missing Location header.");
220 // do something useful with the response body and ensure it is fully consumed
221 if (ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
222 // ck Location header!
223 JSONObject jo2 = null;
225 jo2 = new JSONObject(new JSONTokener(entity.getContent()));
226 System.err.println(jo2.toString());
227 } catch (Exception e) {
228 fail("Bad JSON: "+e.getMessage());
231 jo2.getString("publisher");
232 JSONObject jo3 = jo2.getJSONObject("links");
233 jo3.getString("self");
234 jo3.getString("publish");
235 jo3.getString("subscribe");
236 jo3.getString("log");
237 } catch (JSONException e) {
238 fail("required field missing from result: "+e.getMessage());
241 EntityUtils.consume(entity);
243 } catch (IOException e) {
244 fail(e.getMessage());
246 httpPost.releaseConnection();
249 private JSONObject buildFeedRequest() {
250 JSONObject jo = new JSONObject();
251 jo.put("name", "JunitFeed");
252 jo.put("version", ""+System.currentTimeMillis()); // make version unique
253 jo.put("description", "Sample feed used by JUnit to test");
255 JSONObject jo2 = new JSONObject();
256 jo2.put("classification", "unrestricted");
258 JSONArray ja = new JSONArray();
259 JSONObject jo3 = new JSONObject();
260 jo3.put("id", "id001");
261 jo3.put("password", "re1kwelj");
262 JSONObject jo4 = new JSONObject();
263 jo4.put("id", "id002");
264 jo4.put("password", "o9eqlmbd");
267 jo2.put("endpoint_ids", ja);
269 ja = new JSONArray();
271 ja.put("192.168.0.1");
272 ja.put("135.207.136.128/25");
273 jo2.put("endpoint_addrs", ja);
275 jo.put("authorization", jo2);
280 curl -v -X POST -H 'X-ATT-DR-ON-BEHALF-OF: tester' -H 'Content-type: application/vnd.att-dr.feed' --user publisher:tomcat \
281 --data "$data" http://127.0.0.1:8080/prov/feed/