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