Skip to content

Commit c4903af

Browse files
committed
Tests renewed, spotless ran and capitalized CFE mentions. Fixes issue #208 .
1 parent 4f6c299 commit c4903af

8 files changed

Lines changed: 160 additions & 94 deletions

File tree

src/main/java/com/teragrep/cfe18/handlers/HubController.java

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
import java.util.List;
6969

7070
@RestController
71-
@RequestMapping(path = "host/hub")
71+
@RequestMapping(path = "v2/host/hub")
7272
@SecurityRequirement(name = "api")
7373
public class HubController {
7474

@@ -83,28 +83,45 @@ public class HubController {
8383
@Autowired
8484
HubMapper hubMapper;
8585

86-
@RequestMapping(path = "", method = RequestMethod.PUT, produces = "application/json")
86+
@RequestMapping(
87+
path = "",
88+
method = RequestMethod.PUT,
89+
produces = "application/json"
90+
)
8791
@Operation(summary = "Create new hub")
8892
@ApiResponses(value = {
89-
@ApiResponse(responseCode = "201", description = "New hub created",
90-
content = {@Content(mediaType = "application/json",
91-
schema = @Schema(implementation = Hub.class))}),
92-
@ApiResponse(responseCode = "409", description = "ID,MD5 or fqhost already exists",
93-
content = @Content),
94-
@ApiResponse(responseCode = "400", description = "Internal server error, contact admin", content = @Content)})
93+
@ApiResponse(
94+
responseCode = "201",
95+
description = "New hub created",
96+
content = {
97+
@Content(
98+
mediaType = "application/json",
99+
schema = @Schema(implementation = Hub.class)
100+
)
101+
}
102+
),
103+
@ApiResponse(
104+
responseCode = "409",
105+
description = "ID,MD5 or fqhost already exists",
106+
content = @Content
107+
),
108+
@ApiResponse(
109+
responseCode = "400",
110+
description = "Internal server error, contact admin",
111+
content = @Content
112+
)
113+
})
95114
public ResponseEntity<String> create(@RequestBody Hub newHub) {
96115
LOGGER.info("About to insert <[{}]>", newHub);
97116
try {
98-
Hub h = hubMapper.create(
99-
newHub.getFqHost(),
100-
newHub.getMd5(),
101-
newHub.getIp());
117+
Hub h = hubMapper.create(newHub.getFqHost(), newHub.getMd5(), newHub.getIp());
102118
LOGGER.debug("Values returned <[{}]>", h);
103119
JSONObject jsonObject = new JSONObject();
104120
jsonObject.put("id", h.getId());
105121
jsonObject.put("message", "New hub created");
106122
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.CREATED);
107-
} catch (RuntimeException ex) {
123+
}
124+
catch (RuntimeException ex) {
108125
LOGGER.error(ex.getMessage());
109126
JSONObject jsonErr = new JSONObject();
110127
jsonErr.put("id", newHub.getId());
@@ -123,21 +140,41 @@ public ResponseEntity<String> create(@RequestBody Hub newHub) {
123140
}
124141
}
125142

