65d041d128d4d9e438ec5881f643abfa02e7cda0
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testDRFeedsPost.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.*;
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
47 import com.att.research.datarouter.provisioning.FeedServlet;
48
49 public class testDRFeedsPost extends testBase {
50         @BeforeClass
51         public static void setUpBeforeClass() throws Exception {
52         }
53
54         @AfterClass
55         public static void tearDownAfterClass() throws Exception {
56         }
57
58         @Test
59         public void testNormal() {
60                 JSONObject jo = buildFeedRequest();
61                 testCommon(jo, HttpServletResponse.SC_CREATED);
62         }
63         @Test
64         public void testNormalNoCTVersion() {
65                 JSONObject jo = buildFeedRequest();
66                 testCommon(jo, HttpServletResponse.SC_CREATED, "application/vnd.att-dr.feed", "JUnit");
67         }
68         @Test
69         public void testBadContentType() {
70                 JSONObject jo = buildFeedRequest();
71                 testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "Junit");
72         }
73         @Test
74         public void testNoBehalfHeader() {
75                 JSONObject jo = buildFeedRequest();
76                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, null);
77         }
78         @Test
79         public void testMissingName() {
80                 JSONObject jo = buildFeedRequest();
81                 jo.remove("name");
82                 testCommon(jo, 400);
83         }
84         @Test
85         public void testTooLongName() {
86                 JSONObject jo = buildFeedRequest();
87                 jo.put("name", "123456789012345678901234567890");
88                 testCommon(jo, 400);
89         }
90         @Test
91         public void testMissingVersion() {
92                 JSONObject jo = buildFeedRequest();
93                 jo.remove("version");
94                 testCommon(jo, 400);
95         }
96         @Test
97         public void testTooLongVersion() {
98                 JSONObject jo = buildFeedRequest();
99                 jo.put("version", "123456789012345678901234567890");
100                 testCommon(jo, 400);
101         }
102         @Test
103         public void testTooLongDescription() {
104                 // normal request
105                 JSONObject jo = buildFeedRequest();
106                 jo.put("description", s_257);
107                 testCommon(jo, 400);
108         }
109         @Test
110         public void testMissingAuthorization() {
111                 JSONObject jo = buildFeedRequest();
112                 jo.remove("authorization");
113                 testCommon(jo, 400);
114         }
115         @Test
116         public void testMissingClassification() {
117                 JSONObject jo = buildFeedRequest();
118                 JSONObject j2 = jo.getJSONObject("authorization");
119                 j2.remove("classification");
120                 testCommon(jo, 400);
121         }
122         @Test
123         public void testTooLongClassification() {
124                 JSONObject jo = buildFeedRequest();
125                 JSONObject j2 = jo.getJSONObject("authorization");
126                 j2.put("classification", s_33);
127                 testCommon(jo, 400);
128         }
129         @Test
130         public void testNoEndpointIds() {
131                 JSONObject jo = buildFeedRequest();
132                 JSONObject j2 = jo.getJSONObject("authorization");
133                 j2.put("endpoint_ids", new JSONArray());
134                 testCommon(jo, 400);
135         }
136         @Test
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^&#$%@#&^%$@#&^");
142                 testCommon(jo, 400);
143         }
144         @Test
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
150                 testCommon(jo, 400);
151         }
152         @Test
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
158                 testCommon(jo, 400);
159         }
160         @Test
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");
166                 testCommon(jo, 400);
167         }
168         @Test
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
174                 testCommon(jo, 201);
175         }
176         @Test
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
182                 testCommon(jo, 201);
183         }
184         @Test
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");
190                 testCommon(jo, 201);
191         }
192         private void testCommon(JSONObject jo, int expect) {
193                 testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
194         }
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);
198                 try {
199                         if (bhdr != null)
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);
204
205                         HttpResponse response = httpclient.execute(httpPost);
206                     ckResponse(response, expect);
207
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);
213
214                         if (code == HttpServletResponse.SC_CREATED) {
215                                 Header[] loc = response.getHeaders("Location");
216                                 if (loc == null)
217                                         fail("Missing Location header.");
218                         }
219
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;
224                                 try {
225                                         jo2 = new JSONObject(new JSONTokener(entity.getContent()));
226         System.err.println(jo2.toString());
227                                 } catch (Exception e) {
228                                         fail("Bad JSON: "+e.getMessage());
229                                 }
230                                 try {
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());
239                                 }
240                         } else {
241                                 EntityUtils.consume(entity);
242                         }
243                 } catch (IOException e) {
244                         fail(e.getMessage());
245                 } finally {
246                         httpPost.releaseConnection();
247                 }
248         }
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");
254
255                         JSONObject jo2 = new JSONObject();
256                         jo2.put("classification", "unrestricted");
257
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");
265                                 ja.put(jo3);
266                                 ja.put(jo4);
267                         jo2.put("endpoint_ids", ja);
268
269                         ja = new JSONArray();
270                                 ja.put("10.0.0.1");
271                                 ja.put("192.168.0.1");
272                                 ja.put("135.207.136.128/25");
273                         jo2.put("endpoint_addrs", ja);
274
275                 jo.put("authorization", jo2);
276                 return jo;
277         }
278 }
279 /*
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/
282 */