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