Replace ATT headers
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / IntegrationTestDrFeedsPost.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package datarouter.provisioning;
25
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29
30 import javax.servlet.http.HttpServletResponse;
31
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;
47
48 public class IntegrationTestDrFeedsPost extends IntegrationTestBase {
49     @BeforeClass
50     public static void setUpBeforeClass() throws Exception {
51     }
52
53     @AfterClass
54     public static void tearDownAfterClass() throws Exception {
55     }
56
57     @Test
58     public void testNormal() {
59         JSONObject jo = buildFeedRequest();
60         testCommon(jo, HttpServletResponse.SC_CREATED);
61     }
62
63     @Test
64     public void testNormalNoCtVersion() {
65         JSONObject jo = buildFeedRequest();
66         testCommon(jo, HttpServletResponse.SC_CREATED, "application/vnd.dmaap-dr.feed", "JUnit");
67     }
68
69     @Test
70     public void testBadContentType() {
71         JSONObject jo = buildFeedRequest();
72         testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "Junit");
73     }
74
75     @Test
76     public void testNoBehalfHeader() {
77         JSONObject jo = buildFeedRequest();
78         testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, null);
79     }
80
81     @Test
82     public void testMissingName() {
83         JSONObject jo = buildFeedRequest();
84         jo.remove("name");
85         testCommon(jo, 400);
86     }
87
88     @Test
89     public void testTooLongName() {
90         JSONObject jo = buildFeedRequest();
91         jo.put("name", "123456789012345678901234567890");
92         testCommon(jo, 400);
93     }
94
95     @Test
96     public void testMissingVersion() {
97         JSONObject jo = buildFeedRequest();
98         jo.remove("version");
99         testCommon(jo, 400);
100     }
101
102     @Test
103     public void testTooLongVersion() {
104         JSONObject jo = buildFeedRequest();
105         jo.put("version", "123456789012345678901234567890");
106         testCommon(jo, 400);
107     }
108
109     @Test
110     public void testTooLongDescription() {
111         // normal request
112         JSONObject jo = buildFeedRequest();
113         jo.put("description", s257);
114         testCommon(jo, 400);
115     }
116
117     @Test
118     public void testMissingAuthorization() {
119         JSONObject jo = buildFeedRequest();
120         jo.remove("authorization");
121         testCommon(jo, 400);
122     }
123
124     @Test
125     public void testMissingClassification() {
126         JSONObject jo = buildFeedRequest();
127         JSONObject j2 = jo.getJSONObject("authorization");
128         j2.remove("classification");
129         testCommon(jo, 400);
130     }
131
132     @Test
133     public void testTooLongClassification() {
134         JSONObject jo = buildFeedRequest();
135         JSONObject j2 = jo.getJSONObject("authorization");
136         j2.put("classification", s33);
137         testCommon(jo, 400);
138     }
139
140     @Test
141     public void testNoEndpointIds() {
142         JSONObject jo = buildFeedRequest();
143         JSONObject j2 = jo.getJSONObject("authorization");
144         j2.put("endpoint_ids", new JSONArray());
145         testCommon(jo, 400);
146     }
147
148     @Test
149     public void testBadIpAddress1() {
150         JSONObject jo = buildFeedRequest();
151         JSONObject j2 = jo.getJSONObject("authorization");
152         JSONArray ja = j2.getJSONArray("endpoint_addrs");
153         ja.put("ZZZ^&#$%@#&^%$@#&^");
154         testCommon(jo, 400);
155     }
156
157     @Test
158     public void testBadIpAddress2() {
159         JSONObject jo = buildFeedRequest();
160         JSONObject j2 = jo.getJSONObject("authorization");
161         JSONArray ja = j2.getJSONArray("endpoint_addrs");
162         ja.put("135.207.136.678");  // bad IPv4 addr
163         testCommon(jo, 400);
164     }
165
166     @Test
167     public void testBadIpAddress3() {
168         JSONObject jo = buildFeedRequest();
169         JSONObject j2 = jo.getJSONObject("authorization");
170         JSONArray ja = j2.getJSONArray("endpoint_addrs");
171         ja.put("2001:1890:1110:d000:1a29::17567");  // bad IPv6 addr
172         testCommon(jo, 400);
173     }
174
175     @Test
176     public void testBadNetMask() {
177         JSONObject jo = buildFeedRequest();
178         JSONObject j2 = jo.getJSONObject("authorization");
179         JSONArray ja = j2.getJSONArray("endpoint_addrs");
180         ja.put("10.10.10.10/64");
181         testCommon(jo, 400);
182     }
183
184     @Test
185     public void testGoodIpAddress1() {
186         JSONObject jo = buildFeedRequest();
187         JSONObject j2 = jo.getJSONObject("authorization");
188         JSONArray ja = j2.getJSONArray("endpoint_addrs");
189         ja.put("135.207.136.175"); // good IPv4 addr
190         testCommon(jo, 201);
191     }
192
193     @Test
194     public void testGoodIpAddress2() {
195         JSONObject jo = buildFeedRequest();
196         JSONObject j2 = jo.getJSONObject("authorization");
197         JSONArray ja = j2.getJSONArray("endpoint_addrs");
198         ja.put("2001:1890:1110:d000:1a29::175"); // good IPv6 addr
199         testCommon(jo, 201);
200     }
201
202     @Test
203     public void testGoodNetMask() {
204         JSONObject jo = buildFeedRequest();
205         JSONObject j2 = jo.getJSONObject("authorization");
206         JSONArray ja = j2.getJSONArray("endpoint_addrs");
207         ja.put("2001:1890:1110:d000:1a29::175/120");
208         testCommon(jo, 201);
209     }
210
211     private void testCommon(JSONObject jo, int expect) {
212         testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
213     }
214
215     private void testCommon(JSONObject jo, int expect, String ctype, String bhdr) {
216         String url   = props.getProperty("test.host") + "/";
217         HttpPost httpPost = new HttpPost(url);
218         try {
219             if (bhdr != null) {
220                 httpPost.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
221             }
222             String strJo = jo.toString();
223             HttpEntity body = new ByteArrayEntity(strJo.getBytes(), ContentType.create(ctype));
224             httpPost.setEntity(body);
225
226             HttpResponse response = httpclient.execute(httpPost);
227             ckResponse(response, expect);
228
229             HttpEntity entity = response.getEntity();
230             ctype = entity.getContentType().getValue().trim();
231             int code = response.getStatusLine().getStatusCode();
232             if (code == HttpServletResponse.SC_CREATED && !ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
233                 fail("Got wrong content type: " + ctype);
234             }
235
236             if (code == HttpServletResponse.SC_CREATED) {
237                 Header[] loc = response.getHeaders("Location");
238                 if (loc == null) {
239                     fail("Missing Location header.");
240                 }
241             }
242
243             // do something useful with the response body and ensure it is fully consumed
244             if (ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
245                 // ck Location header!
246                 JSONObject jo2 = null;
247                 try {
248                     jo2 = new JSONObject(new JSONTokener(entity.getContent()));
249                     System.err.println(jo2.toString());
250                 } catch (Exception e) {
251                     fail("Bad JSON: " + e.getMessage());
252                 }
253                 try {
254                     jo2.getString("publisher");
255                     JSONObject jo3 = jo2.getJSONObject("links");
256                     jo3.getString("self");
257                     jo3.getString("publish");
258                     jo3.getString("subscribe");
259                     jo3.getString("log");
260                 } catch (JSONException e) {
261                     fail("required field missing from result: " + e.getMessage());
262                 }
263             } else {
264                 EntityUtils.consume(entity);
265             }
266         } catch (IOException e) {
267             fail(e.getMessage());
268         } finally {
269             httpPost.releaseConnection();
270         }
271     }
272
273     private JSONObject buildFeedRequest() {
274         JSONObject jo = new JSONObject();
275         jo.put("name", "JunitFeed");
276         jo.put("version", "" + System.currentTimeMillis());  // make version unique
277         jo.put("description", "Sample feed used by JUnit to test");
278
279         JSONObject jo2 = new JSONObject();
280         jo2.put("classification", "unrestricted");
281
282         JSONObject jo3 = new JSONObject();
283         jo3.put("id", "id001");
284         jo3.put("password", "re1kwelj");
285         JSONObject jo4 = new JSONObject();
286         jo4.put("id", "id002");
287         jo4.put("password", "o9eqlmbd");
288
289         JSONArray ja = new JSONArray();
290         ja.put(jo3);
291         ja.put(jo4);
292         jo2.put("endpoint_ids", ja);
293
294         ja = new JSONArray();
295         ja.put("10.0.0.1");
296         ja.put("192.168.0.1");
297         ja.put("135.207.136.128/25");
298         jo2.put("endpoint_addrs", ja);
299
300         jo.put("authorization", jo2);
301         return jo;
302     }
303 }
304 /*
305 curl -v -X POST -H 'X-DMAAP-DR-ON-BEHALF-OF: tester' -H 'Content-type: application/vnd.dmaap-dr.feed' \
306     --user publisher:tomcat \
307     --data "$data" http://127.0.0.1:8080/prov/feed/
308 */