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 ******************************************************************************/
23 package datarouter.provisioning;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import java.text.SimpleDateFormat;
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.JSONTokener;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
43 import com.att.research.datarouter.provisioning.FeedServlet;
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'");
51 public static void setUpBeforeClass() throws Exception {
52 // need to seed the DB here
56 public static void tearDownAfterClass() throws Exception {
57 // need to "unseed" the DB
61 public void setUp() throws Exception {
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");
73 public void testNormal() {
74 testCommon(HttpServletResponse.SC_OK);
77 public void testNormalPubOnly() {
78 testCommon(HttpServletResponse.SC_OK, "?type=pub");
81 public void testNormalDelOnly() {
82 testCommon(HttpServletResponse.SC_OK, "?type=del");
85 public void testNormalExpOnly() {
86 testCommon(HttpServletResponse.SC_OK, "?type=exp");
89 public void testNormalXXXOnly() {
90 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?type=xxx");
93 public void testNormalStatusSuccess() {
94 testCommon(HttpServletResponse.SC_OK, "?statusCode=success");
97 public void testNormalStatusRedirect() {
98 testCommon(HttpServletResponse.SC_OK, "?statusCode=redirect");
101 public void testNormalStatusFailure() {
102 testCommon(HttpServletResponse.SC_OK, "?statusCode=failure");
105 public void testNormalStatus200() {
106 testCommon(HttpServletResponse.SC_OK, "?statusCode=200");
109 public void testNormalStatusXXX() {
110 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?statusCode=xxx");
113 public void testNormalExpiryNotRetryable() {
114 testCommon(HttpServletResponse.SC_OK, "?expiryReason=notRetryable");
117 public void testNormalExpiryRetriesExhausted() {
118 testCommon(HttpServletResponse.SC_OK, "?expiryReason=retriesExhausted");
121 public void testNormalExpiryXXX() {
122 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?expiryReason=xxx");
125 public void testNormalPublishId() {
126 testCommon(HttpServletResponse.SC_OK, "?publishId=1366985877801.mtdvnj00-drtr.proto.research.att.com");
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)));
134 public void testBadStart() {
135 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?start=xxx");
138 public void testLongEnd() {
139 testCommon(HttpServletResponse.SC_OK, "?end=1364837896220");
142 public void testBadEnd() {
143 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?end=2013-04-25T11:01:25Q");
145 private void testCommon(int expect) {
146 testCommon(expect, "");
148 private void testCommon(int expect, String query) {
149 String url = props.getProperty("test.host") + "/feedlog/" + feedid + query;
150 HttpGet httpGet = new HttpGet(url);
152 HttpResponse response = httpclient.execute(httpGet);
153 ckResponse(response, expect);
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);
162 // do something useful with the response body and ensure it is fully consumed
163 if (ctype.equals(FeedServlet.LOGLIST_CONTENT_TYPE)) {
165 returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
166 int n = returnedlist.length();
168 System.err.println(n + " items");
169 } catch (Exception e) {
170 fail("Bad JSON: "+e.getMessage());
173 EntityUtils.consume(entity);
175 } catch (IOException e) {
176 fail(e.getMessage());
178 httpGet.releaseConnection();