126-
@RequestMapping(path = "/{id}", method = RequestMethod.GET, produces = "application/json")
143+
@RequestMapping(
144+
path = "/{id}",
145+
method = RequestMethod.GET,
146+
produces = "application/json"
147+
)
127148
@Operation(summary = "Fetch hub")
128149
@ApiResponses(value = {
129-
@ApiResponse(responseCode = "200", description = "Hub retrieved",
130-
content = {@Content(mediaType = "application/json",
131-
schema = @Schema(implementation = Hub.class))}),
132-
@ApiResponse(responseCode = "404", description = "Hub does not exist with the given ID",
133-
content = @Content),
134-
@ApiResponse(responseCode = "400", description = "Internal server error, contact admin", content = @Content)})
135-
public ResponseEntity<?> get(@PathVariable("id") int id, @RequestParam(required = false) Integer version) {
150+
@ApiResponse(
151+
responseCode = "200",
152+
description = "Hub retrieved",
153+
content = {
154+
@Content(
155+
mediaType = "application/json",
156+
schema = @Schema(implementation = Hub.class)
157+
)
158+
}
159+
),
160+
@ApiResponse(
161+
responseCode = "404",
162+
description = "Hub does not exist with the given ID",
163+
content = @Content
164+
),
165+
@ApiResponse(
166+
responseCode = "400",
167+
description = "Internal server error, contact admin",
168+
content = @Content
169+
)
170+
})
171+
public ResponseEntity<String> get(@PathVariable("id") int id, @RequestParam(required = false) Integer version) {
136172
LOGGER.info("About to fetch <[{}]>", id);
137173
try {
138174
Hub h = hubMapper.get(id, version);
139-
return new ResponseEntity<>(h, HttpStatus.OK);
140-
} catch (RuntimeException ex) {
175+
return new ResponseEntity<>(h.toString(), HttpStatus.OK);
176+
}
177+
catch (RuntimeException ex) {
141178
LOGGER.error(ex.getMessage());
142179
JSONObject jsonErr = new JSONObject();
143180
jsonErr.put("id", id);
@@ -156,27 +193,64 @@ public ResponseEntity<?> get(@PathVariable("id") int id, @RequestParam(required
156193
}
157194
}
158195

159-
@RequestMapping(path = "", method = RequestMethod.GET, produces = "application/json")
160-
@Operation(summary = "Fetch all hubs", description = "Will return empty list if there are no hubs to fetch")
196+
@RequestMapping(
197+
path = "",
198+
method = RequestMethod.GET,
199+
produces = "application/json"
200+
)
201+
@Operation(
202+
summary = "Fetch all hubs",
203+
description = "Will return empty list if there are no hubs to fetch"
204+
)
161205
@ApiResponses(value = {
162-
@ApiResponse(responseCode = "200", description = "Successfully retrieved",
163-
content = {@Content(mediaType = "application/json",
164-
schema = @Schema(implementation = Hub.class))})})
206+
@ApiResponse(
207+
responseCode = "200",
208+
description = "Successfully retrieved",
209+
content = {
210+
@Content(
211+
mediaType = "application/json",
212+
schema = @Schema(implementation = Hub.class)
213+
)
214+
}
215+
)
216+
})
165217
public List<Hub> getAll(@RequestParam(required = false) Integer version) {
166218
return hubMapper.getAll(version);
167219
}
168220

169-
@RequestMapping(path = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
221+
@RequestMapping(
222+
path = "/{id}",
223+
method = RequestMethod.DELETE,
224+
produces = MediaType.APPLICATION_JSON_VALUE
225+
)
170226
@Operation(summary = "Delete hub")
171227
@ApiResponses(value = {
172-
@ApiResponse(responseCode = "200", description = "Hub deleted",
173-
content = {@Content(mediaType = "application/json",
174-
schema = @Schema(implementation = Hub.class))}),
175-
@ApiResponse(responseCode = "404", description = "Hub does not exist",
176-
content = @Content),
177-
@ApiResponse(responseCode = "409", description = "Hub is being used",
178-
content = @Content),
179-
@ApiResponse(responseCode = "400", description = "Internal server error, contact admin", content = @Content)})
228+
@ApiResponse(
229+
responseCode = "200",
230+
description = "Hub deleted",
231+
content = {
232+
@Content(
233+
mediaType = "application/json",
234+
schema = @Schema(implementation = Hub.class)
235+
)
236+
}
237+
),
238+
@ApiResponse(
239+
responseCode = "404",
240+
description = "Hub does not exist",
241+
content = @Content
242+
),
243+
@ApiResponse(
244+
responseCode = "409",
245+
description = "Hub is being used",
246+
content = @Content
247+
),
248+
@ApiResponse(
249+
responseCode = "400",
250+
description = "Internal server error, contact admin",
251+
content = @Content
252+
)
253+
})
180254
public ResponseEntity<String> delete(@PathVariable("id") int id) {
181255
LOGGER.info("Deleting Hub <[{}]>", id);
182256
try {
@@ -185,7 +259,8 @@ public ResponseEntity<String> delete(@PathVariable("id") int id) {
185259
j.put("id", id);
186260
j.put("message", "Hub deleted");
187261
return new ResponseEntity<>(j.toString(), HttpStatus.OK);
188-
} catch (RuntimeException ex) {
262+
}
263+
catch (RuntimeException ex) {
189264
LOGGER.error(ex.getMessage());
190265
JSONObject jsonErr = new JSONObject();
191266
jsonErr.put("id", id);
@@ -198,8 +273,9 @@ public ResponseEntity<String> delete(@PathVariable("id") int id) {
198273
if (state.equals("23000")) {
199274
jsonErr.put("message", "Is in use");
200275
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.CONFLICT);
201-
// 45000 = Custom error, row does not exist
202-
} else if (state.equals("45000")) {
276+
// 45000 = Custom error, row does not exist
277+
}
278+
else if (state.equals("45000")) {
203279
jsonErr.put("message", "Record does not exist");
204280
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.NOT_FOUND);
205281
}
@@ -208,5 +284,3 @@ public ResponseEntity<String> delete(@PathVariable("id") int id) {
208284
}
209285
}
210286
}
211-
212-

