Updates for more code coverage
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / resources / DR_SubResourceTest.java
1
2 /*-
3  * ============LICENSE_START=======================================================
4  * org.onap.dmaap
5  * ================================================================================
6  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.dmaap.dbcapi.resources;
22 import org.onap.dmaap.dbcapi.model.*;
23 import org.onap.dmaap.dbcapi.service.*;
24 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
25
26 import static org.junit.Assert.*;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import java.util.*;
32 import java.sql.*;
33
34 import org.glassfish.jersey.test.JerseyTest;
35 import org.glassfish.jersey.server.ResourceConfig;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.core.Application;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.Path;
41 import javax.ws.rs.GET;
42
43 public class DR_SubResourceTest extends JerseyTest{
44         
45         static DmaapObjectFactory factory = new DmaapObjectFactory();
46
47         @Override
48         protected Application configure() {
49                 return new ResourceConfig()
50                                 .register( DR_SubResource.class )
51                                 .register( FeedResource.class )
52                                 .register( DcaeLocationResource.class )
53                                 .register( DmaapResource.class );
54         }
55
56         String d, un, up, f, p;
57         
58         @Before
59         public void preTest() throws Exception {
60                 try {
61
62                         Dmaap dmaap = factory.genDmaap();
63                         Entity<Dmaap> reqEntity = Entity.entity( dmaap, MediaType.APPLICATION_JSON );
64                         Response resp = target( "dmaap").request().post( reqEntity, Response.class );
65                         System.out.println( resp.getStatus() );
66                         assertTrue( resp.getStatus() == 200 );
67                 }catch (Exception e ) {
68                 }
69                 try {
70                         DcaeLocation loc = factory.genDcaeLocation( "central" );
71                         Entity<DcaeLocation> reqEntity = Entity.entity( loc, MediaType.APPLICATION_JSON );
72                         Response resp = target( "dcaeLocations").request().post( reqEntity, Response.class );
73                         System.out.println( "POST dcaeLocation resp=" + resp.getStatus() + " " + resp.readEntity( String.class ));
74                         if ( resp.getStatus() != 409 ) {
75                                 assertTrue( resp.getStatus() == 201 );
76                         }
77                 } catch (Exception e ) {
78                 }
79         
80
81         }
82 /*
83         @Before
84         public void setUp() throws Exception {
85                 d = "central-onap";
86                 un = "user1";
87                 up = "secretW0rd";
88                 f = "234";
89                 p = "678";
90         }
91
92         @After
93         public void tearDown() throws Exception {
94         }
95 */
96
97
98
99 /*  may conflict with test framework! 
100         @Before
101         public void setUp() throws Exception {
102         }
103
104         @After
105         public void tearDown() throws Exception {
106         }
107 */
108
109         private Feed addFeed( String name, String desc ) {
110                 Feed feed = new Feed( name, "1.0", desc, "dgl", "unrestricted" );
111                 Entity<Feed> reqEntity = Entity.entity( feed, MediaType.APPLICATION_JSON );
112                 Response resp = target( "feeds").request().post( reqEntity, Response.class );
113                 int rc = resp.getStatus();
114                 System.out.println( "POST feed resp=" + rc );
115                 assertTrue( rc == 200 || rc == 409 );
116                 feed = resp.readEntity( Feed.class );
117                 return feed;
118         }
119         
120         private DR_Sub addSub( String d, String un, String up, String feedId ) {
121                 DR_Sub dr_sub = new DR_Sub( d, un, up, feedId, 
122                                 "https://subscriber.onap.org/foo", "https://dr-prov/sublog", true );
123
124                 Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
125                 Response resp = target( "dr_subs").request().post( reqEntity2, Response.class);
126                 System.out.println( "POST dr_subs resp=" + resp.getStatus() );
127                 assertTrue( resp.getStatus() == 201 );
128                 dr_sub = resp.readEntity( DR_Sub.class );
129                 
130                 return dr_sub;
131         }
132         private DR_Sub addSubByName( String d, String un, String up, String feedName ) {
133                 DR_Sub dr_sub = new DR_Sub( d, un, up, null, 
134                                 "https://subscriber.onap.org/foo", "https://dr-prov/sublog", true );
135                 
136                 dr_sub.setFeedName(feedName);
137
138                 Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
139                 Response resp = target( "dr_subs").request().post( reqEntity2, Response.class);
140                 System.out.println( "POST dr_subs resp=" + resp.getStatus() );
141                 assertTrue( resp.getStatus() == 201 );
142                 dr_sub = resp.readEntity( DR_Sub.class );
143                 
144                 return dr_sub;
145         }
146
147         @Test
148         public void GetTest() {
149                 Response resp = target( "dr_subs").request().get( Response.class );
150                 System.out.println( "GET dr_subs resp=" + resp.getStatus() );
151
152                 assertTrue( resp.getStatus() == 200 );
153         }
154         
155         @Test
156         public void PostTest() {
157
158                 Feed feed = addFeed( "subPostTest", "post unit test" );
159                 System.out.println( "subPostTest: feedId=" + feed.getFeedId());
160                 
161                 String d, un, up;
162                 d = "central-onap";
163                 un = "user1";
164                 up = "secretW0rd";
165
166                 DR_Sub dr_pub = addSub( d, un, up, feed.getFeedId() );
167         }
168
169         @Test
170         public void PostTestByName() {
171
172                 Feed feed = addFeed( "subPostTest2", "post unit test" );
173                 System.out.println( "subPostTest2: feedId=" + feed.getFeedId());
174                 
175                 String d, un, up;
176                 d = "central-onap";
177                 un = "user1";
178                 up = "secretW0rd";
179
180                 DR_Sub dr_pub = addSubByName( d, un, up, feed.getFeedName() );
181         }
182
183         @Test
184         public void PutTest() {
185
186                 Feed feed = addFeed( "subPutTest", "put unit test");
187                 String d, un, up;
188                 d = "central-onap";
189                 un = "user1";
190                 up = "secretW0rd";
191
192                 DR_Sub dr_sub = addSub( d, un, up, feed.getFeedId() );
193                 
194                 dr_sub.setUserpwd("newSecret");
195                 Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
196                 Response resp = target( "dr_subs")
197                                 .path( dr_sub.getSubId() )
198                                 .request()
199                                 .put( reqEntity2, Response.class);
200                 System.out.println( "PUT dr_subs resp=" + resp.getStatus() );
201                 assertTrue( resp.getStatus() == 200 );
202         }
203
204
205 // TODO: figure out how to check delete() response
206         @Test
207         public void DelTest() {
208
209                 Feed feed = addFeed( "subDelTest", "del unit test");
210                 String d, un, up;
211                 d = "central-onap";
212                 un = "user1";
213                 up = "secretW0rd";
214
215                 DR_Sub dr_sub = addSub( d, un, up, feed.getFeedId() );
216                 
217                 Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
218                 Response resp = target( "dr_subs")
219                                 .path( dr_sub.getSubId() )
220                                 .request()
221                                 .delete();
222                 System.out.println( "DEL dr_subs resp=" + resp.getStatus() );
223                 assertTrue( resp.getStatus() == 204 );
224         }
225
226
227 }
228
229