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;
46 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
48 public class testDRFeedsPost extends testBase {
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);
63 public void testNormalNoCTVersion() {
64 JSONObject jo = buildFeedRequest();
65 testCommon(jo, HttpServletResponse.SC_CREATED, "application/vnd.att-dr.feed", "JUnit");
68 public void testBadContentType() {
69 JSONObject jo = buildFeedRequest();
70 testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "Junit");
73 public void testNoBehalfHeader() {
74 JSONObject jo = buildFeedRequest();
75 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, null);
78 public void testMissingName() {
79 JSONObject jo = buildFeedRequest();
84 public void testTooLongName() {
85 JSONObject jo = buildFeedRequest();
86 jo.put("name", "123456789012345678901234567890");
90 public void testMissingVersion() {
91 JSONObject jo = buildFeedRequest();
96 public void testTooLongVersion() {
97 JSONObject jo = buildFeedRequest();
98 jo.put("version", "123456789012345678901234567890");
102 public void testTooLongDescription() {
104 JSONObject jo = buildFeedRequest();
105 jo.put("description", s_257);
109 public void testMissingAuthorization() {
110 JSONObject jo = buildFeedRequest();
111 jo.remove("authorization");
115 public void testMissingClassification() {
116 JSONObject jo = buildFeedRequest();
117 JSONObject j2 = jo.getJSONObject("authorization");
118 j2.remove("classification");
122 public void testTooLongClassification() {
123 JSONObject jo = buildFeedRequest();
124 JSONObject j2 = jo.getJSONObject("authorization");
125 j2.put("classification", s_33);
129 public void testNoEndpointIds() {
130 JSONObject jo = buildFeedRequest();
131 JSONObject j2 = jo.getJSONObject("authorization");
132 j2.put("endpoint_ids", new JSONArray());
136 public void testBadIPAddress1() {
137 JSONObject jo = buildFeedRequest();
138 JSONObject j2 = jo.getJSONObject("authorization");
139 JSONArray ja = j2.getJSONArray("endpoint_addrs");
140 ja.put("ZZZ^&#$%@#&^%$@#&^");
144 public void testBadIPAddress2() {
145 JSONObject jo = buildFeedRequest();
146 JSONObject j2 = jo.getJSONObject("authorization");
147 JSONArray ja = j2.getJSONArray("endpoint_addrs");
148 ja.put("135.207.136.678"); // bad IPv4 addr
152 public void testBadIPAddress3() {
153 JSONObject jo = buildFeedRequest();
154 JSONObject j2 = jo.getJSONObject("authorization");
155 JSONArray ja = j2.getJSONArray("endpoint_addrs");
156 ja.put("2001:1890:1110:d000:1a29::17567"); // bad IPv6 addr
160 public void testBadNetMask() {
161 JSONObject jo = buildFeedRequest();
162 JSONObject j2 = jo.getJSONObject("authorization");
163 JSONArray ja = j2.getJSONArray("endpoint_addrs");
164 ja.put("10.10.10.10/64");
168 public void testGoodIPAddress1() {
169 JSONObject jo = buildFeedRequest();
170 JSONObject j2 = jo.getJSONObject("authorization");
171 JSONArray ja = j2.getJSONArray("endpoint_addrs");
172 ja.put("135.207.136.175"); // good IPv4 addr
176 public void testGoodIPAddress2() {
177 JSONObject jo = buildFeedRequest();
178 JSONObject j2 = jo.getJSONObject("authorization");
179 JSONArray ja = j2.getJSONArray("endpoint_addrs");
180 ja.put("2001:1890:1110:d000:1a29::175"); // good IPv6 addr
184 public void testGoodNetMask() {
185 JSONObject jo = buildFeedRequest();
186 JSONObject j2 = jo.getJSONObject("authorization");
187 JSONArray ja = j2.getJSONArray("endpoint_addrs");
188 ja.put("2001:1890:1110:d000:1a29::175/120");
191 private void testCommon(JSONObject jo, int expect) {
192 testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
194 private void testCommon(JSONObject jo, int expect, String ctype, String bhdr) {
195 String url = props.getProperty("test.host") + "/";
196 HttpPost httpPost = new HttpPost(url);
199 httpPost.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
200 String t = jo.toString();
201 HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create(ctype));
202 httpPost.setEntity(body);
204 HttpResponse response = httpclient.execute(httpPost);
205 ckResponse(response, expect);
207 HttpEntity entity = response.getEntity();
208 ctype = entity.getContentType().getValue().trim();
209 int code = response.getStatusLine().getStatusCode();
210 if (code == HttpServletResponse.SC_CREATED && !ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE))
211 fail("Got wrong content type: "+ctype);
213 if (code == HttpServletResponse.SC_CREATED) {
214 Header[] loc = response.getHeaders("Location");
216 fail("Missing Location header.");
219 // do something useful with the response body and ensure it is fully consumed
220 if (ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
221 // ck Location header!
222 JSONObject jo2 = null;
224 jo2 = new JSONObject(new JSONTokener(entity.getContent()));
225 System.err.println(jo2.toString());
226 } catch (Exception e) {
227 fail("Bad JSON: "+e.getMessage());
230 jo2.getString("publisher");
231 JSONObject jo3 = jo2.getJSONObject("links");
232 jo3.getString("self");
233 jo3.getString("publish");
234 jo3.getString("subscribe");
235 jo3.getString("log");
236 } catch (JSONException e) {
237 fail("required field missing from result: "+e.getMessage());
240 EntityUtils.consume(entity);
242 } catch (IOException e) {
243 fail(e.getMessage());
245 httpPost.releaseConnection();
248 private JSONObject buildFeedRequest() {
249 JSONObject jo = new JSONObject();
250 jo.put("name", "JunitFeed");
251 jo.put("version", ""+System.currentTimeMillis()); // make version unique
252 jo.put("description", "Sample feed used by JUnit to test");
254 JSONObject jo2 = new JSONObject();
255 jo2.put("classification", "unrestricted");
257 JSONArray ja = new JSONArray();
258 JSONObject jo3 = new JSONObject();
259 jo3.put("id", "id001");
260 jo3.put("password", "re1kwelj");
261 JSONObject jo4 = new JSONObject();
262 jo4.put("id", "id002");
263 jo4.put("password", "o9eqlmbd");
266 jo2.put("endpoint_ids", ja);
268 ja = new JSONArray();
270 ja.put("192.168.0.1");
271 ja.put("135.207.136.128/25");
272 jo2.put("endpoint_addrs", ja);
274 jo.put("authorization", jo2);
279 curl -v -X POST -H 'X-ATT-DR-ON-BEHALF-OF: tester' -H 'Content-type: application/vnd.att-dr.feed' --user publisher:tomcat \
280 --data "$data" http://127.0.0.1:8080/prov/feed/