2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.dbcapi.resources;
22 import static org.junit.Assert.assertTrue;
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;
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;
41 public class TopicResourceTest extends JerseyTest {
43 static DmaapObjectFactory factory = new DmaapObjectFactory();
46 protected Application configure() {
48 return new ResourceConfig()
49 .register( TopicResource.class )
50 .register( MR_ClusterResource.class )
51 .register( DcaeLocationResource.class )
52 .register( DmaapResource.class );
55 private static final String fmt = "%24s: %s%n";
57 private boolean is2xx(int val ) {
58 if ( val >= 200 && val < 300 ) {
65 public void preTest() throws Exception {
66 DatabaseClass.clearDatabase();
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()) );
75 }catch (Exception e ) {
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()) );
85 } catch (Exception e ) {
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()) );
95 } catch (Exception e ) {
100 /* may conflict with test framework!
102 public void tearDown() throws Exception {
108 public void GetTest() {
109 Response resp = target( "topics").request().get( Response.class );
110 System.out.println( "GET feed resp=" + resp.getStatus() );
112 assertTrue( resp.getStatus() == 200 );
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);
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 ) );
129 assertTrue( resp.getStatus() == 200 );
135 public void PutTest() {
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);
147 // now change a field
148 topic.setOwner( "newbody" );
149 reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
151 // update with incorrect key
152 resp = target( "topics")
153 .path( "org.onap.dmaap.notATopic" )
155 .put( reqEntity, Response.class );
157 System.out.println( "PUT Topic resp=" + resp.getStatus() + " expect 400" );
158 assertTrue( resp.getStatus() == 400 );
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())
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
172 public void DelTest() {
174 Topic topic = factory.genSimpleTopic( "test3" );
175 topic.setFqtn( "org.onap.unittest.test3" );
177 Response resp = target( "topics").
178 path( topic.getFqtn() ).
180 delete( Response.class );
182 // confirm topic is not there
183 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
184 assertTrue( resp.getStatus() == 404 );
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 );
193 topic = new Topic( json );
194 // now really delete it
195 resp = target( "topics").
196 path( topic.getFqtn()).
198 delete( Response.class );
199 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
200 assertTrue( resp.getStatus() == 204 );