aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test/java/datarouter/provisioning/testInternalMisc.java
blob: d4339cc47aee3a9554a50a6dd4506c7d3a08e417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/

package datarouter.provisioning;

import static org.junit.Assert.fail;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.junit.Test;
import org.onap.dmaap.datarouter.provisioning.FeedServlet;

public class testInternalMisc extends testBase {
	@Test
	public void testInternalDrlogs() {
		String url   = props.getProperty("test.host") + "/internal/drlogs";
		HttpGet httpPost = new HttpGet(url);
		try {
			httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");

			HttpResponse response = httpclient.execute(httpPost);
			int code = response.getStatusLine().getStatusCode();
			if (code != 200)
				fail("Unexpected response, expect "+HttpServletResponse.SC_NOT_FOUND+" got "+code);

			HttpEntity entity = response.getEntity();
			String ctype = entity.getContentType().getValue().trim();
			boolean ok  = ctype.equals("text/plain");
			if (!ok)
				fail("Got wrong content type: "+ctype);

			EntityUtils.consume(entity);
		} catch (IOException e) {
			e.printStackTrace();
			fail(e.getMessage());
		} finally {
			httpPost.releaseConnection();
		}
	}

	@Test
	public void testInternalHalt() {
		String url   = props.getProperty("test.host") + "/internal/halt";
		HttpGet httpPost = new HttpGet(url);
		try {
			httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");

			HttpResponse response = httpclient.execute(httpPost);
			int code = response.getStatusLine().getStatusCode();
			if (code != HttpServletResponse.SC_NOT_FOUND)
				fail("Unexpected response, expect "+HttpServletResponse.SC_NOT_FOUND+" got "+code);

			HttpEntity entity = response.getEntity();
			EntityUtils.consume(entity);
		} catch (IOException e) {
			e.printStackTrace();
			fail(e.getMessage());
		} finally {
			httpPost.releaseConnection();
		}
	}

	@SuppressWarnings("unused")
	@Test
	public void testInternalLogs() {
		String url   = props.getProperty("test.host") + "/internal/logs";
		HttpGet httpPost = new HttpGet(url);
		try {
			httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");

			HttpResponse response = httpclient.execute(httpPost);
			int code = response.getStatusLine().getStatusCode();
			if (code != 200)
				fail("Unexpected response, expect "+200+" got "+code);

			HttpEntity entity = response.getEntity();
			String ctype = entity.getContentType().getValue().trim();
			boolean ok  = ctype.equals("application/json");
			if (!ok)
				fail("Got wrong content type: "+ctype);

			// do something useful with the response body and ensure it is fully consumed
			if (ok) {
				try {
					new JSONArray(new JSONTokener(entity.getContent()));
				} catch (Exception e) {
					fail("Bad JSON: "+e.getMessage());
				}
			} else {
				EntityUtils.consume(entity);
			}
		} catch (IOException e) {
			e.printStackTrace();
			fail(e.getMessage());
		} finally {
			httpPost.releaseConnection();
		}
	}

	@Test
	public void testInternalBadURL() {
		String url   = props.getProperty("test.host") + "/internal/badurl";
		HttpGet httpPost = new HttpGet(url);
		try {
			httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");

			HttpResponse response = httpclient.execute(httpPost);
			int code = response.getStatusLine().getStatusCode();
			if (code != HttpServletResponse.SC_NOT_FOUND)
				fail("Unexpected response, expect "+HttpServletResponse.SC_NOT_FOUND+" got "+code);

			HttpEntity entity = response.getEntity();
			EntityUtils.consume(entity);
		} catch (IOException e) {
			e.printStackTrace();
			fail(e.getMessage());
		} finally {
			httpPost.releaseConnection();
		}
	}

}