Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testSubscribePost.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.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.ByteArrayEntity;
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.util.EntityUtils;
38 import org.json.JSONArray;
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import org.json.JSONTokener;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
47 import org.onap.dmaap.datarouter.provisioning.SubscribeServlet;
48
49 public class testSubscribePost extends testBase {
50         private int feednum = 0;
51
52         @BeforeClass
53         public static void setUpBeforeClass() throws Exception {
54         }
55
56         @AfterClass
57         public static void tearDownAfterClass() throws Exception {
58         }
59
60         @Before
61         public void setUp() throws Exception {
62                 super.setUp();
63                 getDBstate();
64                 // use the first feed to subscribe to
65                 JSONArray ja = db_state.getJSONArray("feeds");
66                 for (int i = 0; i < ja.length(); i++) {
67                         JSONObject feed0 = ja.getJSONObject(i);
68                         if (feed0 != null && !feed0.getBoolean("deleted")) {
69                                 feednum = feed0.getInt("feedid");
70                                 return;
71                         }
72                 }
73         }
74
75         @Test
76         public void testNormal() {
77                 JSONObject jo = buildSubRequest();
78                 testCommon(jo, HttpServletResponse.SC_CREATED);
79         }
80         @Test
81         public void testMissingUrl() {
82                 JSONObject jo = buildSubRequest();
83                 jo.getJSONObject("delivery").remove("url");
84                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
85         }
86         @Test
87         public void testTooLongUrl() {
88                 JSONObject jo = buildSubRequest();
89                 jo.getJSONObject("delivery").put("url", "https://"+s_257);
90                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
91         }
92         @Test
93         public void testMissingUser() {
94                 JSONObject jo = buildSubRequest();
95                 jo.getJSONObject("delivery").remove("user");
96                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
97         }
98         @Test
99         public void testTooLongUser() {
100                 JSONObject jo = buildSubRequest();
101                 jo.getJSONObject("delivery").put("user", s_33);
102                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
103         }
104         @Test
105         public void testMissingPassword() {
106                 JSONObject jo = buildSubRequest();
107                 jo.getJSONObject("delivery").remove("password");
108                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
109         }
110         @Test
111         public void testTooLongPassword() {
112                 JSONObject jo = buildSubRequest();
113                 jo.getJSONObject("delivery").put("password", s_33);
114                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
115         }
116         @Test
117         public void testNonBooleanMetadata() {
118                 JSONObject jo = buildSubRequest();
119                 jo.put("metadataOnly", s_33);
120                 testCommon(jo, HttpServletResponse.SC_BAD_REQUEST);
121         }
122         private void testCommon(JSONObject jo, int expect) {
123                 String url   = props.getProperty("test.host") + "/subscribe/" + feednum;
124                 HttpPost httpPost = new HttpPost(url);
125                 try {
126                         httpPost.addHeader(SubscribeServlet.BEHALF_HEADER, "JUnit");
127                         String t = jo.toString();
128                         HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create(SubscribeServlet.SUB_CONTENT_TYPE));
129                         httpPost.setEntity(body);
130
131                         HttpResponse response = httpclient.execute(httpPost);
132                     ckResponse(response, expect);
133
134                         HttpEntity entity = response.getEntity();
135                         String ctype = entity.getContentType().getValue();
136                         int code = response.getStatusLine().getStatusCode();
137                         if (code == HttpServletResponse.SC_CREATED && !ctype.equals(SubscribeServlet.SUBFULL_CONTENT_TYPE))
138                                 fail("Got wrong content type: "+ctype);
139
140                         // do something useful with the response body and ensure it is fully consumed
141                         if (ctype.equals(FeedServlet.SUBFULL_CONTENT_TYPE)) {
142                                 JSONObject jo2 = null;
143                                 try {
144                                         jo2 = new JSONObject(new JSONTokener(entity.getContent()));
145                                 } catch (Exception e) {
146                                         fail("Bad JSON: "+e.getMessage());
147                                 }
148                                 try {
149                                         jo2.getString("subscriber");
150                                         JSONObject jo3 = jo2.getJSONObject("links");
151                                         jo3.getString("self");
152                                         jo3.getString("feed");
153                                         jo3.getString("log");
154                                 } catch (JSONException e) {
155                                         fail("required field missing from result: "+e.getMessage());
156                                 }
157                         } else {
158                                 EntityUtils.consume(entity);
159                         }
160                 } catch (IOException e) {
161                         fail(e.getMessage());
162                 } finally {
163                         httpPost.releaseConnection();
164                 }
165         }
166         private JSONObject buildSubRequest() {
167                 JSONObject jo = new JSONObject();
168
169                         JSONObject jo2 = new JSONObject();
170                         jo2.put("url", "https://www.att.com/");
171                         jo2.put("user", "dmr");
172                         jo2.put("password", "passw0rd");
173                         jo2.put("use100", true);
174
175                 jo.put("delivery", jo2);
176                 jo.put("metadataOnly", Boolean.FALSE);
177                 return jo;
178         }
179 }