From 8e0d31651f81e3a381a26bb906d4c52e84980c19 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Tue, 28 May 2024 00:12:28 +0200 Subject: [PATCH 1/3] set header content-type from constants less error-prone --- .../study/server/ShortCircuitTest.java | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java b/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java index 5fff2d40ff..6dd8af7017 100644 --- a/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java +++ b/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java @@ -53,6 +53,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.stream.binder.test.InputDestination; import org.springframework.cloud.stream.binder.test.OutputDestination; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; @@ -198,7 +199,7 @@ public MockResponse dispatch(RecordedRequest request) { .build(), shortCircuitAnalysisResultDestination); return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisResultUuidStr) - .addHeader("Content-Type", "application/json; charset=utf-8"); + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2)) { input.send(MessageBuilder.withPayload("") .setHeader("resultUuid", SHORT_CIRCUIT_ANALYSIS_RESULT_UUID) @@ -206,7 +207,7 @@ public MockResponse dispatch(RecordedRequest request) { .build(), shortCircuitAnalysisResultDestination); return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisResultUuidStr) - .addHeader("Content-Type", "application/json; charset=utf-8"); + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/networks/" + NETWORK_UUID_STRING_NOT_FOUND + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_4)) { input.send(MessageBuilder.withPayload("") .setHeader("resultUuid", SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND) @@ -214,37 +215,40 @@ public MockResponse dispatch(RecordedRequest request) { .build(), shortCircuitAnalysisResultDestination); return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisResultNotFoundUuidStr) - .addHeader("Content-Type", "application/json; charset=utf-8"); + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID)) { input.send(MessageBuilder.withPayload("") .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") .build(), shortCircuitAnalysisFailedDestination); return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisErrorResultUuidStr) - .addHeader("Content-Type", "application/json; charset=utf-8"); + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "\\?mode=FULL")) { - return new MockResponse().setResponseCode(200).setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) - .addHeader("Content-Type", "application/json; charset=utf-8"); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault-types")) { return new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json; charset=utf-8"); + .setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); + } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault-types")) { + return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault_results/paged" + "\\?mode=FULL&page=0&size=20&sort=id,DESC")) { - return new MockResponse().setResponseCode(200).setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) - .addHeader("Content-Type", "application/json; charset=utf-8"); + return new MockResponse().setResponseCode(200) + .setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/csv")) { - return new MockResponse().setResponseCode(200).setBody(getBinaryAsBuffer(SHORT_CIRCUIT_ANALYSIS_CSV_RESULT)) - .addHeader("Content-Type", "application/json; charset=utf-8"); + return new MockResponse().setResponseCode(200) + .setBody(getBinaryAsBuffer(SHORT_CIRCUIT_ANALYSIS_CSV_RESULT)) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND + "/csv")) { return new MockResponse().setResponseCode(404); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/feeder_results/paged" + "\\?mode=FULL&filters=fakeFilters&page=0&size=20&sort=id,DESC")) { - return new MockResponse().setResponseCode(200).setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) - .addHeader("Content-Type", "application/json; charset=utf-8"); + return new MockResponse().setResponseCode(200) + .setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status")) { - return new MockResponse().setResponseCode(200).setBody(SHORT_CIRCUIT_ANALYSIS_STATUS_JSON) - .addHeader("Content-Type", "application/json; charset=utf-8"); - } else if (path.matches("/v1/results/invalidate-status\\?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID)) { return new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json; charset=utf-8"); + .setBody(SHORT_CIRCUIT_ANALYSIS_STATUS_JSON) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); + } else if (path.matches("/v1/results/invalidate-status\\?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID)) { + return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop.*") || path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID + "/stop.*")) { String resultUuid = path.matches(".*variantId=" + VARIANT_ID_2 + ".*") ? SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID : SHORT_CIRCUIT_ANALYSIS_RESULT_UUID; @@ -252,21 +256,18 @@ public MockResponse dispatch(RecordedRequest request) { .setHeader("resultUuid", resultUuid) .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") .build(), shortCircuitAnalysisStoppedDestination); - return new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json; charset=utf-8"); + return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/results")) { - return new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json; charset=utf-8"); + return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/treereports")) { - return new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json; charset=utf-8"); + return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/supervision/results-count")) { - return new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody("1"); + return new MockResponse().setResponseCode(200).setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE).setBody("1"); } else { - LOGGER.error("Unhandled method+path: " + request.getMethod() + " " + request.getPath()); - return new MockResponse().setResponseCode(418).setBody("Unhandled method+path: " + request.getMethod() + " " + request.getPath()); + LOGGER.error("Unhandled method+path: {} {}", request.getMethod(), request.getPath()); + return new MockResponse().setResponseCode(418) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE) + .setBody("Unhandled method+path: " + request.getMethod() + " " + request.getPath()); } } From 4597939cedb0e12c8d38e9792ac70c807ccf4d09 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Thu, 23 May 2024 18:10:47 +0200 Subject: [PATCH 2/3] simplify matchers use `equals()` instead of `matches()` for simple matches --- .../study/server/ShortCircuitTest.java | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java b/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java index 6dd8af7017..edbbf2c625 100644 --- a/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java +++ b/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java @@ -223,31 +223,31 @@ public MockResponse dispatch(RecordedRequest request) { return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisErrorResultUuidStr) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "\\?mode=FULL")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "?mode=FULL")) { return new MockResponse().setResponseCode(200) .setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault-types")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault-types")) { return new MockResponse().setResponseCode(200); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault_results/paged" + "\\?mode=FULL&page=0&size=20&sort=id,DESC")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault_results/paged?mode=FULL&page=0&size=20&sort=id,DESC")) { return new MockResponse().setResponseCode(200) .setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/csv")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/csv")) { return new MockResponse().setResponseCode(200) .setBody(getBinaryAsBuffer(SHORT_CIRCUIT_ANALYSIS_CSV_RESULT)) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND + "/csv")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND + "/csv")) { return new MockResponse().setResponseCode(404); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/feeder_results/paged" + "\\?mode=FULL&filters=fakeFilters&page=0&size=20&sort=id,DESC")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/feeder_results/paged?mode=FULL&filters=fakeFilters&page=0&size=20&sort=id,DESC")) { return new MockResponse().setResponseCode(200) .setBody(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status")) { + } else if (path.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status")) { return new MockResponse().setResponseCode(200) .setBody(SHORT_CIRCUIT_ANALYSIS_STATUS_JSON) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/results/invalidate-status\\?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID)) { + } else if (path.equals("/v1/results/invalidate-status?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID)) { return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop.*") || path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID + "/stop.*")) { @@ -257,11 +257,11 @@ public MockResponse dispatch(RecordedRequest request) { .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") .build(), shortCircuitAnalysisStoppedDestination); return new MockResponse().setResponseCode(200); - } else if (path.matches("/v1/results")) { + } else if (path.equals("/v1/results")) { return new MockResponse().setResponseCode(200); - } else if (path.matches("/v1/treereports")) { + } else if (path.equals("/v1/treereports")) { return new MockResponse().setResponseCode(200); - } else if (path.matches("/v1/supervision/results-count")) { + } else if (path.equals("/v1/supervision/results-count")) { return new MockResponse().setResponseCode(200).setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE).setBody("1"); } else { LOGGER.error("Unhandled method+path: {} {}", request.getMethod(), request.getPath()); @@ -375,13 +375,13 @@ public void testAllBusesShortCircuit() throws Exception { status().isOk(), content().string(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON)); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "\\?mode=FULL"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "?mode=FULL"))); // export short circuit analysis csv result mockMvc.perform(post("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result/csv", studyNameUserIdUuid, modificationNode3Uuid) .param("type", "ALL_BUSES") .content(CSV_HEADERS)).andExpectAll(status().isOk(), content().bytes(SHORT_CIRCUIT_ANALYSIS_CSV_RESULT)); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/csv"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/csv"))); // get short circuit result but with unknown node mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result", studyNameUserIdUuid, unknownModificationNodeUuid)).andExpect( @@ -394,7 +394,7 @@ public void testAllBusesShortCircuit() throws Exception { status().isOk(), content().string(SHORT_CIRCUIT_ANALYSIS_STATUS_JSON)); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status"))); // stop short circuit analysis mockMvc.perform(put("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/stop", studyNameUserIdUuid, modificationNode3Uuid)).andExpect(status().isOk()); @@ -420,7 +420,7 @@ public void testAllBusesShortCircuit() throws Exception { .queryParam("type", String.valueOf(ComputationType.SHORT_CIRCUIT)) .queryParam("dryRun", String.valueOf(true))) .andExpect(status().isOk()); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/supervision/results-count"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/supervision/results-count"))); // Delete Shortcircuit results // In short-circuit server there is no distinction between 1-bus and all-buses, so we remove all kinds of short-circuit @@ -432,7 +432,7 @@ public void testAllBusesShortCircuit() throws Exception { var requests = TestUtils.getRequestsDone(2, server); assertTrue(requests.contains("/v1/results")); - assertTrue(requests.stream().anyMatch(r -> r.matches("/v1/treereports"))); + assertTrue(requests.stream().anyMatch(r -> r.equals("/v1/treereports"))); assertEquals(0, networkModificationNodeInfoRepository.findAllByShortCircuitAnalysisResultUuidNotNull().size()); } @@ -476,7 +476,7 @@ public void testGetShortCircuitAnalysisCsvResultNotFound() throws Exception { mockMvc.perform(post("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result/csv", studyNameUserIdUuid, modificationNode4Uuid) .param("type", "ALL_BUSES") .content(CSV_HEADERS)).andExpectAll(status().isNotFound()); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND + "/csv"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND + "/csv"))); } @Test @@ -509,14 +509,14 @@ public void testPagedShortCircuit() throws Exception { studyNameUserIdUuid, modificationNode1Uuid, ComputationType.SHORT_CIRCUIT, "fault-types")) .andExpectAll(status().isOk()); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault-types"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault-types"))); // get short circuit result with pagination mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result?paged=true&page=0&size=20&sort=id,DESC", studyNameUserIdUuid, modificationNode1Uuid)).andExpectAll( status().isOk(), content().string(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON)); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault_results/paged\\?mode=FULL&page=0&size=20&sort=id,DESC"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/fault_results/paged?mode=FULL&page=0&size=20&sort=id,DESC"))); // get short circuit result with pagination but with unknown node mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result?paged=true&page=0&size=20", studyNameUserIdUuid, unknownModificationNodeUuid)).andExpect( @@ -529,7 +529,7 @@ public void testPagedShortCircuit() throws Exception { status().isOk(), content().string(SHORT_CIRCUIT_ANALYSIS_STATUS_JSON)); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status"))); // stop short circuit analysis mockMvc.perform(put("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/stop", studyNameUserIdUuid, modificationNode1Uuid)).andExpect(status().isOk()); @@ -590,7 +590,7 @@ public void testOneBusShortCircuit() throws Exception { content().string(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON) ); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "\\?mode=FULL"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "?mode=FULL"))); // get short circuit result with pagination mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result?paged=true&page=0&size=20&sort=id,DESC&filters=fakeFilters", studyNameUserIdUuid, modificationNode3Uuid) @@ -599,7 +599,7 @@ public void testOneBusShortCircuit() throws Exception { status().isOk(), content().string(SHORT_CIRCUIT_ANALYSIS_RESULT_JSON)); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/feeder_results/paged\\?mode=FULL&filters=fakeFilters&page=0&size=20&sort=id,DESC"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/feeder_results/paged?mode=FULL&filters=fakeFilters&page=0&size=20&sort=id,DESC"))); // get one bus short circuit status mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/status", studyNameUserIdUuid, modificationNode3Uuid) @@ -609,14 +609,14 @@ public void testOneBusShortCircuit() throws Exception { content().string(SHORT_CIRCUIT_ANALYSIS_STATUS_JSON) ); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/status"))); //Test result count mockMvc.perform(delete("/v1/supervision/computation/results") .queryParam("type", String.valueOf(ComputationType.SHORT_CIRCUIT)) .queryParam("dryRun", String.valueOf(true))) .andExpect(status().isOk()); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/supervision/results-count"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/supervision/results-count"))); // Delete Shortcircuit results mockMvc.perform(delete("/v1/supervision/computation/results") @@ -626,7 +626,7 @@ public void testOneBusShortCircuit() throws Exception { var requests = TestUtils.getRequestsDone(2, server); assertTrue(requests.contains("/v1/results")); - assertTrue(requests.stream().anyMatch(r -> r.matches("/v1/treereports"))); + assertTrue(requests.stream().anyMatch(r -> r.equals("/v1/treereports"))); assertEquals(0, networkModificationNodeInfoRepository.findAllByOneBusShortCircuitAnalysisResultUuidNotNull().size()); } @@ -758,7 +758,7 @@ public void testInvalidateShortCircuitStatus() throws Exception { .header("userId", "userId")).andExpect(status().isOk()); checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS); checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_ONE_BUS_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/invalidate-status\\?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.equals("/v1/results/invalidate-status?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID))); } From b60ea941533bddea852b1bba945a47de2364490c Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Tue, 28 May 2024 00:13:20 +0200 Subject: [PATCH 3/3] fix matchers match full path & fix regex syntax --- .../study/server/ShortCircuitTest.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java b/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java index edbbf2c625..4f73a69f60 100644 --- a/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java +++ b/src/test/java/org/gridsuite/study/server/ShortCircuitTest.java @@ -190,8 +190,7 @@ public void setup() throws IOException { @NotNull public MockResponse dispatch(RecordedRequest request) { String path = Objects.requireNonNull(request.getPath()); - request.getBody(); - if (path.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&busId=BUS_TEST_ID&variantId=" + VARIANT_ID_2)) { + if (path.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&busId=BUS_TEST_ID&variantId=" + VARIANT_ID_2 + "$")) { input.send(MessageBuilder.withPayload("") .setHeader("resultUuid", SHORT_CIRCUIT_ANALYSIS_RESULT_UUID) .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") @@ -200,7 +199,7 @@ public MockResponse dispatch(RecordedRequest request) { return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisResultUuidStr) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2)) { + } else if (path.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_2 + "$")) { input.send(MessageBuilder.withPayload("") .setHeader("resultUuid", SHORT_CIRCUIT_ANALYSIS_RESULT_UUID) .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") @@ -208,7 +207,7 @@ public MockResponse dispatch(RecordedRequest request) { return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisResultUuidStr) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/networks/" + NETWORK_UUID_STRING_NOT_FOUND + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_4)) { + } else if (path.matches("^/v1/networks/" + NETWORK_UUID_STRING_NOT_FOUND + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_4 + "$")) { input.send(MessageBuilder.withPayload("") .setHeader("resultUuid", SHORT_CIRCUIT_ANALYSIS_RESULT_UUID_NOT_FOUND) .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") @@ -216,7 +215,7 @@ public MockResponse dispatch(RecordedRequest request) { return new MockResponse().setResponseCode(200) .setBody(shortCircuitAnalysisResultNotFoundUuidStr) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); - } else if (path.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID)) { + } else if (path.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID + "$")) { input.send(MessageBuilder.withPayload("") .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") .build(), shortCircuitAnalysisFailedDestination); @@ -249,11 +248,10 @@ public MockResponse dispatch(RecordedRequest request) { .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE); } else if (path.equals("/v1/results/invalidate-status?resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "&resultUuid=" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID)) { return new MockResponse().setResponseCode(200); - } else if (path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop.*") - || path.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID + "/stop.*")) { - String resultUuid = path.matches(".*variantId=" + VARIANT_ID_2 + ".*") ? SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID : SHORT_CIRCUIT_ANALYSIS_RESULT_UUID; + } else if (path.matches("^/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop.+$") + || path.matches("^/v1/results/" + SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID + "/stop.+$")) { input.send(MessageBuilder.withPayload("") - .setHeader("resultUuid", resultUuid) + .setHeader("resultUuid", path.matches("[?&]variantId=" + VARIANT_ID_2) ? SHORT_CIRCUIT_ANALYSIS_OTHER_NODE_RESULT_UUID : SHORT_CIRCUIT_ANALYSIS_RESULT_UUID) .setHeader("receiver", "%7B%22nodeUuid%22%3A%22" + request.getPath().split("%")[5].substring(4) + "%22%2C%22userId%22%3A%22userId%22%7D") .build(), shortCircuitAnalysisStoppedDestination); return new MockResponse().setResponseCode(200); @@ -367,7 +365,7 @@ public void testAllBusesShortCircuit() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_2 + "$"))); // get short circuit result mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/result", studyNameUserIdUuid, modificationNode3Uuid)) @@ -401,7 +399,7 @@ public void testAllBusesShortCircuit() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_RESULT); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop\\?receiver=.*nodeUuid.*"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop\\?receiver=.+nodeUuid.+$"))); // short circuit analysis failed mockMvc.perform(put("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/run", studyNameUserIdUuid, modificationNode2Uuid) @@ -412,7 +410,7 @@ public void testAllBusesShortCircuit() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID + "$"))); // Test result count // In short-circuit server there is no distinction between 1-bus and all-buses, so the count will return all kinds of short-circuit @@ -469,7 +467,7 @@ public void testGetShortCircuitAnalysisCsvResultNotFound() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING_NOT_FOUND + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_4))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING_NOT_FOUND + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_4 + "$"))); // export short circuit analysis csv result not found @@ -502,7 +500,7 @@ public void testPagedShortCircuit() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_2 + "$"))); // get fault types mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/computation/result/enum-values?computingType={computingType}&enumName={enumName}", @@ -536,7 +534,7 @@ public void testPagedShortCircuit() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_RESULT); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop\\?receiver=.*nodeUuid.*"))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/results/" + SHORT_CIRCUIT_ANALYSIS_RESULT_UUID + "/stop\\?receiver=.+nodeUuid.+$"))); } @Test @@ -578,7 +576,7 @@ public void testOneBusShortCircuit() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_ONE_BUS_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_2 + "$"))); assertEquals(1, networkModificationNodeInfoRepository.findAllByOneBusShortCircuitAnalysisResultUuidNotNull().size()); @@ -736,7 +734,7 @@ public void testInvalidateShortCircuitStatus() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_RESULT); checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_2 + "$"))); //run a one bus short circuit analysis mockMvc.perform(put("/v1/studies/{studyUuid}/nodes/{nodeUuid}/shortcircuit/run", studyNameUserIdUuid, modificationNode3Uuid) @@ -751,7 +749,7 @@ public void testInvalidateShortCircuitStatus() throws Exception { checkUpdateModelStatusMessagesReceived(studyNameUserIdUuid, NotificationService.UPDATE_TYPE_ONE_BUS_SHORT_CIRCUIT_STATUS); - assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.*&reportUuid=.*&reporterId=.*&variantId=" + VARIANT_ID_2))); + assertTrue(TestUtils.getRequestsDone(1, server).stream().anyMatch(r -> r.matches("^/v1/networks/" + NETWORK_UUID_STRING + "/run-and-save\\?receiver=.+&reportUuid=.+&reporterId=.+&variantId=" + VARIANT_ID_2 + "$"))); // invalidate status mockMvc.perform(put("/v1/studies/{studyUuid}/short-circuit/invalidate-status", studyNameUserIdUuid)