4646package com .teragrep .cfe18 .controllerTests ;
4747
4848import com .google .gson .Gson ;
49- import com .teragrep .cfe18 .handlers .entities .HostFile ;
5049import com .teragrep .cfe18 .handlers .entities .Hub ;
5150import org .apache .http .HttpEntity ;
5251import org .apache .http .HttpResponse ;
@@ -80,90 +79,15 @@ public class HubControllerTest extends TestSpringBootInformation {
8079 private int port ;
8180
8281 @ Test
83- @ Order ( 1 )
82+ @ BeforeAll
8483 public void testData () {
85- Hub hub2 = new Hub ();
86- hub2 .setFqHost ("hubfq1" );
87- hub2 .setMd5 ("hubmd52" );
88- hub2 .setIp ("hubip2" );
89-
90- String json2 = gson .toJson (hub2 );
91-
92- // forms the json to requestEntity
93- StringEntity requestEntity2 = new StringEntity (String .valueOf (json2 ), ContentType .APPLICATION_JSON );
94-
95- // Creates the request
96- HttpPut request2 = new HttpPut ("http://localhost:" + port + "/v2/host/hub" );
97- // set requestEntity to the put request
98- request2 .setEntity (requestEntity2 );
99- // Header
100- request2 .setHeader ("Authorization" , "Bearer " + token );
101-
102- // Get the response from endpoint
103- HttpResponse httpResponse = Assertions
104- .assertDoesNotThrow (() -> HttpClientBuilder .create ().build ().execute (request2 ));
105-
106- // Get the entity from response
107- HttpEntity entity = httpResponse .getEntity ();
108-
109- // Entity response string
110- String responseString = Assertions .assertDoesNotThrow (() -> EntityUtils .toString (entity ));
111-
112- // Parsing response as JSONObject
113- JSONObject responseAsJson = Assertions .assertDoesNotThrow (() -> new JSONObject (responseString ));
114-
115- // Creating expected message as JSON Object from the data that was sent towards endpoint
116- String expected = "New hub created" ;
117-
118- // Creating string from Json that was given as a response
119- String actual = Assertions .assertDoesNotThrow (() -> responseAsJson .get ("message" ).toString ());
120-
121- // add host to hub
122- HostFile host = new HostFile ();
123- host .setMd5 ("randommd5value" );
124- host .setFqHost ("hostFq" );
125- host .setHubFq ("hubfq1" );
126-
127- String json = gson .toJson (host );
128-
129- // forms the json to requestEntity
130- StringEntity requestEntity = new StringEntity (String .valueOf (json ), ContentType .APPLICATION_JSON );
131-
132- // Creates the request
133- HttpPut request = new HttpPut ("http://localhost:" + port + "/host/file" );
134- // set requestEntity to the put request
135- request .setEntity (requestEntity );
136- // Header
137- request .setHeader ("Authorization" , "Bearer " + token );
138-
139- // Get the response from endpoint
140- HttpResponse httpResponse2 = Assertions
141- .assertDoesNotThrow (() -> HttpClientBuilder .create ().build ().execute (request ));
142-
143- // Get the entity from response
144- HttpEntity entity2 = httpResponse2 .getEntity ();
145-
146- // Entity response string
147- String responseString2 = Assertions .assertDoesNotThrow (() -> EntityUtils .toString (entity2 ));
148-
149- // Parsing response as JSONObject
150- JSONObject responseAsJson2 = Assertions .assertDoesNotThrow (() -> new JSONObject (responseString2 ));
151-
152- // Creating expected message as JSON Object from the data that was sent towards endpoint
153- String expected2 = "New host created" ;
154-
155- // Creating string from Json that was given as a response
156- String actual2 = Assertions .assertDoesNotThrow (() -> responseAsJson2 .get ("message" ).toString ());
157-
158- // Assertions
159- assertEquals (expected , actual );
160- assertEquals (HttpStatus .SC_CREATED , httpResponse .getStatusLine ().getStatusCode ());
161- assertEquals (expected2 , actual2 );
162- assertEquals (HttpStatus .SC_CREATED , httpResponse2 .getStatusLine ().getStatusCode ());
84+ TestApiClient testApiClient = new TestApiClient (port , token );
85+ testApiClient .insertHub ("hubfq1" , "hubmd52" , "hubip2" );
86+ testApiClient .insertHostFile ("hostFq" , "randommd5value" , "hubfq1" );
16387 }
16488
16589 @ Test
166- @ Order (2 )
90+ @ Order (1 )
16791 public void testInsertHub () {
16892 Hub hub = new Hub ();
16993 hub .setFqHost ("hubfq" );
@@ -172,42 +96,31 @@ public void testInsertHub() {
17296
17397 String json = gson .toJson (hub );
17498
175- // forms the json to requestEntity
17699 StringEntity requestEntity = new StringEntity (String .valueOf (json ), ContentType .APPLICATION_JSON );
177100
178- // Creates the request
179101 HttpPut request = new HttpPut ("http://localhost:" + port + "/v2/host/hub" );
180- // set requestEntity to the put request
181102 request .setEntity (requestEntity );
182- // Header
183103 request .setHeader ("Authorization" , "Bearer " + token );
184104
185- // Get the response from endpoint
186105 HttpResponse httpResponse = Assertions
187106 .assertDoesNotThrow (() -> HttpClientBuilder .create ().build ().execute (request ));
188107
189- // Get the entity from response
190108 HttpEntity entity = httpResponse .getEntity ();
191109
192- // Entity response string
193110 String responseString = Assertions .assertDoesNotThrow (() -> EntityUtils .toString (entity ));
194111
195- // Parsing response as JSONObject
196112 JSONObject responseAsJson = Assertions .assertDoesNotThrow (() -> new JSONObject (responseString ));
197113
198- // Creating expected message as JSON Object from the data that was sent towards endpoint
199114 String expected = "New hub created" ;
200115
201- // Creating string from Json that was given as a response
202116 String actual = Assertions .assertDoesNotThrow (() -> responseAsJson .get ("message" ).toString ());
203117
204- // Assertions
205118 assertEquals (expected , actual );
206119 assertEquals (HttpStatus .SC_CREATED , httpResponse .getStatusLine ().getStatusCode ());
207120 }
208121
209122 @ Test
210- @ Order (3 )
123+ @ Order (2 )
211124 public void testGetHub () {
212125 Hub hub = new Hub ();
213126 hub .setHostId (1 );
@@ -216,7 +129,6 @@ public void testGetHub() {
216129 hub .setIp ("hubip2" );
217130 hub .setId (1 );
218131
219- // Asserting get request // request host_id as path variable
220132 HttpGet requestGet = new HttpGet ("http://localhost:" + port + "/v2/host/hub/" + 1 );
221133
222134 requestGet .setHeader ("Authorization" , "Bearer " + token );
@@ -233,7 +145,7 @@ public void testGetHub() {
233145 }
234146
235147 @ Test
236- @ Order (4 )
148+ @ Order (3 )
237149 public void testGetAllHubs () {
238150 ArrayList <Hub > expected = new ArrayList <>();
239151
@@ -256,7 +168,6 @@ public void testGetAllHubs() {
256168
257169 String json = gson .toJson (expected );
258170
259- // Asserting get request // request host_id as path variable
260171 HttpGet requestGet = new HttpGet ("http://localhost:" + port + "/v2/host/hub" );
261172
262173 requestGet .setHeader ("Authorization" , "Bearer " + token );
@@ -273,13 +184,11 @@ public void testGetAllHubs() {
273184 }
274185
275186 @ Test
276- @ Order (5 )
187+ @ Order (4 )
277188 public void testDeleteHubInUse () {
278- // try to delete given hub when host is using the given hub
279189
280190 HttpDelete delete = new HttpDelete ("http://localhost:" + port + "/v2/host/hub/" + 1 );
281191
282- // Header
283192 delete .setHeader ("Authorization" , "Bearer " + token );
284193
285194 HttpResponse deleteResponse = Assertions
@@ -289,24 +198,21 @@ public void testDeleteHubInUse() {
289198
290199 String responseStringGet = Assertions .assertDoesNotThrow (() -> EntityUtils .toString (entityDelete , "UTF-8" ));
291200
292- // Parsing response as JSONObject
293201 JSONObject responseAsJson = Assertions .assertDoesNotThrow (() -> new JSONObject (responseStringGet ));
294202
295- // Creating string from Json that was given as a response
296203 String actual = Assertions .assertDoesNotThrow (() -> responseAsJson .get ("message" ).toString ());
297- // Creating expected message as JSON Object from the data that was sent towards endpoint
204+
298205 String expected = "Is in use" ;
299206
300207 assertEquals (expected , actual );
301208 assertEquals (HttpStatus .SC_CONFLICT , deleteResponse .getStatusLine ().getStatusCode ());
302209 }
303210
304211 @ Test
305- @ Order (6 )
212+ @ Order (5 )
306213 public void testDeleteNonExistentHub () {
307214 HttpDelete delete = new HttpDelete ("http://localhost:" + port + "/v2/host/hub/" + 112412214 );
308215
309- // Header
310216 delete .setHeader ("Authorization" , "Bearer " + token );
311217
312218 HttpResponse deleteResponse = Assertions
@@ -316,12 +222,10 @@ public void testDeleteNonExistentHub() {
316222
317223 String responseStringGet = Assertions .assertDoesNotThrow (() -> EntityUtils .toString (entityDelete , "UTF-8" ));
318224
319- // Parsing response as JSONObject
320225 JSONObject responseAsJson = Assertions .assertDoesNotThrow (() -> new JSONObject (responseStringGet ));
321226
322- // Creating string from Json that was given as a response
323227 String actual = Assertions .assertDoesNotThrow (() -> responseAsJson .get ("message" ).toString ());
324- // Creating expected message as JSON Object from the data that was sent towards endpoint
228+
325229 String expected = "Record does not exist" ;
326230
327231 assertEquals (expected , actual );
@@ -333,7 +237,6 @@ public void testDeleteNonExistentHub() {
333237 public void testDeleteHub () {
334238 HttpDelete delete = new HttpDelete ("http://localhost:" + port + "/v2/host/hub/" + 2 );
335239
336- // Header
337240 delete .setHeader ("Authorization" , "Bearer " + token );
338241
339242 HttpResponse deleteResponse = Assertions
@@ -343,13 +246,10 @@ public void testDeleteHub() {
343246
344247 String responseStringGet = Assertions .assertDoesNotThrow (() -> EntityUtils .toString (entityDelete , "UTF-8" ));
345248
346- // Parsing response as JSONObject
347249 JSONObject responseAsJson = Assertions .assertDoesNotThrow (() -> new JSONObject (responseStringGet ));
348250
349- // Creating string from Json that was given as a response
350251 String actual = Assertions .assertDoesNotThrow (() -> responseAsJson .get ("message" ).toString ());
351252
352- // Creating expected message as JSON Object from the data that was sent towards endpoint
353253 String expected = "Hub deleted" ;
354254
355255 assertEquals (expected , actual );
0 commit comments