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