Update for OOM integration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / DbTestData.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 org.apache.http.HttpEntity;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.entity.ByteArrayEntity;
30 import org.apache.http.entity.ContentType;
31 import org.apache.http.impl.client.AbstractHttpClient;
32 import org.apache.http.util.EntityUtils;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
36
37 import java.io.IOException;
38 import java.util.Properties;
39
40 /**
41  * The DbTestData class
42  *
43  * @version 1.0.1
44  */
45 public class DbTestData {
46
47     private static boolean dbReady = false;
48
49     public static void populateDb(AbstractHttpClient httpclient, Properties props) {
50         if (!dbReady) {
51             JSONObject jo = buildFeedRequest();
52             for (int i = 0; i < 10; i++) {
53                 jo.put("version", "" + System.currentTimeMillis());
54                 int statusCode = -1;
55                 String url = props.getProperty("test.host");
56                 HttpPost httpPost = new HttpPost(url);
57                 try {
58                     httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
59                     String feedRequestString = jo.toString();
60                     HttpEntity body = new ByteArrayEntity(feedRequestString.getBytes(),
61                         ContentType.create(FeedServlet.FEED_CONTENT_TYPE));
62                     httpPost.setEntity(body);
63                     HttpResponse response = httpclient.execute(httpPost);
64                     statusCode = response.getStatusLine().getStatusCode();
65                     HttpEntity entity = response.getEntity();
66                     EntityUtils.consume(entity);
67                 } catch (IOException e) {
68                     System.err.println(e);
69                 } finally {
70                     httpPost.releaseConnection();
71                 }
72                 System.out.println(i + " " + statusCode);
73             }
74             dbReady = true;
75         }
76     }
77
78     private static JSONObject buildFeedRequest() {
79         JSONObject jo = new JSONObject();
80         jo.put("name", "feed");
81         jo.put("version", "" + System.currentTimeMillis());
82         jo.put("description", "Sample feed used by JUnit to test");
83
84         JSONObject jo2 = new JSONObject();
85         jo2.put("classification", "unrestricted");
86
87         JSONObject jo3 = new JSONObject();
88         jo3.put("id", "id001");
89         jo3.put("password", "re1kwelj");
90
91         JSONObject jo4 = new JSONObject();
92         jo4.put("id", "id002");
93         jo4.put("password", "o9eqlmbd");
94
95         JSONArray ja = new JSONArray();
96         ja.put(jo3);
97         ja.put(jo4);
98         jo2.put("endpoint_ids", ja);
99
100         ja = new JSONArray();
101         ja.put("10.0.0.1");
102         ja.put("192.168.0.1");
103         ja.put("135.207.136.128/25");
104         jo2.put("endpoint_addrs", ja);
105
106         jo.put("authorization", jo2);
107         return jo;
108     }
109 }