d124af4395ad27d57d76f287c9c3443218ff0dae
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testBase.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.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.security.KeyStore;
33 import java.util.Properties;
34
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.StatusLine;
38 import org.apache.http.client.methods.HttpGet;
39 import org.apache.http.conn.scheme.Scheme;
40 import org.apache.http.conn.ssl.SSLSocketFactory;
41 import org.apache.http.impl.client.AbstractHttpClient;
42 import org.apache.http.impl.client.DefaultHttpClient;
43 import org.apache.http.util.EntityUtils;
44 import org.json.JSONObject;
45 import org.json.JSONTokener;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
49
50 public class testBase {
51         /** The properties file to read the DB properties from */
52         public static final String CONFIG_FILE = "tests.properties";
53
54         public Properties props;
55         protected AbstractHttpClient httpclient;
56         protected String s_33;
57         protected String s_257;
58         protected static JSONObject db_state;
59
60         @Before
61         public void setUp() throws Exception {
62                 if (props == null) {
63                         props = new Properties();
64                         InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE);
65                         try {
66                                 props.load(inStream);
67                         } catch (Exception e) {
68                                 e.printStackTrace();
69                         } finally {
70                                 inStream.close();
71                         }
72                 }
73
74                 httpclient = new DefaultHttpClient();
75                 String s = "0123456789ABCDEF";
76                 s_33 = s + s + "!";
77                 s = s + s + s + s;
78                 s_257 = s + s + s + s + "!";
79
80                 // keystore
81                 String store = props.getProperty("test.keystore");
82                 String pass  = props.getProperty("test.kspassword");
83                 KeyStore keyStore  = KeyStore.getInstance(KeyStore.getDefaultType());
84             FileInputStream instream = new FileInputStream(new File(store));
85             try {
86                 keyStore.load(instream, pass.toCharArray());
87             } catch (Exception x) {
88                 System.err.println("READING KEYSTORE: "+x);
89             } finally {
90                 try { instream.close(); } catch (Exception ignore) {}
91             }
92
93                 store = props.getProperty("test.truststore");
94                 pass  = props.getProperty("test.tspassword");
95                 KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
96             instream = new FileInputStream(new File(store));
97             try {
98                 trustStore.load(instream, pass.toCharArray());
99             } catch (Exception x) {
100                 System.err.println("READING TRUSTSTORE: "+x);
101             } finally {
102                 try { instream.close(); } catch (Exception ignore) {}
103             }
104
105             SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, "changeit", trustStore);
106             Scheme sch = new Scheme("https", 443, socketFactory);
107             httpclient.getConnectionManager().getSchemeRegistry().register(sch);
108         }
109
110         public JSONObject getDBstate() {
111                 // set db_state!
112                 if (db_state == null) {
113                         String url   = props.getProperty("test.host") + "/internal/prov";
114                         HttpGet httpGet = new HttpGet(url);
115                         try {
116                                 httpGet.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
117                                 HttpResponse response = httpclient.execute(httpGet);
118                                 HttpEntity entity = response.getEntity();
119                                 String ctype = entity.getContentType().getValue().trim();
120                                 // save the response body as db_state
121                                 boolean ok  = ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE1);
122                                         ok |= ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE2);
123                                 if (ok) {
124                                         db_state = null;
125                                         try {
126                                                 db_state = new JSONObject(new JSONTokener(entity.getContent()));
127                                         } catch (Exception e) {
128                                                 fail("Bad JSON: "+e.getMessage());
129                                         }
130                                 } else {
131                                         EntityUtils.consume(entity);
132                                 }
133                         } catch (IOException e) {
134                                 fail(e.getMessage());
135                         } finally {
136                                 httpGet.releaseConnection();
137                         }
138                 }
139                 return db_state;
140         }
141
142         @After
143         public void tearDown() throws Exception {
144                 // When HttpClient instance is no longer needed,
145                 // shut down the connection manager to ensure
146                 // immediate deallocation of all system resources
147                 httpclient.getConnectionManager().shutdown();
148         }
149
150         protected void ckResponse(HttpResponse response, int expect) {
151                 System.out.println(response.getStatusLine());
152                 StatusLine sl = response.getStatusLine();
153                 int code = sl.getStatusCode();
154                 if (code != expect)
155                         fail("Unexpected response, expect "+expect+" got "+code+" "+sl.getReasonPhrase());
156         }
157 }