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";
59 public void preTest() throws Exception {
60 DatabaseClass.clearDatabase();
63 Dmaap dmaap = factory.genDmaap();
64 Entity<Dmaap> reqEntity = Entity.entity( dmaap, MediaType.APPLICATION_JSON );
65 Response resp = target( "dmaap").request().put( reqEntity, Response.class );
66 System.out.println( resp.getStatus() );
67 assertTrue( resp.getStatus() == 200 );
68 }catch (Exception e ) {
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 );
78 } catch (Exception e ) {
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 );
88 } catch (Exception e ) {
93 /* may conflict with test framework!
95 public void tearDown() throws Exception {
101 public void GetTest() {
102 Response resp = target( "topics").request().get( Response.class );
103 System.out.println( "GET feed resp=" + resp.getStatus() );
105 assertTrue( resp.getStatus() == 200 );
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);
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 ) );
122 assertTrue( resp.getStatus() == 200 );
128 public void PutTest() {
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);
140 // now change a field
141 topic.setOwner( "newbody" );
142 reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
144 // update with incorrect key
145 resp = target( "topics")
146 .path( "org.onap.dmaap.notATopic" )
148 .put( reqEntity, Response.class );
150 System.out.println( "PUT Topic resp=" + resp.getStatus() + " expect 400" );
151 assertTrue( resp.getStatus() == 400 );
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())
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
165 public void DelTest() {
167 Topic topic = factory.genSimpleTopic( "test3" );
168 topic.setFqtn( "org.onap.unittest.test3" );
170 Response resp = target( "topics").
171 path( topic.getFqtn() ).
173 delete( Response.class );
175 // confirm topic is not there
176 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
177 assertTrue( resp.getStatus() == 404 );
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 );
186 topic = new Topic( json );
187 // now really delete it
188 resp = target( "topics").
189 path( topic.getFqtn()).
191 delete( Response.class );
192 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
193 assertTrue( resp.getStatus() == 204 );