[DMAAP-DR] Remove AAF/TLS phase 1
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / IntegrationTestDrFeedsGet.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.HttpGet;
35 import org.apache.http.util.EntityUtils;
36 import org.json.JSONArray;
37 import org.json.JSONObject;
38 import org.json.JSONTokener;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
44
45 public class IntegrationTestDrFeedsGet extends IntegrationTestBase {
46     private JSONArray returnedlist;
47
48     @BeforeClass
49     public static void setUpBeforeClass() throws Exception {
50     }
51
52     @AfterClass
53     public static void tearDownAfterClass() throws Exception {
54     }
55
56     @Before
57     public void setUp() throws Exception {
58         super.setUp();
59         getDBstate();
60     }
61
62     @Test
63     public void testNormal() {
64         testCommon(HttpServletResponse.SC_OK);
65         int expect = 0;
66         JSONArray ja = db_state.getJSONArray("feeds");
67         for (int i = 0; i < ja.length(); i++) {
68             JSONObject jo = ja.getJSONObject(i);
69             if (!jo.getBoolean("deleted")) {
70                 expect++;
71             }
72         }
73         if (returnedlist.length() != expect) {
74             fail("bad length, got " + returnedlist.length() + " expect " + expect);
75         }
76     }
77
78     @Test
79     public void testNormalGoodName() {
80         JSONArray ja = db_state.getJSONArray("feeds");
81         JSONObject feed0 = ja.getJSONObject(0);
82         String name = feed0.getString("name");
83         String query = "?name=" + name;
84         int expect = 0;
85         for (int n = 0; n < ja.length(); n++) {
86             JSONObject jo = ja.getJSONObject(n);
87             if (!jo.getBoolean("deleted") && jo.getString("name").equals(name)) {
88                 expect++;
89             }
90         }
91         testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
92         if (returnedlist.length() != expect) {
93             fail("bad length, got " + returnedlist.length() + " expect " + expect);
94         }
95     }
96
97     @Test
98     public void testNormalBadName() {
99         String query = "?name=ZZTOP123456";
100         testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
101         if (returnedlist.length() != 0) {
102             fail("bad length, got " + returnedlist.length() + " expect 0");
103         }
104     }
105
106     @Test
107     public void testNormalBadPath() {
108         String query = "flarg/?publisher=JUnit";
109         testCommon(HttpServletResponse.SC_NOT_FOUND, query, "text/html;charset=ISO-8859-1", "JUnit");
110     }
111
112     @Test
113     public void testNormalGoodPublisher() {
114         JSONArray ja = db_state.getJSONArray("feeds");
115         JSONObject feed0 = ja.getJSONObject(0);
116         String query = "?publisher=" + feed0.getString("publisher");
117         testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
118         int expect = 0;
119         for (int i = 0; i < ja.length(); i++) {
120             JSONObject jo = ja.getJSONObject(i);
121             if (jo.getString("publisher").equals(feed0.getString("publisher")) && !jo.getBoolean("deleted")) {
122                 expect++;
123             }
124         }
125         if (returnedlist.length() != expect) {
126             fail("bad length, got " + returnedlist.length() + " expected " + expect);
127         }
128     }
129
130     @Test
131     public void testNormalBadPublisher() {
132         String query = "?publisher=ZZTOP123456";
133         testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
134         if (returnedlist.length() != 0) {
135             fail("bad length");
136         }
137     }
138
139     @Test
140     public void testNormalGoodSubscriber() {
141         JSONArray ja = db_state.getJSONArray("subscriptions");
142         if (ja.length() > 0) {
143             JSONObject sub0 = ja.getJSONObject(0);
144             String query = "?subscriber=" + sub0.getString("subscriber");
145             testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
146 // aarg! - this is complicated!
147 //        int expect = 0;
148 //        for (int i = 0; i < ja.length(); i++) {
149 //            JSONObject jo = ja.getJSONObject(i);
150 //            if (jo.getString("subscriber").equals(sub0.getString("subscriber")))
151 //                expect++;
152 //        }
153 //        if (returnedlist.length() != 1)
154 //            fail("bad length " + returnedlist.toString());
155         } else {
156             // There are no subscriptions yet, so use a made up name
157             testCommon(HttpServletResponse.SC_OK, "?subscriber=foo", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
158         }
159     }
160
161     @Test
162     public void testNormalBadSubscriber() {
163         String query = "?subscriber=ZZTOP123456";
164         testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
165         if (returnedlist.length() != 0) {
166             fail("bad length");
167         }
168     }
169
170     private void testCommon(int expect) {
171         testCommon(expect, "", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
172     }
173
174     private void testCommon(int expect, String query, String ectype, String bhdr) {
175         String url = props.getProperty("test.host") + "/" + query;
176         HttpGet httpGet = new HttpGet(url);
177         try {
178             if (bhdr != null) {
179                 httpGet.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
180             }
181
182             HttpResponse response = httpclient.execute(httpGet);
183             ckResponse(response, expect);
184
185             HttpEntity entity = response.getEntity();
186             String ctype = entity.getContentType().getValue().trim();
187             if (!ctype.equals(ectype)) {
188                 fail("Got wrong content type: " + ctype);
189             }
190
191             // do something useful with the response body and ensure it is fully consumed
192             if (ctype.equals(FeedServlet.FEEDLIST_CONTENT_TYPE)) {
193                 try {
194                     returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
195                 } catch (Exception e) {
196                     fail("Bad JSON: " + e.getMessage());
197                 }
198             } else {
199                 EntityUtils.consume(entity);
200             }
201         } catch (IOException e) {
202             fail(e.getMessage());
203         } finally {
204             httpGet.releaseConnection();
205         }
206     }
207 }