[DMAAP-DR] Remove AAF/TLS phase 1
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / IntegrationTestFeedPut.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 static org.junit.Assert.fail;
27
28 import java.io.IOException;
29
30 import jakarta.servlet.http.HttpServletResponse;
31
32 import org.apache.http.Header;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.client.methods.HttpPut;
36 import org.apache.http.entity.ByteArrayEntity;
37 import org.apache.http.entity.ContentType;
38 import org.apache.http.util.EntityUtils;
39 import org.json.JSONArray;
40 import org.json.JSONException;
41 import org.json.JSONObject;
42 import org.json.JSONTokener;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.dmaap.datarouter.provisioning.FeedServlet;
48
49 public class IntegrationTestFeedPut extends IntegrationTestBase {
50     @BeforeClass
51     public static void setUpBeforeClass() throws Exception {
52     }
53
54     @AfterClass
55     public static void tearDownAfterClass() throws Exception {
56     }
57
58     @Before
59     public void setUp() throws Exception {
60         super.setUp();
61         getDBstate();
62     }
63
64     @Test
65     public void testPutNoFeedId() {
66         JSONObject jo = buildFeedRequest();
67         testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, "/feed/");
68     }
69
70     @Test
71     public void testPutNoFeed() {
72         JSONObject jo = buildFeedRequest();
73         testCommon(jo, HttpServletResponse.SC_NOT_FOUND, "/feed/999999");
74     }
75
76     @Test
77     public void testBadContentType() {
78         JSONObject jo = buildFeedRequest();
79         testCommon(jo, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "bad/bad", "JUnit");
80     }
81
82     @Test
83     public void testChangeName() {
84         JSONObject jo = buildFeedRequest();
85         jo.put("name", "badname");
86         testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
87     }
88
89     @Test
90     public void testChangeVersion() {
91         JSONObject jo = buildFeedRequest();
92         jo.put("version", "badvers");
93         testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
94     }
95
96     @Test
97     public void testBadPublisher() {
98         JSONObject jo = buildFeedRequest();
99         testCommon(jo, HttpServletResponse.SC_BAD_REQUEST, FeedServlet.FEED_CONTENT_TYPE, "BadBadBad");
100     }
101
102     @Test
103     public void testChangeDescription() {
104         JSONObject jo = buildFeedRequest();
105         // change descr
106         jo.put("description", "This description HAS BEEN CHANGED!!!");
107         testCommon(jo, HttpServletResponse.SC_OK, FeedServlet.FEED_CONTENT_TYPE, "JUnit");
108     }
109
110     private void testCommon(JSONObject jo, int expect, String uri) {
111         testCommon(jo, expect, FeedServlet.FEED_CONTENT_TYPE, "Junit", uri);
112     }
113
114     private void testCommon(JSONObject jo, int expect, String ctype, String bhdr) {
115         JSONArray ja = db_state.getJSONArray("feeds");
116         for (int i = 0; i < ja.length(); i++) {
117             JSONObject feed0 = ja.getJSONObject(i);
118             if (!feed0.getBoolean("deleted") && feed0.getString("publisher").equals(bhdr)) {
119                 int feedid = feed0.getInt("feedid");
120                 testCommon(jo, expect, ctype, bhdr, "/feed/" + feedid);
121                 return;
122             }
123         }
124     }
125
126     private void testCommon(JSONObject jo, int expect, String ctype, String bhdr, String uri) {
127         String url   = props.getProperty("test.host") + uri;
128         HttpPut put = new HttpPut(url);
129         try {
130             if (bhdr != null) {
131                 put.addHeader(FeedServlet.BEHALF_HEADER, bhdr);
132             }
133             String strJo = jo.toString();
134             HttpEntity body = new ByteArrayEntity(strJo.getBytes(), ContentType.create(ctype));
135             put.setEntity(body);
136
137             HttpResponse response = httpclient.execute(put);
138             ckResponse(response, expect);
139
140             HttpEntity entity = response.getEntity();
141             ctype = entity.getContentType().getValue().trim();
142             int code = response.getStatusLine().getStatusCode();
143             if (code == HttpServletResponse.SC_CREATED && !ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
144                 fail("Got wrong content type: " + ctype);
145             }
146
147             if (code == HttpServletResponse.SC_CREATED) {
148                 Header[] loc = response.getHeaders("Location");
149                 if (loc == null) {
150                     fail("Missing Location header.");
151                 }
152             }
153
154             // do something useful with the response body and ensure it is fully consumed
155             if (ctype.equals(FeedServlet.FEEDFULL_CONTENT_TYPE)) {
156                 // ck Location header!
157                 JSONObject jo2 = null;
158                 try {
159                     jo2 = new JSONObject(new JSONTokener(entity.getContent()));
160                     System.err.println(jo2.toString());
161                 } catch (Exception e) {
162                     fail("Bad JSON: " + e.getMessage());
163                 }
164                 try {
165                     jo2.getString("publisher");
166                     JSONObject jo3 = jo2.getJSONObject("links");
167                     jo3.getString("self");
168                     jo3.getString("publish");
169                     jo3.getString("subscribe");
170                     jo3.getString("log");
171                 } catch (JSONException e) {
172                     fail("required field missing from result: " + e.getMessage());
173                 }
174             } else {
175                 EntityUtils.consume(entity);
176             }
177         } catch (IOException e) {
178             fail(e.getMessage());
179         } finally {
180             put.releaseConnection();
181         }
182     }
183
184     private JSONObject buildFeedRequest() {
185         JSONObject jo = new JSONObject();
186         jo.put("name", "feed");
187         jo.put("version", "1.0.0");
188         jo.put("description", "Sample feed used by JUnit to test");
189
190         JSONObject jo2 = new JSONObject();
191         jo2.put("classification", "unrestricted");
192
193         JSONObject jo3 = new JSONObject();
194         jo3.put("id", "id001");
195         jo3.put("password", "re1kwelj");
196         JSONObject jo4 = new JSONObject();
197         jo4.put("id", "id002");
198         jo4.put("password", "o9eqlmbd");
199
200         JSONArray ja = new JSONArray();
201         ja.put(jo3);
202         ja.put(jo4);
203         jo2.put("endpoint_ids", ja);
204
205         ja = new JSONArray();
206         ja.put("20.0.0.1");
207         ja.put("195.68.12.15");
208         ja.put("135.207.136.128/25");
209         jo2.put("endpoint_addrs", ja);
210
211         jo.put("authorization", jo2);
212         return jo;
213     }
214 }