[DMAAP-48] Initial code import
[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 package datarouter.provisioning;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.security.KeyManagementException;
30 import java.security.KeyStore;
31 import java.security.KeyStoreException;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.UnrecoverableKeyException;
34
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.conn.scheme.Scheme;
39 import org.apache.http.conn.ssl.SSLSocketFactory;
40 import org.apache.http.entity.ByteArrayEntity;
41 import org.apache.http.entity.ContentType;
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.JSONArray;
46 import org.json.JSONObject;
47
48 import com.att.research.datarouter.provisioning.FeedServlet;
49
50 public class FillDB {
51         public static void main(String[] args)
52                 throws KeyStoreException, FileNotFoundException, KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException
53         {
54                 AbstractHttpClient httpclient = new DefaultHttpClient();
55
56                 String keystore = "/home/eby/dr2/misc/client.keystore";
57                 String kspass   = "changeit";
58                 KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
59             FileInputStream instream = new FileInputStream(new File(keystore));
60             try {
61                 trustStore.load(instream, kspass.toCharArray());
62             } catch (Exception x) {
63                 System.err.println("READING KEYSTORE: "+x);
64             } finally {
65                 try { instream.close(); } catch (Exception ignore) {}
66             }
67
68             SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore, "changeit", trustStore);
69             Scheme sch = new Scheme("https", 443, socketFactory);
70             httpclient.getConnectionManager().getSchemeRegistry().register(sch);
71
72             JSONObject jo = buildFeedRequest();
73                 for (int i = 0; i < 10000; i++) {
74                         jo.put("version", ""+System.currentTimeMillis());
75                         int rv = -1;
76                         String url   = "https://conwy.proto.research.att.com:6443/";
77                         HttpPost httpPost = new HttpPost(url);
78                         try {
79                                 httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
80                                 String t = jo.toString();
81                                 HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create(FeedServlet.FEED_CONTENT_TYPE));
82                                 httpPost.setEntity(body);
83
84                                 HttpResponse response = httpclient.execute(httpPost);
85                                 rv = response.getStatusLine().getStatusCode();
86                                 HttpEntity entity = response.getEntity();
87                                 EntityUtils.consume(entity);
88                         } catch (IOException e) {
89                                 System.err.println(e);
90                         } finally {
91                                 httpPost.releaseConnection();
92                         }
93                         System.out.println(i + " " + rv);
94                 }
95         }
96         private static JSONObject buildFeedRequest() {
97                 JSONObject jo = new JSONObject();
98                 jo.put("name", "feed");
99                 jo.put("version", ""+System.currentTimeMillis());
100                 jo.put("description", "Sample feed used by JUnit to test");
101
102                         JSONObject jo2 = new JSONObject();
103                         jo2.put("classification", "unrestricted");
104
105                         JSONArray ja = new JSONArray();
106                                 JSONObject jo3 = new JSONObject();
107                                 jo3.put("id", "id001");
108                                 jo3.put("password", "re1kwelj");
109                                 JSONObject jo4 = new JSONObject();
110                                 jo4.put("id", "id002");
111                                 jo4.put("password", "o9eqlmbd");
112                                 ja.put(jo3);
113                                 ja.put(jo4);
114                         jo2.put("endpoint_ids", ja);
115
116                         ja = new JSONArray();
117                                 ja.put("10.0.0.1");
118                                 ja.put("192.168.0.1");
119                                 ja.put("135.207.136.128/25");
120                         jo2.put("endpoint_addrs", ja);
121
122                 jo.put("authorization", jo2);
123                 return jo;
124         }
125 }