Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testLogGet.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 package datarouter.provisioning;
24
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.text.SimpleDateFormat;
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.JSONTokener;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
43
44 public class testLogGet extends testBase {
45         private JSONArray returnedlist;
46         private int feedid = 4;
47         private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
48
49         @BeforeClass
50         public static void setUpBeforeClass() throws Exception {
51                 // need to seed the DB here
52         }
53
54         @AfterClass
55         public static void tearDownAfterClass() throws Exception {
56                 // need to "unseed" the DB
57         }
58
59         @Before
60         public void setUp() throws Exception {
61                 super.setUp();
62                 getDBstate();
63 //              JSONArray ja = db_state.getJSONArray("feeds");
64 //              for (int i = 0; i < ja.length(); i++) {
65 //                      JSONObject jo = ja.getJSONObject(i);
66 //                      if (!jo.getBoolean("deleted"))
67 //                              feedid = jo.getInt("feedid");
68 //              }
69         }
70
71         @Test
72         public void testNormal() {
73                 testCommon(HttpServletResponse.SC_OK);
74         }
75         @Test
76         public void testNormalPubOnly() {
77                 testCommon(HttpServletResponse.SC_OK, "?type=pub");
78         }
79         @Test
80         public void testNormalDelOnly() {
81                 testCommon(HttpServletResponse.SC_OK, "?type=del");
82         }
83         @Test
84         public void testNormalExpOnly() {
85                 testCommon(HttpServletResponse.SC_OK, "?type=exp");
86         }
87         @Test
88         public void testNormalXXXOnly() {
89                 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?type=xxx");
90         }
91         @Test
92         public void testNormalStatusSuccess() {
93                 testCommon(HttpServletResponse.SC_OK, "?statusCode=success");
94         }
95         @Test
96         public void testNormalStatusRedirect() {
97                 testCommon(HttpServletResponse.SC_OK, "?statusCode=redirect");
98         }
99         @Test
100         public void testNormalStatusFailure() {
101                 testCommon(HttpServletResponse.SC_OK, "?statusCode=failure");
102         }
103         @Test
104         public void testNormalStatus200() {
105                 testCommon(HttpServletResponse.SC_OK, "?statusCode=200");
106         }
107         @Test
108         public void testNormalStatusXXX() {
109                 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?statusCode=xxx");
110         }
111         @Test
112         public void testNormalExpiryNotRetryable() {
113                 testCommon(HttpServletResponse.SC_OK, "?expiryReason=notRetryable");
114         }
115         @Test
116         public void testNormalExpiryRetriesExhausted() {
117                 testCommon(HttpServletResponse.SC_OK, "?expiryReason=retriesExhausted");
118         }
119         @Test
120         public void testNormalExpiryXXX() {
121                 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?expiryReason=xxx");
122         }
123         @Test
124         public void testNormalPublishId() {
125                 testCommon(HttpServletResponse.SC_OK, "?publishId=1366985877801.mtdvnj00-drtr.proto.research.att.com");
126         }
127         @Test
128         public void testNormalStart() {
129                 long n = System.currentTimeMillis() - (5 * 24 * 60 * 60 * 1000L);       // 5 days
130                 testCommon(HttpServletResponse.SC_OK, String.format("?start=%s", sdf.format(n)));
131         }
132         @Test
133         public void testBadStart() {
134                 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?start=xxx");
135         }
136         @Test
137         public void testLongEnd() {
138                 testCommon(HttpServletResponse.SC_OK, "?end=1364837896220");
139         }
140         @Test
141         public void testBadEnd() {
142                 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?end=2013-04-25T11:01:25Q");
143         }
144         private void testCommon(int expect) {
145                 testCommon(expect, "");
146         }
147         private void testCommon(int expect, String query) {
148                 String url = props.getProperty("test.host") + "/feedlog/" + feedid + query;
149                 HttpGet httpGet = new HttpGet(url);
150                 try {
151                         HttpResponse response = httpclient.execute(httpGet);
152                     ckResponse(response, expect);
153
154                         HttpEntity entity = response.getEntity();
155                         String ctype = entity.getContentType().getValue().trim();
156                         if (expect == HttpServletResponse.SC_OK) {
157                                 if (!ctype.equals(FeedServlet.LOGLIST_CONTENT_TYPE))
158                                         fail("Got wrong content type: "+ctype);
159                         }
160
161                         // do something useful with the response body and ensure it is fully consumed
162                         if (ctype.equals(FeedServlet.LOGLIST_CONTENT_TYPE)) {
163                                 try {
164                                         returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
165                                         int n = returnedlist.length();
166                                         if (n != 0)
167                                                 System.err.println(n + " items");
168                                 } catch (Exception e) {
169                                         fail("Bad JSON: "+e.getMessage());
170                                 }
171                         } else {
172                                 EntityUtils.consume(entity);
173                         }
174                 } catch (IOException e) {
175                         fail(e.getMessage());
176                 } finally {
177                         httpGet.releaseConnection();
178                 }
179         }
180 }