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.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;
39 public class TopicResourceTest extends JerseyTest {
41 static DmaapObjectFactory factory = new DmaapObjectFactory();
44 protected Application configure() {
46 return new ResourceConfig()
47 .register( TopicResource.class )
48 .register( MR_ClusterResource.class )
49 .register( DcaeLocationResource.class );
52 private static final String fmt = "%24s: %s%n";
58 public void preTest() throws Exception {
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 );
67 } catch (Exception e ) {
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);
77 } catch (Exception e ) {
82 /* may conflict with test framework!
84 public void tearDown() throws Exception {
90 public void GetTest() {
91 Response resp = target( "topics").request().get( Response.class );
92 System.out.println( "GET feed resp=" + resp.getStatus() );
94 assertTrue( resp.getStatus() == 200 );
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);
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 ) );
109 assertTrue( resp.getStatus() == 200 );
114 public void PutTest() {
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);
126 // now change a field
127 topic.setOwner( "newbody" );
128 reqEntity = Entity.entity( topic, MediaType.APPLICATION_JSON );
130 // update with incorrect key
131 resp = target( "topics")
132 .path( "org.onap.dmaap.notATopic" )
134 .put( reqEntity, Response.class );
136 System.out.println( "PUT Topic resp=" + resp.getStatus() + " expect 400" );
137 assertTrue( resp.getStatus() == 400 );
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())
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
151 public void DelTest() {
153 Topic topic = factory.genSimpleTopic( "test3" );
154 topic.setFqtn( "org.onap.unittest.test3" );
156 Response resp = target( "topics").
157 path( topic.getFqtn() ).
159 delete( Response.class );
161 // confirm topic is not there
162 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
163 assertTrue( resp.getStatus() == 404 );
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 );
172 topic = new Topic( json );
173 // now really delete it
174 resp = target( "topics").
175 path( topic.getFqtn()).
177 delete( Response.class );
178 System.out.println( "DELETE Topic resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
179 assertTrue( resp.getStatus() == 204 );