04c1bda6683e2552bf043e652c8b06168179020b
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / resources / TopicResourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2018 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 package org.onap.dmaap.dbcapi.resources;
21
22 import static org.junit.Assert.assertTrue;
23
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.glassfish.jersey.test.JerseyTest;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.dmaap.dbcapi.database.DatabaseClass;
34 import org.onap.dmaap.dbcapi.model.DcaeLocation;
35 import org.onap.dmaap.dbcapi.model.Dmaap;
36 import org.onap.dmaap.dbcapi.model.MR_Cluster;
37 import org.onap.dmaap.dbcapi.model.Topic;
38 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
39
40
41 public class TopicResourceTest extends JerseyTest {
42
43         static DmaapObjectFactory factory = new DmaapObjectFactory();
44
45         @Override
46         protected Application configure() {
47
48                 return new ResourceConfig()
49                                 .register( TopicResource.class )
50                                 .register( MR_ClusterResource.class )
51                                 .register( DcaeLocationResource.class )
52                                 .register( DmaapResource.class );
53         }
54
55         private static final String  fmt = "%24s: %s%n";
56
57         private boolean is2xx(int val ) {
58                 if ( val >= 200 && val < 300 ) {
59                         return true;
60                 }
61                 return false;
62         }
63
64         @Before
65         public void preTest() throws Exception {
66                 DatabaseClass.clearDatabase();
67                 try {
68
69                         Dmaap dmaap = factory.genDmaap();
70                         Entity<Dmaap> reqEntity = Entity.entity( dmaap, MediaType.APPLICATION_JSON );
71                         Response resp = target( "dmaap").request().post( reqEntity, Response.class );
72                         System.out.println( "POST dmaap resp=" + resp.getStatus() );
73                         assertTrue( is2xx( resp.getStatus()) );
74                 
75                 }catch (Exception e ) {
76                 }
77                 try {
78                         DcaeLocation loc = factory.genDcaeLocation( "central" );
79                         Entity<DcaeLocation> reqEntity = Entity.entity( loc, MediaType.APPLICATION_JSON );
80                         Response resp = target( "dcaeLocations").request().post( reqEntity, Response.class );
81                         System.out.println( "POST dcaeLocation resp=" + resp.getStatus() + " " + resp.readEntity( String.class ));
82                         if ( resp.getStatus() != 409 ) {
83                                 assertTrue( is2xx( resp.getStatus())  );
84                         }
85                 } catch (Exception e ) {
86                 }
87                 try {
88                         MR_Cluster cluster = factory.genMR_Cluster( "central" );
89                         Entity<MR_Cluster> reqEntity = Entity.entity( cluster, MediaType.APPLICATION_JSON );
90                         Response resp = target( "mr_clusters").request().post( reqEntity, Response.class );
91                         System.out.println( "POST MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
92                         if (resp.getStatus() != 409 ) {
93                                 assertTrue( is2xx( resp.getStatus()) );
94                         }       
95                 } catch (Exception e ) {
96                         
97                 }
98
99         }
100         /*  may conflict with test framework! 
101         @After
102         public void tearDown() throws Exception {
103         }
104 */
105
106
107         @Test
108         public void GetTest() {
109                 Response resp = target( "topics").request().get( Response.class );
110                 System.out.println( "GET feed resp=" + resp.getStatus() );
111
112                 assertTrue( resp.getStatus() == 200 );
113         }
114         
115
116         @Test
117         public void PostTest() {
118                 Topic topic = factory.genSimpleTopic( "test1" );
119                 Entity<Topic> reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
120                 Response resp = target( "topics").request().post( reqEntity, Response.class );
121                 System.out.println( "POST Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
122                 if (resp.getStatus() != 409 ) {
123                         assertTrue( resp.getStatus() == 201);
124                 }
125                 resp = target( "topics").
126                                 path( topic.genFqtn() ).request().get( Response.class );
127                 System.out.println( "GET Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
128         
129                 assertTrue( resp.getStatus() == 200 );
130                 
131         }
132
133
134         @Test
135         public void PutTest() {
136
137                 Topic topic = factory.genSimpleTopic( "test2" );
138                 Entity<Topic> reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
139                 Response resp = target( "topics").request().post( reqEntity, Response.class );
140                 String json = resp.readEntity(String.class);
141                 System.out.println( "POST Topic resp=" + resp.getStatus() + " " + json );
142                 if (resp.getStatus() != 409 ) {
143                         assertTrue( resp.getStatus() == 201);
144                 }
145
146                 
147                 // now change a field
148                 topic.setOwner( "newbody" );
149                 reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
150
151                 // update with incorrect key
152                 resp = target( "topics")
153                                         .path( "org.onap.dmaap.notATopic" )
154                                         .request()
155                                         .put( reqEntity, Response.class );
156                 
157                 System.out.println( "PUT Topic resp=" + resp.getStatus() + " expect 400" );
158                 assertTrue( resp.getStatus() == 400 );
159
160                 // update with correct key
161                 topic = new Topic( json );
162                 reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
163                 resp = target( "topics")
164                                         .path( topic.getFqtn())
165                                         .request()
166                                         .put( reqEntity, Response.class );
167                 System.out.println( "PUT Topic resp=" + resp.getStatus() + " " + resp.readEntity(String.class));
168                 assertTrue( resp.getStatus() == 400 );  // PUT is not allowed even with the right key
169         }
170
171         @Test
172         public void DelTest() {
173
174                 Topic topic = factory.genSimpleTopic( "test3" );
175                 topic.setFqtn( "org.onap.unittest.test3" );
176                 
177                 Response resp = target( "topics").
178                                 path( topic.getFqtn() ).
179                                 request().
180                                 delete( Response.class );
181
182                 // confirm topic is not there 
183                 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
184                 assertTrue( resp.getStatus() == 404 );
185                 
186                 // now, add it
187                 Entity<Topic> reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
188                 resp = target( "topics").request().post( reqEntity, Response.class );
189                 String json = resp.readEntity( String.class );
190                 System.out.println( "POST Topic resp=" + resp.getStatus() + " " + json );
191                 assertTrue( resp.getStatus() == 201 );
192                 
193                 topic = new Topic( json );
194                 // now really delete it 
195                  resp = target( "topics").
196                                 path( topic.getFqtn()).
197                                 request().
198                                 delete( Response.class );
199                 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
200                 assertTrue( resp.getStatus() == 204 );
201
202         }
203
204
205
206 }
207