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.Test;
32 import org.onap.dmaap.dbcapi.model.DcaeLocation;
33 import org.onap.dmaap.dbcapi.model.MR_Cluster;
34 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
37 public class MR_ClusterResourceTest extends JerseyTest {
39 static DmaapObjectFactory factory = new DmaapObjectFactory();
42 protected Application configure() {
44 return new ResourceConfig()
45 .register( MR_ClusterResource.class )
46 .register( DcaeLocationResource.class );
49 private static final String fmt = "%24s: %s%n";
53 /* may conflict with test framework!
55 public void preTest() throws Exception {
59 public void tearDown() throws Exception {
65 public void GetTest() {
66 Response resp = target( "mr_clusters").request().get( Response.class );
67 System.out.println( "GET MR_Cluster resp=" + resp.getStatus() );
69 assertTrue( resp.getStatus() == 200 );
72 public void PostTest() {
73 MR_Cluster cluster = factory.genMR_Cluster( "central" );
74 Entity<MR_Cluster> reqEntity = Entity.entity( cluster, MediaType.APPLICATION_JSON );
75 Response resp = target( "mr_clusters").request().post( reqEntity, Response.class );
76 System.out.println( "POST MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
77 if (resp.getStatus() != 409 ) {
78 assertTrue( resp.getStatus() == 201);
80 resp = target( "mr_clusters").
81 path( cluster.getDcaeLocationName()).request().get( Response.class );
82 System.out.println( "GET MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
84 assertTrue( resp.getStatus() >= 200 && resp.getStatus() < 300 );
89 public void PutTest() {
92 DcaeLocation loc = factory.genDcaeLocation( "central" );
93 Entity<DcaeLocation> reqEntity = Entity.entity( loc, MediaType.APPLICATION_JSON );
94 Response resp = target( "dcaeLocations").request().post( reqEntity, Response.class );
95 System.out.println( "POST dcaeLocation resp=" + resp.getStatus() + " " + resp.readEntity( String.class ));
96 if ( resp.getStatus() != 409 ) {
97 assertTrue( resp.getStatus() == 201 );
99 } catch (Exception e ) {
102 String h[] = {"host4", "host5", "host6" };
103 MR_Cluster cluster = factory.genMR_Cluster( "central" );
104 Entity<MR_Cluster> reqEntity = Entity.entity( cluster, MediaType.APPLICATION_JSON );
105 Response resp = target( "mr_clusters").request().post( reqEntity, Response.class );
108 System.out.println( "POST MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
109 if( resp.getStatus() != 409 ) {
110 assertTrue( resp.getStatus() >= 200 && resp.getStatus() < 300 );
113 // now change a field
115 reqEntity = Entity.entity( cluster, MediaType.APPLICATION_JSON );
117 // update with incorrect key
118 resp = target( "mr_clusters")
119 .path( "invalidLocationNam" )
121 .put( reqEntity, Response.class );
122 System.out.println( "PUT MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity(String.class));
123 assertTrue( resp.getStatus() == 404 );
125 // update with correct key
126 resp = target( "mr_clusters")
127 .path( cluster.getDcaeLocationName())
129 .put( reqEntity, Response.class );
130 System.out.println( "PUT MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity(String.class));
131 assertTrue( resp.getStatus() >= 200 && resp.getStatus() < 300 );
135 public void DelTest() {
138 DcaeLocation loc = factory.genDcaeLocation( "edge" );
139 Entity<DcaeLocation> reqEntity = Entity.entity( loc, MediaType.APPLICATION_JSON );
140 Response resp = target( "dcaeLocations").request().post( reqEntity, Response.class );
141 System.out.println( "POST dcaeLocation resp=" + resp.getStatus() + " " + resp.readEntity( String.class ));
142 if ( resp.getStatus() != 409 ) {
143 assertTrue( resp.getStatus() == 201 );
145 } catch (Exception e ) {
149 MR_Cluster cluster = factory.genMR_Cluster( "edge" );
151 Response resp = target( "mr_clusters").
152 path( cluster.getDcaeLocationName()).
154 delete( Response.class );
156 // confirm cluster is not there
157 System.out.println( "DELETE MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
158 assertTrue( resp.getStatus() == 404 );
161 Entity<MR_Cluster> reqEntity = Entity.entity( cluster, MediaType.APPLICATION_JSON );
162 resp = target( "mr_clusters").request().post( reqEntity, Response.class );
165 System.out.println( "POST MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
166 assertTrue( resp.getStatus() == 201 || resp.getStatus() == 200 );
168 // now really delete it
169 resp = target( "mr_clusters").
170 path( cluster.getDcaeLocationName()).
172 delete( Response.class );
173 System.out.println( "DELETE MR_Cluster resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
174 assertTrue( resp.getStatus() == 204 );