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