src/main/java/com/teragrep/cfe18/handlers/entities/Hub.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,7 @@ public void setIp(String ip) {
101101

102102
@Override
103103
public String toString() {
104-
return "Hub{" +
105-
"id=" + id +
106-
", hostId=" + hostId +
107-
", fqHost='" + fqHost + '\'' +
108-
", md5='" + md5 + '\'' +
109-
", ip='" + ip + '\'' +
110-
'}';
104+
return "Hub{" + "id=" + id + ", hostId=" + hostId + ", fqHost='" + fqHost + '\'' + ", md5='" + md5 + '\''
105+
+ ", ip='" + ip + '\'' + '}';
111106
}
112107
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ BEGIN
5959
END IF;
6060

6161
-- check if there are hosts using the hub before deleting
62+
-- Without this check all the hosts connected to hub are deleted. This is due to host_type_cfe delete on cascade hosts.
6263
IF ((SELECT COUNT(htc.host_id)
6364
FROM cfe_00.host_type_cfe htc
6465
WHERE htc.hub_id = input_hub_id
@@ -67,8 +68,7 @@ BEGIN
6768
INNER JOIN hubs h2 ON h.id = h2.host_id
6869
WHERE h2.id = input_hub_id)) > 0) THEN
6970
SELECT JSON_OBJECT('id', input_hub_id, 'message', 'Hosts use the hub') INTO @ha;
70-
-- Signals constraint error since schema design is reversed and allows deleting hub that hosts rely on.
71-
-- Should maybe rework schema on this part since it hides flawed design.
71+
-- Signal user error due to user not removing hosts from Hub before deleting.
7272
SIGNAL SQLSTATE '23000' SET MESSAGE_TEXT = @ha;
7373
END IF;
7474
-- select the host id before deleting hub since it's not accessible later

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@ BEGIN
5757
ELSE
5858
SET @time = tx_id;
5959
END IF;
60-
SELECT DISTINCT htc.hub_id AS id,
60+
SELECT DISTINCT h.id AS id,
6161
h2.id AS host_id,
6262
h2.fqhost AS hub_fq_host,
6363
h.ip AS ip,
6464
h2.MD5 AS md5
6565
FROM cfe_00.hubs FOR SYSTEM_TIME AS OF TRANSACTION @time h
66-
INNER JOIN location.host FOR SYSTEM_TIME AS OF TRANSACTION @time h2 ON h2.id = h.host_id
67-
INNER JOIN cfe_00.host_type_cfe FOR SYSTEM_TIME AS OF TRANSACTION @time htc ON h.id = htc.hub_id;
66+
INNER JOIN location.host FOR SYSTEM_TIME AS OF TRANSACTION @time h2 ON h2.id = h.host_id;
6867

6968

