[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testInternalGet.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
41 import com.att.research.datarouter.provisioning.FeedServlet;
42 import com.att.research.datarouter.provisioning.beans.Parameters;
43
44 public class testInternalGet extends testBase {
45         @BeforeClass
46         public static void setUpBeforeClass() throws Exception {
47         }
48
49         @AfterClass
50         public static void tearDownAfterClass() throws Exception {
51         }
52
53         @Test
54         public void testNormal() {
55                 String url   = props.getProperty("test.host") + "/internal/prov";
56                 HttpGet httpPost = new HttpGet(url);
57                 try {
58                         httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
59
60                         HttpResponse response = httpclient.execute(httpPost);
61                         int code = response.getStatusLine().getStatusCode();
62                         if (code != 200)
63                                 fail("Unexpected response, expect "+200+" got "+code);
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                         // do something useful with the response body and ensure it is fully consumed
73                         if (ok) {
74                                 JSONObject jo = null;
75                                 try {
76                                         jo = new JSONObject(new JSONTokener(entity.getContent()));
77                                 } catch (Exception e) {
78                                         fail("Bad JSON: "+e.getMessage());
79                                 }
80                                 try {
81                                         jo.getJSONArray("feeds");
82                                         jo.getJSONArray("subscriptions");
83                                         JSONObject jo2 = jo.getJSONObject("parameters");
84                                         jo2.getJSONArray(Parameters.NODES);
85                                         jo2.getString(Parameters.ACTIVE_POD);
86                                         jo2.getString(Parameters.STANDBY_POD);
87                                         jo2.getInt(Parameters.LOGROLL_INTERVAL);
88                                         jo2.getInt(Parameters.DELIVERY_INIT_RETRY_INTERVAL);
89                                         jo2.getInt(Parameters.DELIVERY_MAX_RETRY_INTERVAL);
90                                         jo2.getInt(Parameters.DELIVERY_RETRY_RATIO);
91                                         jo2.getInt(Parameters.DELIVERY_MAX_AGE);
92                                 } catch (JSONException e) {
93                                         fail("required field missing from result: "+e.getMessage());
94                                 }
95                         } else {
96                                 EntityUtils.consume(entity);
97                         }
98                 } catch (IOException e) {
99                         e.printStackTrace();
100                         fail(e.getMessage());
101                 } finally {
102                         httpPost.releaseConnection();
103                 }
104         }
105 }