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