7069
END;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,36 @@ BEGIN
5858
FROM location.host h
5959
WHERE h.MD5 = md5
6060
AND h.fqhost = fqhost
61-
AND h.host_type = 'cfe') = 0) THEN
61+
AND h.host_type = 'CFE') = 0) THEN
6262

6363
INSERT INTO location.host(MD5, fqhost, host_type)
64-
VALUES (md5, fqhost, 'cfe');
64+
VALUES (md5, fqhost, 'CFE');
6565
SELECT LAST_INSERT_ID() INTO @hid;
6666
ELSE
67-
SELECT id INTO @hid FROM location.host h WHERE h.MD5 = md5 AND h.fqhost = fqhost AND h.host_type = 'cfe';
67+
SELECT id INTO @hid FROM location.host h WHERE h.MD5 = md5 AND h.fqhost = fqhost AND h.host_type = 'CFE';
6868
END IF;
6969

7070
IF ((SELECT COUNT(h.host_id)
7171
FROM cfe_00.hubs h
7272
WHERE h.host_id = @hid
7373
AND h.ip = ip
74-
AND h.host_type = 'cfe') = 0) THEN
74+
AND h.host_type = 'CFE') = 0) THEN
7575

7676
INSERT INTO cfe_00.hubs(host_id, ip, host_type)
77-
VALUES (@hid, ip, 'cfe');
77+
VALUES (@hid, ip, 'CFE');
7878
SELECT LAST_INSERT_ID() INTO @id;
7979
ELSE
80-
SELECT id INTO @id FROM cfe_00.hubs h WHERE h.host_id = @hid AND h.ip = ip AND h.host_type = 'cfe';
80+
SELECT id INTO @id FROM cfe_00.hubs h WHERE h.host_id = @hid AND h.ip = ip AND h.host_type = 'CFE';
8181
END IF;
8282

8383
IF ((SELECT COUNT(host_id)
8484
FROM cfe_00.host_type_cfe htc
8585
WHERE htc.host_id = @hid
86-
AND htc.host_type = 'cfe'
86+
AND htc.host_type = 'CFE'
8787
AND htc.hub_id = @id) = 0) THEN
8888

8989
INSERT INTO cfe_00.host_type_cfe(host_id, host_type, hub_id)
90-
VALUES (@hid, 'cfe', @id);
90+
VALUES (@hid, 'CFE', @id);
9191
END IF;
9292
COMMIT;
9393
SELECT @id AS id;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ create table hubs
5151
id int auto_increment primary key,
5252
host_id int unique not null,
5353
ip varchar(255) not null,
54-
host_type varchar(20) not null check (host_type = 'cfe'),
54+
host_type varchar(20) not null check (host_type = 'CFE'),
5555
constraint ´HubIdToHost´ foreign key (host_id, host_type) references location.host (id, host_type),
5656
start_trxid BIGINT UNSIGNED GENERATED ALWAYS AS ROW START INVISIBLE,
5757
end_trxid BIGINT UNSIGNED GENERATED ALWAYS AS ROW END INVISIBLE,
@@ -93,7 +93,7 @@ create table bundles
9393
create table host_type_cfe
9494
(
9595
host_id int not null,
96-
host_type varchar(20) not null check (host_type = 'cfe'),
96+
host_type varchar(20) not null check (host_type = 'CFE'),
9797
hub_id int not null,
9898
constraint hostTypeCfe foreign key (host_id, host_type) references location.host (id, host_type) on delete cascade,
9999
constraint hub_id_TO_hubs foreign key (hub_id) references hubs (id),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testData() {
9797
StringEntity requestEntity1 = new StringEntity(String.valueOf(json1), ContentType.APPLICATION_JSON);
9898

9999
// Creates the request
100-
HttpPut request1 = new HttpPut("http://localhost:" + port + "/host/hub");
100+
HttpPut request1 = new HttpPut("http://localhost:" + port + "/v2/host/hub");
101101
// set requestEntity to the put request
102102
request1.setEntity(requestEntity1);
103103
// Header

0 commit comments

Comments
 (0)