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