1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package datarouter.provisioning;
26 import static org.junit.Assert.fail;
28 import java.io.IOException;
30 import javax.servlet.http.HttpServletResponse;
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;
45 public class IntegrationTestDrFeedsGet extends IntegrationTestBase {
46 private JSONArray returnedlist;
49 public static void setUpBeforeClass() throws Exception {
53 public static void tearDownAfterClass() throws Exception {
57 public void setUp() throws Exception {
63 public void testNormal() {
64 testCommon(HttpServletResponse.SC_OK);
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")) {
73 if (returnedlist.length() != expect) {
74 fail("bad length, got " + returnedlist.length() + " expect " + expect);
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;
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)) {
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);
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");
107 public void testNormalBadPath() {
108 String query = "flarg/?publisher=JUnit";
109 testCommon(HttpServletResponse.SC_NOT_FOUND, query, "text/html;charset=ISO-8859-1", "JUnit");
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");
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")) {
125 if (returnedlist.length() != expect) {
126 fail("bad length, got " + returnedlist.length() + " expected " + expect);
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) {
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!
148 // for (int i = 0; i < ja.length(); i++) {
149 // JSONObject jo = ja.getJSONObject(i);
150 // if (jo.getString("subscriber").equals(sub0.getString("subscriber")))
153 // if (returnedlist.length() != 1)
154 // fail("bad length " + returnedlist.toString());
156 // There are no subscriptions yet, so use a made up name
157 testCommon(HttpServletResponse.SC_OK, "?subscriber=foo", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
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) {
170 private void testCommon(int expect) {
171 testCommon(expect, "", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
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);
179 httpGet.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
182 HttpResponse response = httpclient.execute(httpGet);
183 ckResponse(response, expect);
185 HttpEntity entity = response.getEntity();
186 String ctype = entity.getContentType().getValue().trim();
187 if (!ctype.equals(ectype)) {
188 fail("Got wrong content type: " + ctype);
191 // do something useful with the response body and ensure it is fully consumed
192 if (ctype.equals(FeedServlet.FEEDLIST_CONTENT_TYPE)) {
194 returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
195 } catch (Exception e) {
196 fail("Bad JSON: " + e.getMessage());
199 EntityUtils.consume(entity);
201 } catch (IOException e) {
202 fail(e.getMessage());
204 httpGet.releaseConnection();