Skip to content

Commit 65bf6ee

Browse files
committed
Rechecked procedures and applied TestApiClient to Hub tests
1 parent 35094ad commit 65bf6ee

3 files changed

Lines changed: 73 additions & 122 deletions

File tree

src/main/resources/db/migration/R__Procedure_create_hub.sql

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* Teragrep, the applicable Commercial License may apply to this file if you as
4444
* a licensee so wish it.
4545
*/
46-
use cfe_18;
46+
USE cfe_18;
4747
DELIMITER //
4848
CREATE OR REPLACE PROCEDURE insert_cfe_hub(fqhost VARCHAR(128), md5 VARCHAR(32),
4949
ip VARCHAR(255))
@@ -54,7 +54,7 @@ BEGIN
5454
RESIGNAL;
5555
END;
5656
START TRANSACTION;
57-
IF ((SELECT COUNT(id)
57+
IF ((SELECT COUNT(h.id)
5858
FROM cfe_18.host h
5959
WHERE h.MD5 = md5
6060
AND h.fqhost = fqhost
@@ -67,20 +67,19 @@ BEGIN
6767
SELECT id INTO @hid FROM cfe_18.host h WHERE h.MD5 = md5 AND h.fqhost = fqhost AND h.host_type = 'CFE';
6868
END IF;
6969

70-
IF ((SELECT COUNT(h.host_id)
71-
FROM cfe_18.hubs h
72-
WHERE h.host_id = @hid
73-
AND h.ip = ip
74-
AND h.host_type = 'CFE') = 0) THEN
70+
IF ((SELECT COUNT(hu.host_id)
71+
FROM cfe_18.hubs hu
72+
WHERE hu.host_id = @hid
73+
AND hu.ip = ip
74+
AND hu.host_type = 'CFE') = 0) THEN
7575

7676
INSERT INTO cfe_18.hubs(host_id, ip, host_type)
7777
VALUES (@hid, ip, 'CFE');
78-
SELECT LAST_INSERT_ID() INTO @id;
79-
ELSE
80-
SELECT id INTO @id FROM cfe_18.hubs h WHERE h.host_id = @hid AND h.ip = ip AND h.host_type = 'CFE';
8178
END IF;
8279

83-
IF ((SELECT COUNT(host_id)
80+
SELECT id INTO @id FROM cfe_18.hubs hu WHERE hu.host_id = @hid AND hu.ip = ip AND hu.host_type = 'CFE';
81+
82+
IF ((SELECT COUNT(htc.host_id)
8483
FROM cfe_18.host_type_cfe htc
8584
WHERE htc.host_id = @hid
8685
AND htc.host_type = 'CFE'

src/test/java/com/teragrep/cfe18/controllerTests/HubControllerTest.java

Lines changed: 11 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
package com.teragrep.cfe18.controllerTests;
4747

4848
import com.google.gson.Gson;
49-
import com.teragrep.cfe18.handlers.entities.HostFile;
5049
import com.teragrep.cfe18.handlers.entities.Hub;
5150
import org.apache.http.HttpEntity;
5251
import 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);

src/test/java/com/teragrep/cfe18/controllerTests/TestApiClient.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,58 @@ public TestApiClient(int port, String token) {
6868
this.token = token;
6969
}
7070

71+
public Integer insertHub(final String fqHost, final String md5, final String ip) {
72+
Hub hub2 = new Hub();
73+
hub2.setFqHost(fqHost);
74+
hub2.setMd5(md5);
75+
hub2.setIp(ip);
76+
77+
String json2 = gson.toJson(hub2);
78+
79+
StringEntity requestEntity2 = new StringEntity(String.valueOf(json2), ContentType.APPLICATION_JSON);
80+
81+
HttpPut request2 = new HttpPut("http://localhost:" + port + "/v2/host/hub");
82+
request2.setEntity(requestEntity2);
83+
request2.setHeader("Authorization", "Bearer " + token);
84+
85+
HttpResponse httpResponse = Assertions
86+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(request2));
87+
88+
HttpEntity entity = httpResponse.getEntity();
89+
90+
String responseString = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entity));
91+
92+
JSONObject responseAsJson = Assertions.assertDoesNotThrow(() -> new JSONObject(responseString));
93+
94+
return Assertions.assertDoesNotThrow(() -> responseAsJson.getInt("id"));
95+
}
96+
97+
public Integer insertHostFile(final String fqHost, final String md5, final String hubFq) {
98+
HostFile host = new HostFile();
99+
host.setFqHost(fqHost);
100+
host.setMd5(md5);
101+
host.setHubFq(hubFq);
102+
103+
String json = gson.toJson(host);
104+
105+
StringEntity requestEntity = new StringEntity(String.valueOf(json), ContentType.APPLICATION_JSON);
106+
107+
HttpPut request = new HttpPut("http://localhost:" + port + "/host/file");
108+
request.setEntity(requestEntity);
109+
request.setHeader("Authorization", "Bearer " + token);
110+
111+
HttpResponse httpResponse2 = Assertions
112+
.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(request));
113+
114+
HttpEntity entity2 = httpResponse2.getEntity();
115+
116+
String responseString2 = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entity2));
117+
118+
JSONObject responseAsJson2 = Assertions.assertDoesNotThrow(() -> new JSONObject(responseString2));
119+
120+
return Assertions.assertDoesNotThrow(() -> responseAsJson2.getInt("id"));
121+
}
122+
71123
public Integer insertFlow(final String name) {
72124
Flow flow = new Flow();
73125
flow.setName(name);

0 commit comments

Comments
 (0)