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 testDRFeedsGet extends testBase {
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"))
72 if (returnedlist.length() != expect)
73 fail("bad length, got "+ returnedlist.length() + " expect " + expect);
76 public void testNormalGoodName() {
77 JSONArray ja = db_state.getJSONArray("feeds");
78 JSONObject feed0 = ja.getJSONObject(0);
79 String name = feed0.getString("name");
80 String query = "?name=" + name;
82 for (int n = 0; n < ja.length(); n++) {
83 JSONObject jo = ja.getJSONObject(n);
84 if (!jo.getBoolean("deleted") && jo.getString("name").equals(name))
87 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
88 if (returnedlist.length() != expect)
89 fail("bad length, got "+ returnedlist.length() + " expect "+expect);
92 public void testNormalBadName() {
93 String query = "?name=ZZTOP123456";
94 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
95 if (returnedlist.length() != 0)
96 fail("bad length, got "+ returnedlist.length() + " expect 0");
99 public void testNormalBadPath() {
100 String query = "flarg/?publisher=JUnit";
101 testCommon(HttpServletResponse.SC_NOT_FOUND, query, "text/html;charset=ISO-8859-1", "JUnit");
104 public void testNormalGoodPublisher() {
105 JSONArray ja = db_state.getJSONArray("feeds");
106 JSONObject feed0 = ja.getJSONObject(0);
107 String query = "?publisher=" + feed0.getString("publisher");
108 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
110 for (int i = 0; i < ja.length(); i++) {
111 JSONObject jo = ja.getJSONObject(i);
112 if (jo.getString("publisher").equals(feed0.getString("publisher")) && !jo.getBoolean("deleted"))
115 if (returnedlist.length() != expect)
116 fail("bad length, got "+returnedlist.length()+" expected "+expect);
119 public void testNormalBadPublisher() {
120 String query = "?publisher=ZZTOP123456";
121 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
122 if (returnedlist.length() != 0)
126 public void testNormalGoodSubscriber() {
127 JSONArray ja = db_state.getJSONArray("subscriptions");
128 if (ja.length() > 0) {
129 JSONObject sub0 = ja.getJSONObject(0);
130 String query = "?subscriber=" + sub0.getString("subscriber");
131 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
132 // aarg! - this is complicated!
134 // for (int i = 0; i < ja.length(); i++) {
135 // JSONObject jo = ja.getJSONObject(i);
136 // if (jo.getString("subscriber").equals(sub0.getString("subscriber")))
139 // if (returnedlist.length() != 1)
140 // fail("bad length "+returnedlist.toString());
142 // There are no subscriptions yet, so use a made up name
143 testCommon(HttpServletResponse.SC_OK, "?subscriber=foo", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
147 public void testNormalBadSubscriber() {
148 String query = "?subscriber=ZZTOP123456";
149 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
150 if (returnedlist.length() != 0)
153 private void testCommon(int expect) {
154 testCommon(expect, "", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
156 private void testCommon(int expect, String query, String ectype, String bhdr) {
157 String url = props.getProperty("test.host") + "/" + query;
158 HttpGet httpGet = new HttpGet(url);
161 httpGet.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
163 HttpResponse response = httpclient.execute(httpGet);
164 ckResponse(response, expect);
166 HttpEntity entity = response.getEntity();
167 String ctype = entity.getContentType().getValue().trim();
168 if (!ctype.equals(ectype))
169 fail("Got wrong content type: "+ctype);
171 // do something useful with the response body and ensure it is fully consumed
172 if (ctype.equals(FeedServlet.FEEDLIST_CONTENT_TYPE)) {
174 returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
175 } catch (Exception e) {
176 fail("Bad JSON: "+e.getMessage());
179 EntityUtils.consume(entity);
181 } catch (IOException e) {
182 fail(e.getMessage());
184 httpGet.releaseConnection();