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;
42 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
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'");
50 public static void setUpBeforeClass() throws Exception {
51 // need to seed the DB here
55 public static void tearDownAfterClass() throws Exception {
56 // need to "unseed" the DB
60 public void setUp() throws Exception {
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");
72 public void testNormal() {
73 testCommon(HttpServletResponse.SC_OK);
76 public void testNormalPubOnly() {
77 testCommon(HttpServletResponse.SC_OK, "?type=pub");
80 public void testNormalDelOnly() {
81 testCommon(HttpServletResponse.SC_OK, "?type=del");
84 public void testNormalExpOnly() {
85 testCommon(HttpServletResponse.SC_OK, "?type=exp");
88 public void testNormalXXXOnly() {
89 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?type=xxx");
92 public void testNormalStatusSuccess() {
93 testCommon(HttpServletResponse.SC_OK, "?statusCode=success");
96 public void testNormalStatusRedirect() {
97 testCommon(HttpServletResponse.SC_OK, "?statusCode=redirect");
100 public void testNormalStatusFailure() {
101 testCommon(HttpServletResponse.SC_OK, "?statusCode=failure");
104 public void testNormalStatus200() {
105 testCommon(HttpServletResponse.SC_OK, "?statusCode=200");
108 public void testNormalStatusXXX() {
109 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?statusCode=xxx");
112 public void testNormalExpiryNotRetryable() {
113 testCommon(HttpServletResponse.SC_OK, "?expiryReason=notRetryable");
116 public void testNormalExpiryRetriesExhausted() {
117 testCommon(HttpServletResponse.SC_OK, "?expiryReason=retriesExhausted");
120 public void testNormalExpiryXXX() {
121 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?expiryReason=xxx");
124 public void testNormalPublishId() {
125 testCommon(HttpServletResponse.SC_OK, "?publishId=1366985877801.mtdvnj00-drtr.proto.research.att.com");
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)));
133 public void testBadStart() {
134 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?start=xxx");
137 public void testLongEnd() {
138 testCommon(HttpServletResponse.SC_OK, "?end=1364837896220");
141 public void testBadEnd() {
142 testCommon(HttpServletResponse.SC_BAD_REQUEST, "?end=2013-04-25T11:01:25Q");
144 private void testCommon(int expect) {
145 testCommon(expect, "");
147 private void testCommon(int expect, String query) {
148 String url = props.getProperty("test.host") + "/feedlog/" + feedid + query;
149 HttpGet httpGet = new HttpGet(url);
151 HttpResponse response = httpclient.execute(httpGet);
152 ckResponse(response, expect);
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);
161 // do something useful with the response body and ensure it is fully consumed
162 if (ctype.equals(FeedServlet.LOGLIST_CONTENT_TYPE)) {
164 returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
165 int n = returnedlist.length();
167 System.err.println(n + " items");
168 } catch (Exception e) {
169 fail("Bad JSON: "+e.getMessage());
172 EntityUtils.consume(entity);
174 } catch (IOException e) {
175 fail(e.getMessage());
177 httpGet.releaseConnection();