Update project structure to org.onap
[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 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
41 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
42
43 public class testInternalGet extends testBase {
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                         HttpEntity entity = response.getEntity();
65                         String ctype = entity.getContentType().getValue().trim();
66                         boolean ok  = ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE1);
67                         ok |= ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE2);
68                         if (!ok)
69                                 fail("Got wrong content type: "+ctype);
70
71                         // do something useful with the response body and ensure it is fully consumed
72                         if (ok) {
73                                 JSONObject jo = null;
74                                 try {
75                                         jo = new JSONObject(new JSONTokener(entity.getContent()));
76                                 } catch (Exception e) {
77                                         fail("Bad JSON: "+e.getMessage());
78                                 }
79                                 try {
80                                         jo.getJSONArray("feeds");
81                                         jo.getJSONArray("subscriptions");
82                                         JSONObject jo2 = jo.getJSONObject("parameters");
83                                         jo2.getJSONArray(Parameters.NODES);
84                                         jo2.getString(Parameters.ACTIVE_POD);
85                                         jo2.getString(Parameters.STANDBY_POD);
86                                         jo2.getInt(Parameters.LOGROLL_INTERVAL);
87                                         jo2.getInt(Parameters.DELIVERY_INIT_RETRY_INTERVAL);
88                                         jo2.getInt(Parameters.DELIVERY_MAX_RETRY_INTERVAL);
89                                         jo2.getInt(Parameters.DELIVERY_RETRY_RATIO);
90                                         jo2.getInt(Parameters.DELIVERY_MAX_AGE);
91                                 } catch (JSONException e) {
92                                         fail("required field missing from result: "+e.getMessage());
93                                 }
94                         } else {
95                                 EntityUtils.consume(entity);
96                         }
97                 } catch (IOException e) {
98                         e.printStackTrace();
99                         fail(e.getMessage());
100                 } finally {
101                         httpPost.releaseConnection();
102                 }
103         }
104 }