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