[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testDRFeedsGet.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 javax.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
44 import com.att.research.datarouter.provisioning.FeedServlet;
45
46 public class testDRFeedsGet extends testBase {
47         private JSONArray returnedlist;
48
49         @BeforeClass
50         public static void setUpBeforeClass() throws Exception {
51         }
52
53         @AfterClass
54         public static void tearDownAfterClass() throws Exception {
55         }
56
57         @Before
58         public void setUp() throws Exception {
59                 super.setUp();
60                 getDBstate();
61         }
62
63         @Test
64         public void testNormal() {
65                 testCommon(HttpServletResponse.SC_OK);
66                 int expect = 0;
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"))
71                                 expect++;
72                 }
73                 if (returnedlist.length() != expect)
74                         fail("bad length, got "+ returnedlist.length() + " expect " + expect);
75         }
76         @Test
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;
82                 int expect = 0;
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))
86                                 expect++;
87                 }
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);
91         }
92         @Test
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");
98         }
99         @Test
100         public void testNormalBadPath() {
101                 String query = "flarg/?publisher=JUnit";
102                 testCommon(HttpServletResponse.SC_NOT_FOUND, query, "text/html;charset=ISO-8859-1", "JUnit");
103         }
104         @Test
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");
110                 int expect = 0;
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"))
114                                 expect++;
115                 }
116                 if (returnedlist.length() != expect)
117                         fail("bad length, got "+returnedlist.length()+" expected "+expect);
118         }
119         @Test
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)
124                         fail("bad length");
125         }
126         @Test
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!
134 //              int expect = 0;
135 //              for (int i = 0; i < ja.length(); i++) {
136 //                      JSONObject jo = ja.getJSONObject(i);
137 //                      if (jo.getString("subscriber").equals(sub0.getString("subscriber")))
138 //                              expect++;
139 //              }
140 //              if (returnedlist.length() != 1)
141 //                      fail("bad length "+returnedlist.toString());
142                 } else {
143                         // There are no subscriptions yet, so use a made up name
144                         testCommon(HttpServletResponse.SC_OK, "?subscriber=foo", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
145                 }
146         }
147         @Test
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)
152                         fail("bad length");
153         }
154         private void testCommon(int expect) {
155                 testCommon(expect, "", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
156         }
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);
160                 try {
161                         if (bhdr != null)
162                                 httpGet.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
163
164                         HttpResponse response = httpclient.execute(httpGet);
165                     ckResponse(response, expect);
166
167                         HttpEntity entity = response.getEntity();
168                         String ctype = entity.getContentType().getValue().trim();
169                         if (!ctype.equals(ectype))
170                                 fail("Got wrong content type: "+ctype);
171
172                         // do something useful with the response body and ensure it is fully consumed
173                         if (ctype.equals(FeedServlet.FEEDLIST_CONTENT_TYPE)) {
174                                 try {
175                                         returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
176                                 } catch (Exception e) {
177                                         fail("Bad JSON: "+e.getMessage());
178                                 }
179                         } else {
180                                 EntityUtils.consume(entity);
181                         }
182                 } catch (IOException e) {
183                         fail(e.getMessage());
184                 } finally {
185                         httpGet.releaseConnection();
186                 }
187         }
188 }