25397f7f9b39fe6906bcfd80f4095616b8632c23
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / IntegrationTestInternalGet.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 org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.util.EntityUtils;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.json.JSONTokener;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
41 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
42
43 public class IntegrationTestInternalGet extends IntegrationTestBase {
44     @BeforeClass
45     public static void setUpBeforeClass() throws Exception {
46     }
47
48     @AfterClass
49     public static void tearDownAfterClass() throws Exception {
50     }
51
52     @Test
53     public void testNormal() {
54         String url   = props.getProperty("test.host") + "/internal/prov";
55         HttpGet httpPost = new HttpGet(url);
56         try {
57             httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
58
59             HttpResponse response = httpclient.execute(httpPost);
60             int code = response.getStatusLine().getStatusCode();
61             if (code != 200) {
62                 fail("Unexpected response, expect " + 200 + " got " + code);
63             }
64
65             HttpEntity entity = response.getEntity();
66             String ctype = entity.getContentType().getValue().trim();
67             boolean ok  = ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE1);
68             ok |= ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE2);
69             if (!ok) {
70                 fail("Got wrong content type: " + ctype);
71             }
72
73             // do something useful with the response body and ensure it is fully consumed
74             if (ok) {
75                 JSONObject jo = null;
76                 try {
77                     jo = new JSONObject(new JSONTokener(entity.getContent()));
78                 } catch (Exception e) {
79                     fail("Bad JSON: " + e.getMessage());
80                 }
81                 try {
82                     jo.getJSONArray("feeds");
83                     jo.getJSONArray("subscriptions");
84                     JSONObject jo2 = jo.getJSONObject("parameters");
85                     jo2.getJSONArray(Parameters.NODES);
86                     jo2.getString(Parameters.ACTIVE_POD);
87                     jo2.getString(Parameters.STANDBY_POD);
88                     jo2.getInt(Parameters.LOGROLL_INTERVAL);
89                     jo2.getInt(Parameters.DELIVERY_INIT_RETRY_INTERVAL);
90                     jo2.getInt(Parameters.DELIVERY_MAX_RETRY_INTERVAL);
91                     jo2.getInt(Parameters.DELIVERY_RETRY_RATIO);
92                     jo2.getInt(Parameters.DELIVERY_MAX_AGE);
93                 } catch (JSONException e) {
94                     fail("required field missing from result: " + e.getMessage());
95                 }
96             } else {
97                 EntityUtils.consume(entity);
98             }
99         } catch (IOException e) {
100             e.printStackTrace();
101             fail(e.getMessage());
102         } finally {
103             httpPost.releaseConnection();
104         }
105     }
106 }