[DMAAP-DR] Remove AAF/TLS phase 1
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / IntegrationTestLogGet.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 import java.text.SimpleDateFormat;
30
31 import jakarta.servlet.http.HttpServletResponse;
32
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.util.EntityUtils;
37 import org.json.JSONArray;
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 IntegrationTestLogGet extends IntegrationTestBase {
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     /**
61      * This is the setUp method.
62      */
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66         getDBstate();
67 //        JSONArray ja = db_state.getJSONArray("feeds");
68 //        for (int i = 0; i < ja.length(); i++) {
69 //            JSONObject jo = ja.getJSONObject(i);
70 //            if (!jo.getBoolean("deleted"))
71 //                feedid = jo.getInt("feedid");
72 //        }
73     }
74
75     @Test
76     public void testNormal() {
77         testCommon(HttpServletResponse.SC_OK);
78     }
79
80     @Test
81     public void testNormalPubOnly() {
82         testCommon(HttpServletResponse.SC_OK, "?type=pub");
83     }
84
85     @Test
86     public void testNormalDelOnly() {
87         testCommon(HttpServletResponse.SC_OK, "?type=del");
88     }
89
90     @Test
91     public void testNormalExpOnly() {
92         testCommon(HttpServletResponse.SC_OK, "?type=exp");
93     }
94
95     @Test
96     public void testNormalXxxOnly() {
97         testCommon(HttpServletResponse.SC_BAD_REQUEST, "?type=xxx");
98     }
99
100     @Test
101     public void testNormalStatusSuccess() {
102         testCommon(HttpServletResponse.SC_OK, "?statusCode=success");
103     }
104
105     @Test
106     public void testNormalStatusRedirect() {
107         testCommon(HttpServletResponse.SC_OK, "?statusCode=redirect");
108     }
109
110     @Test
111     public void testNormalStatusFailure() {
112         testCommon(HttpServletResponse.SC_OK, "?statusCode=failure");
113     }
114
115     @Test
116     public void testNormalStatus200() {
117         testCommon(HttpServletResponse.SC_OK, "?statusCode=200");
118     }
119
120     @Test
121     public void testNormalStatusXxx() {
122         testCommon(HttpServletResponse.SC_BAD_REQUEST, "?statusCode=xxx");
123     }
124
125     @Test
126     public void testNormalExpiryNotRetryable() {
127         testCommon(HttpServletResponse.SC_OK, "?expiryReason=notRetryable");
128     }
129
130     @Test
131     public void testNormalExpiryRetriesExhausted() {
132         testCommon(HttpServletResponse.SC_OK, "?expiryReason=retriesExhausted");
133     }
134
135     @Test
136     public void testNormalExpiryXxx() {
137         testCommon(HttpServletResponse.SC_BAD_REQUEST, "?expiryReason=xxx");
138     }
139
140     @Test
141     public void testNormalPublishId() {
142         testCommon(HttpServletResponse.SC_OK, "?publishId=1366985877801.mtdvnj00-drtr.proto.research.att.com");
143     }
144
145     @Test
146     public void testNormalStart() {
147         long nowMinus5Days = System.currentTimeMillis() - (5 * 24 * 60 * 60 * 1000L);    // 5 days
148         testCommon(HttpServletResponse.SC_OK, String.format("?start=%s", sdf.format(nowMinus5Days)));
149     }
150
151     @Test
152     public void testBadStart() {
153         testCommon(HttpServletResponse.SC_BAD_REQUEST, "?start=xxx");
154     }
155
156     @Test
157     public void testLongEnd() {
158         testCommon(HttpServletResponse.SC_OK, "?end=1364837896220");
159     }
160
161     @Test
162     public void testBadEnd() {
163         testCommon(HttpServletResponse.SC_BAD_REQUEST, "?end=2013-04-25T11:01:25Q");
164     }
165
166     private void testCommon(int expect) {
167         testCommon(expect, "");
168     }
169
170     private void testCommon(int expect, String query) {
171         String url = props.getProperty("test.host") + "/feedlog/" + feedid + query;
172         HttpGet httpGet = new HttpGet(url);
173         try {
174             HttpResponse response = httpclient.execute(httpGet);
175             ckResponse(response, expect);
176
177             HttpEntity entity = response.getEntity();
178             String ctype = entity.getContentType().getValue().trim();
179             if (expect == HttpServletResponse.SC_OK) {
180                 if (!ctype.equals(FeedServlet.LOGLIST_CONTENT_TYPE)) {
181                     fail("Got wrong content type: " + ctype);
182                 }
183             }
184
185             // do something useful with the response body and ensure it is fully consumed
186             if (ctype.equals(FeedServlet.LOGLIST_CONTENT_TYPE)) {
187                 try {
188                     returnedlist = new JSONArray(new JSONTokener(entity.getContent()));
189                     int returnedListLength = returnedlist.length();
190                     if (returnedListLength != 0) {
191                         System.err.println(returnedListLength + " items");
192                     }
193                 } catch (Exception e) {
194                     fail("Bad JSON: " + e.getMessage());
195                 }
196             } else {
197                 EntityUtils.consume(entity);
198             }
199         } catch (IOException e) {
200             fail(e.getMessage());
201         } finally {
202             httpGet.releaseConnection();
203         }
204     }
205 }