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