917507f4d97490acee67a788314f9cf3ec4f14dd
[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         
133         @Test
134         public void GetTest() {
135                 Response resp = target( "dr_subs").request().get( Response.class );
136                 System.out.println( "GET dr_subs resp=" + resp.getStatus() );
137
138                 assertTrue( resp.getStatus() == 200 );
139         }
140         
141         @Test
142         public void PostTest() {
143
144                 Feed feed = addFeed( "subPostTest", "post unit test" );
145                 System.out.println( "subPostTest: feedId=" + feed.getFeedId());
146                 
147                 String d, un, up;
148                 d = "central-onap";
149                 un = "user1";
150                 up = "secretW0rd";
151
152                 DR_Sub dr_pub = addSub( d, un, up, feed.getFeedId() );
153         }
154
155         @Test
156         public void PutTest() {
157
158                 Feed feed = addFeed( "subPutTest", "put unit test");
159                 String d, un, up;
160                 d = "central-onap";
161                 un = "user1";
162                 up = "secretW0rd";
163
164                 DR_Sub dr_sub = addSub( d, un, up, feed.getFeedId() );
165                 
166                 dr_sub.setUserpwd("newSecret");
167                 Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
168                 Response resp = target( "dr_subs")
169                                 .path( dr_sub.getSubId() )
170                                 .request()
171                                 .put( reqEntity2, Response.class);
172                 System.out.println( "PUT dr_subs resp=" + resp.getStatus() );
173                 assertTrue( resp.getStatus() == 200 );
174         }
175 /*
176  * TODO: figure out how to check delete() response
177         @Test
178         public void DelTest() {
179
180                 Feed feed = addFeed( "subDelTest", "del unit test");
181                 String d, un, up;
182                 d = "central-onap";
183                 un = "user1";
184                 up = "secretW0rd";
185
186                 DR_Sub dr_sub = addSub( d, un, up, feed.getFeedId() );
187                 
188                 Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
189                 Response resp = target( "dr_subs")
190                                 .path( dr_sub.getSubId() )
191                                 .request()
192                                 .delete();
193                 System.out.println( "DEL dr_subs resp=" + resp.getStatus() );
194                 assertTrue( resp.getStatus() == 200 );
195         }
196 */
197
198 }
199
200