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