[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testFeedDelete.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.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.client.methods.HttpDelete;
35 import org.apache.http.util.EntityUtils;
36 import org.json.JSONArray;
37 import org.json.JSONObject;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42
43 import com.att.research.datarouter.provisioning.FeedServlet;
44
45 public class testFeedDelete extends testBase {
46         @BeforeClass
47         public static void setUpBeforeClass() throws Exception {
48         }
49
50         @AfterClass
51         public static void tearDownAfterClass() throws Exception {
52         }
53
54         @Before
55         public void setUp() throws Exception {
56                 super.setUp();
57                 getDBstate();
58         }
59
60         @Test
61         public void testDeleteNormal() {
62                 // Delete the first non-deleted feed in the DB
63                 JSONArray ja = db_state.getJSONArray("feeds");
64                 for (int i = ja.length()-1; i >= 0; i--) {
65                         JSONObject feed = ja.getJSONObject(i);
66                         if (!feed.getBoolean("deleted")) {
67                                 int feedid = feed.getInt("feedid");
68                                 testCommon(HttpServletResponse.SC_NO_CONTENT, "/feed/"+feedid);
69                                 return;
70                         }
71                 }
72         }
73         @Test
74         public void testDeleteNoFeedID() {
75                 testCommon(HttpServletResponse.SC_BAD_REQUEST, "/feed/");
76         }
77         @Test
78         public void testDeleteNoFeed() {
79                 testCommon(HttpServletResponse.SC_NOT_FOUND, "/feed/999999");
80         }
81         private void testCommon(int expect, String uri) {
82                 String url = props.getProperty("test.host") + uri;
83                 HttpDelete del = new HttpDelete(url);
84                 try {
85                         del.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
86
87                         HttpResponse response = httpclient.execute(del);
88                     ckResponse(response, expect);
89
90                         HttpEntity entity = response.getEntity();
91                         EntityUtils.consume(entity);
92                 } catch (IOException e) {
93                         fail(e.getMessage());
94                 } finally {
95                         del.releaseConnection();
96                 }
97         }
98 }