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;
44 import com.att.research.datarouter.provisioning.FeedServlet;
46 public class testDRFeedsGet extends testBase {
47 private JSONArray returnedlist;
50 public static void setUpBeforeClass() throws Exception {
54 public static void tearDownAfterClass() throws Exception {
58 public void setUp() throws Exception {
64 public void testNormal() {
65 testCommon(HttpServletResponse.SC_OK);
67 JSONArray ja = db_state.getJSONArray("feeds");
68 for (int i = 0; i < ja.length(); i++) {
69 JSONObject jo = ja.getJSONObject(i);
70 if (!jo.getBoolean("deleted"))
73 if (returnedlist.length() != expect)
74 fail("bad length, got "+ returnedlist.length() + " expect " + expect);
77 public void testNormalGoodName() {
78 JSONArray ja = db_state.getJSONArray("feeds");
79 JSONObject feed0 = ja.getJSONObject(0);
80 String name = feed0.getString("name");
81 String query = "?name=" + name;
83 for (int n = 0; n < ja.length(); n++) {
84 JSONObject jo = ja.getJSONObject(n);
85 if (!jo.getBoolean("deleted") && jo.getString("name").equals(name))
88 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
89 if (returnedlist.length() != expect)
90 fail("bad length, got "+ returnedlist.length() + " expect "+expect);
93 public void testNormalBadName() {
94 String query = "?name=ZZTOP123456";
95 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
96 if (returnedlist.length() != 0)
97 fail("bad length, got "+ returnedlist.length() + " expect 0");
100 public void testNormalBadPath() {
101 String query = "flarg/?publisher=JUnit";
102 testCommon(HttpServletResponse.SC_NOT_FOUND, query, "text/html;charset=ISO-8859-1", "JUnit");
105 public void testNormalGoodPublisher() {
106 JSONArray ja = db_state.getJSONArray("feeds");
107 JSONObject feed0 = ja.getJSONObject(0);
108 String query = "?publisher=" + feed0.getString("publisher");
109 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
111 for (int i = 0; i < ja.length(); i++) {
112 JSONObject jo = ja.getJSONObject(i);
113 if (jo.getString("publisher").equals(feed0.getString("publisher")) && !jo.getBoolean("deleted"))
116 if (returnedlist.length() != expect)
117 fail("bad length, got "+returnedlist.length()+" expected "+expect);
120 public void testNormalBadPublisher() {
121 String query = "?publisher=ZZTOP123456";
122 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
123 if (returnedlist.length() != 0)
127 public void testNormalGoodSubscriber() {
128 JSONArray ja = db_state.getJSONArray("subscriptions");
129 if (ja.length() > 0) {
130 JSONObject sub0 = ja.getJSONObject(0);
131 String query = "?subscriber=" + sub0.getString("subscriber");
132 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
133 // aarg! - this is complicated!
135 // for (int i = 0; i < ja.length(); i++) {
136 // JSONObject jo = ja.getJSONObject(i);
137 // if (jo.getString("subscriber").equals(sub0.getString("subscriber")))
140 // if (returnedlist.length() != 1)
141 // fail("bad length "+returnedlist.toString());
143 // There are no subscriptions yet, so use a made up name
144 testCommon(HttpServletResponse.SC_OK, "?subscriber=foo", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
148 public void testNormalBadSubscriber() {
149 String query = "?subscriber=ZZTOP123456";
150 testCommon(HttpServletResponse.SC_OK, query, FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
151 if (returnedlist.length() != 0)
154 private void testCommon(int expect) {
155 testCommon(expect, "", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
157 private void testCommon(int expect, String query, String ectype, String bhdr) {
158 String url = props.getProperty("test.host") + "/" + query;
159 HttpGet httpGet = new HttpGet(url);
162 httpGet.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
164 HttpResponse response = httpclient.execute(httpGet);
165 ckResponse(response, expect);
167 HttpEntity entity = response.getEntity();
168 String ctype = entity.getContentType().getValue().trim();
169 if (!ctype.equals(ectype))
170 fail("Got wrong content type: "+ctype);
172 // do something useful with the response body and ensure it is fully consumed
173 if (ctype.equals(FeedServlet.FEEDLIST_CONTENT_TYPE)) {
175 returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
176 } catch (Exception e) {
177 fail("Bad JSON: "+e.getMessage());
180 EntityUtils.consume(entity);
182 } catch (IOException e) {
183 fail(e.getMessage());
185 httpGet.releaseConnection();