[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testPublish.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.HttpResponse;
34 import org.apache.http.client.RedirectStrategy;
35 import org.apache.http.client.methods.HttpDelete;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.client.methods.HttpPut;
39 import org.apache.http.client.methods.HttpRequestBase;
40 import org.apache.http.impl.client.DefaultRedirectStrategy;
41 import org.json.JSONArray;
42 import org.json.JSONObject;
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 testPublish extends testBase {
51         private String publish_url;
52
53         @BeforeClass
54         public static void setUpBeforeClass() throws Exception {
55         }
56
57         @AfterClass
58         public static void tearDownAfterClass() throws Exception {
59         }
60
61         @Before
62         public void setUp() throws Exception {
63                 super.setUp();
64                 getDBstate();
65                 // Get publish URL from first feed
66                 JSONArray ja = db_state.getJSONArray("feeds");
67                 for (int i = ja.length()-1; i >= 0; i--) {
68                         JSONObject feed = ja.getJSONObject(i);
69                         if (!feed.getBoolean("deleted")) {
70                                 publish_url = feed.getJSONObject("links").getString("publish");
71                                 publish_url += "/" + System.currentTimeMillis();
72                                 return;
73                         }
74                 }
75         }
76
77         @Test
78         public void testDelete() {
79                 HttpDelete x = new HttpDelete(publish_url);
80                 testCommon(x);
81         }
82         @Test
83         public void testGet() {
84                 HttpGet x = new HttpGet(publish_url);
85                 testCommon(x);
86         }
87         @Test
88         public void testPut() {
89                 HttpPut x = new HttpPut(publish_url);
90                 testCommon(x);
91         }
92         @Test
93         public void testPost() {
94                 HttpPost x = new HttpPost(publish_url);
95                 testCommon(x);
96         }
97         private void testCommon(HttpRequestBase rb) {
98                 try {
99                         rb.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
100                         RedirectStrategy strategy = new DefaultRedirectStrategy() {
101                                 protected boolean isRedirectable(String method) {
102                                         return false;
103                                 }
104                         };
105                         httpclient.setRedirectStrategy(strategy);
106                         HttpResponse response = httpclient.execute(rb);
107                     ckResponse(response, HttpServletResponse.SC_MOVED_PERMANENTLY);
108
109                     // Make sure there is a Location hdr
110                     Header[] loc = response.getHeaders("Location");
111                     if (loc == null || loc.length == 0)
112                         fail("No location header");
113                 } catch (IOException e) {
114                         fail(e.getMessage());
115                 } finally {
116                         rb.releaseConnection();
117                 }
118         }
119 }