Fix datarouter-prov server issue
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / FillDB.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 java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.security.KeyManagementException;
31 import java.security.KeyStore;
32 import java.security.KeyStoreException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.UnrecoverableKeyException;
35
36 import org.apache.http.HttpEntity;
37 import org.apache.http.HttpResponse;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.conn.scheme.Scheme;
40 import org.apache.http.conn.ssl.SSLSocketFactory;
41 import org.apache.http.entity.ByteArrayEntity;
42 import org.apache.http.entity.ContentType;
43 import org.apache.http.impl.client.AbstractHttpClient;
44 import org.apache.http.impl.client.DefaultHttpClient;
45 import org.apache.http.util.EntityUtils;
46 import org.json.JSONArray;
47 import org.json.JSONObject;
48 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
49
50 /**
51  * The FillDB class
52  *
53  * @version 1.0.1
54  */
55 public class FillDB {
56     /**
57      * This is the main method of the FillDB class.
58      *
59      * @throws KeyStoreException KeyStore exception
60      * @throws FileNotFoundException Exeception thrown when error locating file
61      * @throws KeyManagementException KeyManagement exception
62      * @throws UnrecoverableKeyException Exception thrown when Key cannot be recovered
63      * @throws NoSuchAlgorithmException Exception thrown when algorithm does not exist
64      */
65     public static void main(String[] args)
66         throws KeyStoreException, FileNotFoundException, KeyManagementException, UnrecoverableKeyException,
67             NoSuchAlgorithmException {
68         AbstractHttpClient httpclient = new DefaultHttpClient();
69
70         String keystore = "/home/eby/dr2/misc/client.keystore";
71         String kspass   = "changeit";
72         KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
73         FileInputStream instream = new FileInputStream(new File(keystore));
74         try {
75             trustStore.load(instream, kspass.toCharArray());
76         } catch (Exception x) {
77             System.err.println("READING KEYSTORE: " + x);
78         } finally {
79             try {
80                 instream.close();
81             } catch (Exception ignore) {
82                 // Ignore exception
83             }
84         }
85
86         SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore, "changeit", trustStore);
87         Scheme sch = new Scheme("https", 443, socketFactory);
88         httpclient.getConnectionManager().getSchemeRegistry().register(sch);
89
90         JSONObject jo = buildFeedRequest();
91         for (int i = 0; i < 10000; i++) {
92             jo.put("version", "" + System.currentTimeMillis());
93             int rv = -1;
94             String url   = "https://conwy.proto.research.att.com:6443/";
95             HttpPost httpPost = new HttpPost(url);
96             try {
97                 httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
98                 String feedRequestString = jo.toString();
99                 HttpEntity body = new ByteArrayEntity(feedRequestString.getBytes(),
100                         ContentType.create(FeedServlet.FEED_CONTENT_TYPE));
101                 httpPost.setEntity(body);
102
103                 HttpResponse response = httpclient.execute(httpPost);
104                 rv = response.getStatusLine().getStatusCode();
105                 HttpEntity entity = response.getEntity();
106                 EntityUtils.consume(entity);
107             } catch (IOException e) {
108                 System.err.println(e);
109             } finally {
110                 httpPost.releaseConnection();
111             }
112             System.out.println(i + " " + rv);
113         }
114     }
115
116     private static JSONObject buildFeedRequest() {
117         JSONObject jo = new JSONObject();
118         jo.put("name", "feed");
119         jo.put("version", "" + System.currentTimeMillis());
120         jo.put("description", "Sample feed used by JUnit to test");
121
122         JSONObject jo2 = new JSONObject();
123         jo2.put("classification", "unrestricted");
124
125
126         JSONObject jo3 = new JSONObject();
127         jo3.put("id", "id001");
128         jo3.put("password", "re1kwelj");
129
130         JSONObject jo4 = new JSONObject();
131         jo4.put("id", "id002");
132         jo4.put("password", "o9eqlmbd");
133
134         JSONArray ja = new JSONArray();
135         ja.put(jo3);
136         ja.put(jo4);
137         jo2.put("endpoint_ids", ja);
138
139         ja = new JSONArray();
140         ja.put("10.0.0.1");
141         ja.put("192.168.0.1");
142         ja.put("135.207.136.128/25");
143         jo2.put("endpoint_addrs", ja);
144
145         jo.put("authorization", jo2);
146         return jo;
147     }
148 }