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