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