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