Update project structure to org.onap
[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 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
44
45 public class testDRFeedsGet extends testBase {
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                 if (returnedlist.length() != expect)
73                         fail("bad length, got "+ returnedlist.length() + " expect " + expect);
74         }
75         @Test
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;
81                 int expect = 0;
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))
85                                 expect++;
86                 }
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);
90         }
91         @Test
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");
97         }
98         @Test
99         public void testNormalBadPath() {
100                 String query = "flarg/?publisher=JUnit";
101                 testCommon(HttpServletResponse.SC_NOT_FOUND, query, "text/html;charset=ISO-8859-1", "JUnit");
102         }
103         @Test
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");
109                 int expect = 0;
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"))
113                                 expect++;
114                 }
115                 if (returnedlist.length() != expect)
116                         fail("bad length, got "+returnedlist.length()+" expected "+expect);
117         }
118         @Test
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)
123                         fail("bad length");
124         }
125         @Test
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!
133 //              int expect = 0;
134 //              for (int i = 0; i < ja.length(); i++) {
135 //                      JSONObject jo = ja.getJSONObject(i);
136 //                      if (jo.getString("subscriber").equals(sub0.getString("subscriber")))
137 //                              expect++;
138 //              }
139 //              if (returnedlist.length() != 1)
140 //                      fail("bad length "+returnedlist.toString());
141                 } else {
142                         // There are no subscriptions yet, so use a made up name
143                         testCommon(HttpServletResponse.SC_OK, "?subscriber=foo", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
144                 }
145         }
146         @Test
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)
151                         fail("bad length");
152         }
153         private void testCommon(int expect) {
154                 testCommon(expect, "", FeedServlet.FEEDLIST_CONTENT_TYPE, "JUnit");
155         }
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);
159                 try {
160                         if (bhdr != null)
161                                 httpGet.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
162
163                         HttpResponse response = httpclient.execute(httpGet);
164                     ckResponse(response, expect);
165
166                         HttpEntity entity = response.getEntity();
167                         String ctype = entity.getContentType().getValue().trim();
168                         if (!ctype.equals(ectype))
169                                 fail("Got wrong content type: "+ctype);
170
171                         // do something useful with the response body and ensure it is fully consumed
172                         if (ctype.equals(FeedServlet.FEEDLIST_CONTENT_TYPE)) {
173                                 try {
174                                         returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
175                                 } catch (Exception e) {
176                                         fail("Bad JSON: "+e.getMessage());
177                                 }
178                         } else {
179                                 EntityUtils.consume(entity);
180                         }
181                 } catch (IOException e) {
182                         fail(e.getMessage());
183                 } finally {
184                         httpGet.releaseConnection();
185                 }
186         }
187 }