1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package datarouter.provisioning;
26 import static org.junit.Assert.fail;
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;
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;
49 import com.att.research.datarouter.provisioning.FeedServlet;
51 public class testBase {
52 /** The properties file to read the DB properties from */
53 public static final String CONFIG_FILE = "tests.properties";
55 public Properties props;
56 protected AbstractHttpClient httpclient;
57 protected String s_33;
58 protected String s_257;
59 protected static JSONObject db_state;
62 public void setUp() throws Exception {
64 props = new Properties();
65 InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE);
68 } catch (Exception e) {
75 httpclient = new DefaultHttpClient();
76 String s = "0123456789ABCDEF";
79 s_257 = s + s + s + s + "!";
82 String store = props.getProperty("test.keystore");
83 String pass = props.getProperty("test.kspassword");
84 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
85 FileInputStream instream = new FileInputStream(new File(store));
87 keyStore.load(instream, pass.toCharArray());
88 } catch (Exception x) {
89 System.err.println("READING KEYSTORE: "+x);
91 try { instream.close(); } catch (Exception ignore) {}
94 store = props.getProperty("test.truststore");
95 pass = props.getProperty("test.tspassword");
96 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
97 instream = new FileInputStream(new File(store));
99 trustStore.load(instream, pass.toCharArray());
100 } catch (Exception x) {
101 System.err.println("READING TRUSTSTORE: "+x);
103 try { instream.close(); } catch (Exception ignore) {}
106 SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, "changeit", trustStore);
107 Scheme sch = new Scheme("https", 443, socketFactory);
108 httpclient.getConnectionManager().getSchemeRegistry().register(sch);
111 public JSONObject getDBstate() {
113 if (db_state == null) {
114 String url = props.getProperty("test.host") + "/internal/prov";
115 HttpGet httpGet = new HttpGet(url);
117 httpGet.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
118 HttpResponse response = httpclient.execute(httpGet);
119 HttpEntity entity = response.getEntity();
120 String ctype = entity.getContentType().getValue().trim();
121 // save the response body as db_state
122 boolean ok = ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE1);
123 ok |= ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE2);
127 db_state = new JSONObject(new JSONTokener(entity.getContent()));
128 } catch (Exception e) {
129 fail("Bad JSON: "+e.getMessage());
132 EntityUtils.consume(entity);
134 } catch (IOException e) {
135 fail(e.getMessage());
137 httpGet.releaseConnection();
144 public void tearDown() throws Exception {
145 // When HttpClient instance is no longer needed,
146 // shut down the connection manager to ensure
147 // immediate deallocation of all system resources
148 httpclient.getConnectionManager().shutdown();
151 protected void ckResponse(HttpResponse response, int expect) {
152 System.out.println(response.getStatusLine());
153 StatusLine sl = response.getStatusLine();
154 int code = sl.getStatusCode();
156 fail("Unexpected response, expect "+expect+" got "+code+" "+sl.getReasonPhrase());