fd00ea514b8324fca6ae10dab194765317387e92
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / IntegrationTestInternalMisc.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
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.Test;
39 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
40
41 public class IntegrationTestInternalMisc extends IntegrationTestBase {
42     @Test
43     public void testInternalDrlogs() {
44         String url   = props.getProperty("test.host") + "/internal/drlogs";
45         HttpGet httpPost = new HttpGet(url);
46         try {
47             httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
48             HttpResponse response = httpclient.execute(httpPost);
49             int code = response.getStatusLine().getStatusCode();
50             if (code != 200) {
51                 fail("Unexpected response, expect " + HttpServletResponse.SC_NOT_FOUND + " got " + code);
52             }
53
54             HttpEntity entity = response.getEntity();
55             String ctype = entity.getContentType().getValue().trim();
56             boolean ok  = ctype.equals("text/plain");
57             if (!ok) {
58                 fail("Got wrong content type: " + ctype);
59             }
60
61             EntityUtils.consume(entity);
62         } catch (IOException e) {
63             e.printStackTrace();
64             fail(e.getMessage());
65         } finally {
66             httpPost.releaseConnection();
67         }
68     }
69
70     @Test
71     public void testInternalHalt() {
72         String url   = props.getProperty("test.host") + "/internal/halt";
73         HttpGet httpPost = new HttpGet(url);
74         try {
75             httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
76
77             HttpResponse response = httpclient.execute(httpPost);
78             int code = response.getStatusLine().getStatusCode();
79             if (code != HttpServletResponse.SC_NOT_FOUND) {
80                 fail("Unexpected response, expect " + HttpServletResponse.SC_NOT_FOUND + " got " + code);
81             }
82
83             HttpEntity entity = response.getEntity();
84             EntityUtils.consume(entity);
85         } catch (IOException e) {
86             e.printStackTrace();
87             fail(e.getMessage());
88         } finally {
89             httpPost.releaseConnection();
90         }
91     }
92
93     @SuppressWarnings("unused")
94     @Test
95     public void testInternalLogs() {
96         String url   = props.getProperty("test.host") + "/internal/logs";
97         HttpGet httpPost = new HttpGet(url);
98         try {
99             httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
100
101             HttpResponse response = httpclient.execute(httpPost);
102             int code = response.getStatusLine().getStatusCode();
103             if (code != 200) {
104                 fail("Unexpected response, expect " + 200 + " got " + code);
105             }
106
107             HttpEntity entity = response.getEntity();
108             String ctype = entity.getContentType().getValue().trim();
109             boolean ok  = ctype.equals("application/json");
110             if (!ok) {
111                 fail("Got wrong content type: " + ctype);
112             }
113
114             // do something useful with the response body and ensure it is fully consumed
115             if (ok) {
116                 try {
117                     new JSONArray(new JSONTokener(entity.getContent()));
118                 } catch (Exception e) {
119                     fail("Bad JSON: " + e.getMessage());
120                 }
121             } else {
122                 EntityUtils.consume(entity);
123             }
124         } catch (IOException e) {
125             e.printStackTrace();
126             fail(e.getMessage());
127         } finally {
128             httpPost.releaseConnection();
129         }
130     }
131
132     @Test
133     public void testInternalBadUrl() {
134         String url   = props.getProperty("test.host") + "/internal/badurl";
135         HttpGet httpPost = new HttpGet(url);
136         try {
137             httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
138
139             HttpResponse response = httpclient.execute(httpPost);
140             int code = response.getStatusLine().getStatusCode();
141             if (code != HttpServletResponse.SC_NOT_FOUND) {
142                 fail("Unexpected response, expect " + HttpServletResponse.SC_NOT_FOUND + " got " + code);
143             }
144
145             HttpEntity entity = response.getEntity();
146             EntityUtils.consume(entity);
147         } catch (IOException e) {
148             e.printStackTrace();
149             fail(e.getMessage());
150         } finally {
151             httpPost.releaseConnection();
152         }
153     }
154
155 }