fa1c5f4cf99b5aebbd575fffaa576d06620250cc
[dmaap/datarouter.git] / datarouter-prov / src / test / java / datarouter / provisioning / testCleanup.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 org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.methods.HttpDelete;
33 import org.apache.http.util.EntityUtils;
34 import org.json.JSONArray;
35 import org.json.JSONObject;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 import com.att.research.datarouter.provisioning.FeedServlet;
40
41 public class testCleanup extends testBase {
42         @Before
43         public void setUp() throws Exception {
44                 super.setUp();
45                 getDBstate();
46         }
47
48         @Test
49         public void testNormal() {
50                 // Delete all feeds w/JUnit as publisher
51                 JSONArray ja = db_state.getJSONArray("feeds");
52                 for (int i = 0; i < ja.length(); i++) {
53                         JSONObject feed = ja.getJSONObject(i);
54                         if (feed != null && !feed.getBoolean("deleted")) {
55                                 if (feed.getString("publisher").equals("JUnit")) {
56                                         int feedid = feed.getInt("feedid");
57                                         delete("/feed/"+feedid);
58                                 }
59                         }
60                 }
61                 // Delete all subscriptions w/JUnit as subscriber
62                 ja = db_state.getJSONArray("subscriptions");
63                 for (int i = 0; i < ja.length(); i++) {
64                         JSONObject sub = ja.getJSONObject(i);
65                         if (sub != null && sub.getString("subscriber").equals("JUnit")) {
66                                 int subid = sub.getInt("subid");
67                                 delete("/subs/"+subid);
68                         }
69                 }
70         }
71         private void delete(String uri) {
72                 String url = props.getProperty("test.host") + uri;;
73                 HttpDelete del = new HttpDelete(url);
74                 try {
75                         del.addHeader(FeedServlet.BEHALF_HEADER, "JUnit");
76                         HttpResponse response = httpclient.execute(del);
77                         HttpEntity entity = response.getEntity();
78                         EntityUtils.consume(entity);
79                 } catch (IOException e) {
80                         fail(e.getMessage());
81                 } finally {
82                         del.releaseConnection();
83                 }
84         }
85 }