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