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