aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test/java/datarouter/provisioning/DbTestData.java
blob: 36a2eb019f7f683c175773f942f7b574325f63d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/

package datarouter.provisioning;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.FeedServlet;

import java.io.IOException;
import java.util.Properties;

/**
 * The DbTestData class
 *
 * @version 1.0.1
 */
public class DbTestData {

    private static boolean dbReady = false;

    public static void populateDb(AbstractHttpClient httpclient, Properties props) {
        if (!dbReady) {
            JSONObject jo = buildFeedRequest();
            for (int i = 0; i < 10; i++) {
                jo.put("version", "" + System.currentTimeMillis());
                int statusCode = -1;
                String url = props.getProperty("test.host");
                HttpPost httpPost = new HttpPost(url);
                try {
                    httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
                    String feedRequestString = jo.toString();
                    HttpEntity body = new ByteArrayEntity(feedRequestString.getBytes(),
                        ContentType.create(FeedServlet.FEED_CONTENT_TYPE));
                    httpPost.setEntity(body);
                    HttpResponse response = httpclient.execute(httpPost);
                    statusCode = response.getStatusLine().getStatusCode();
                    HttpEntity entity = response.getEntity();
                    EntityUtils.consume(entity);
                } catch (IOException e) {
                    System.err.println(e);
                } finally {
                    httpPost.releaseConnection();
                }
                System.out.println(i + " " + statusCode);
            }
            dbReady = true;
        }
    }

    private static JSONObject buildFeedRequest() {
        JSONObject jo = new JSONObject();
        jo.put("name", "feed");
        jo.put("version", "" + System.currentTimeMillis());
        jo.put("description", "Sample feed used by JUnit to test");

        JSONObject jo2 = new JSONObject();
        jo2.put("classification", "unrestricted");

        JSONObject jo3 = new JSONObject();
        jo3.put("id", "id001");
        jo3.put("password", "re1kwelj");

        JSONObject jo4 = new JSONObject();
        jo4.put("id", "id002");
        jo4.put("password", "o9eqlmbd");

        JSONArray ja = new JSONArray();
        ja.put(jo3);
        ja.put(jo4);
        jo2.put("endpoint_ids", ja);

        ja = new JSONArray();
        ja.put("10.0.0.1");
        ja.put("192.168.0.1");
        ja.put("135.207.136.128/25");
        jo2.put("endpoint_addrs", ja);

        jo.put("authorization", jo2);
        return jo;
    }
}