aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test/java/datarouter/provisioning/IntegrationTestDrFeedsPost.java
blob: 9f604fe9ddc2464be6e6930b89c1a6bdf7b83f5a (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*******************************************************************************
 * ============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 static org.junit.Assert.fail;

import java.io.IOException;

import jakarta.servlet.http.HttpServletResponse;

import org.apache.http.Header;
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.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.dmaap.datarouter.provisioning.FeedServlet;

public class IntegrationTestDrFeedsPost extends IntegrationTestBase {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void testNormal() {
        JSONObject jo = buildFeedRequest();
        testCommon(jo, HttpServletResponse.SC_CREATED);
    }

    @Test
    public void testNormalNoCtVersion() {
        JSONObject jo = buildFeedRequest();
        testCommon(jo, HttpServletResponse.SC_CREATED, "application/vnd.dmaap-dr.feed", "JUnit");
    }

    @Test
    public void testBadContentType() {
        JSONObject jo = buildFeedRequest();
        testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "Junit");
    }

    @Test
    public void testNoBehalfHeader() {
        JSONObject jo = buildFeedRequest();
        testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, null);
    }

    @Test
    public void testMissingName() {
        JSONObject jo = buildFeedRequest();
        jo.remove("name");
        testCommon(jo, 400);
    }

    @Test
    public void testTooLongName() {
        JSONObject jo = buildFeedRequest();
        jo.put("name", "123456789012345678901234567890");
        testCommon(jo, 400);
    }

    @Test
    public void testMissingVersion() {
        JSONObject jo = buildFeedRequest();
        jo.remove("version");
        testCommon(jo, 400);
    }

    @Test
    public void testTooLongVersion() {
        JSONObject jo = buildFeedRequest();
        jo.put("version", "123456789012345678901234567890");
        testCommon(jo, 400);
    }

    @Test
    public void testTooLongDescription() {
        // normal request
        JSONObject jo = buildFeedRequest();
        jo.put("description", s257);
        testCommon(jo, 400);
    }

    @Test
    public void testMissingAuthorization() {
        JSONObject jo = buildFeedRequest();
        jo.remove("authorization");
        testCommon(jo, 400);
    }

    @Test
    public void testMissingClassification() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        j2.remove("classification");
        testCommon(jo, 400);
    }

    @Test
    public void testTooLongClassification() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        j2.put("classification", s33);
        testCommon(jo, 400);
    }

    @Test
    public void testNoEndpointIds() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        j2.put("endpoint_ids", new JSONArray());
        testCommon(jo, 400);
    }

    @Test
    public void testBadIpAddress1() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("ZZZ^&#$%@#&^%$@#&^");
        testCommon(jo, 400);
    }

    @Test
    public void testBadIpAddress2() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("135.207.136.678");  // bad IPv4 addr
        testCommon(jo, 400);
    }

    @Test
    public void testBadIpAddress3() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("2001:1890:1110:d000:1a29::17567");  // bad IPv6 addr
        testCommon(jo, 400);
    }

    @Test
    public void testBadNetMask() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("10.10.10.10/64");
        testCommon(jo, 400);
    }

    @Test
    public void testGoodIpAddress1() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("135.207.136.175"); // good IPv4 addr
        testCommon(jo, 201);
    }

    @Test
    public void testGoodIpAddress2() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("2001:1890:1110:d000:1a29::175"); // good IPv6 addr
        testCommon(jo, 201);
    }

    @Test
    public void testGoodNetMask() {
        JSONObject jo = buildFeedRequest();
        JSONObject j2 = jo.getJSONObject("authorization");
        JSONArray ja = j2.getJSONArray("endpoint_addrs");
        ja.put("2001:1890:1110:d000:1a29::175/120");
        testCommon(jo, 201);
    }

    private void testCommon(JSONObject jo, int expect) {
        testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
    }

    private void testCommon(JSONObject jo, int expect, String ctype, String bhdr) {
        String url   = props.getProperty("test.host") + "/";
        HttpPost httpPost = new HttpPost(url);
        try {
            if (bhdr != null) {
                httpPost.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
            }
            String strJo = jo.toString();
            HttpEntity body = new ByteArrayEntity(strJo.getBytes(), ContentType.create(ctype));
            httpPost.setEntity(body);

            HttpResponse response = httpclient.execute(httpPost);
            ckResponse(response, expect);

            HttpEntity entity = response.getEntity();
            ctype = entity.getContentType().getValue().trim();
            int code = response.getStatusLine().getStatusCode();
            if (code == HttpServletResponse.SC_CREATED && !ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
                fail("Got wrong content type: " + ctype);
            }

            if (code == HttpServletResponse.SC_CREATED) {
                Header[] loc = response.getHeaders("Location");
                if (loc == null) {
                    fail("Missing Location header.");
                }
            }

            // do something useful with the response body and ensure it is fully consumed
            if (ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
                // ck Location header!
                JSONObject jo2 = null;
                try {
                    jo2 = new JSONObject(new JSONTokener(entity.getContent()));
                    System.err.println(jo2.toString());
                } catch (Exception e) {
                    fail("Bad JSON: " + e.getMessage());
                }
                try {
                    jo2.getString("publisher");
                    JSONObject jo3 = jo2.getJSONObject("links");
                    jo3.getString("self");
                    jo3.getString("publish");
                    jo3.getString("subscribe");
                    jo3.getString("log");
                } catch (JSONException e) {
                    fail("required field missing from result: " + e.getMessage());
                }
            } else {
                EntityUtils.consume(entity);
            }
        } catch (IOException e) {
            fail(e.getMessage());
        } finally {
            httpPost.releaseConnection();
        }
    }

    private JSONObject buildFeedRequest() {
        JSONObject jo = new JSONObject();
        jo.put("name", "JunitFeed");
        jo.put("version", "" + System.currentTimeMillis());  // make version unique
        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;
    }
}
/*
curl -v -X POST -H 'X-DMAAP-DR-ON-BEHALF-OF: tester' -H 'Content-type: application/vnd.dmaap-dr.feed' \
    --user publisher:tomcat \
    --data "$data" http://127.0.0.1:8080/prov/feed/
*/