From 444f864e6bf61e72b63639abf3afd2714ab37275 Mon Sep 17 00:00:00 2001 From: Alexander Zagniotov Date: Wed, 3 Mar 2021 23:34:05 +0900 Subject: [PATCH] Disabled internal StubHttpLifecycleCache due to issue #170 until the caching solution will be revisited (#176) --- .../azagniotov/stubby4j/StubsPortalTest.java | 39 ++++++++++++++++- src/functional-test/resources/yaml/stubs.yaml | 42 +++++++++++++++++++ .../stubby4j/stubs/StubRepository.java | 36 ++++------------ 3 files changed, 88 insertions(+), 29 deletions(-) diff --git a/src/functional-test/java/io/github/azagniotov/stubby4j/StubsPortalTest.java b/src/functional-test/java/io/github/azagniotov/stubby4j/StubsPortalTest.java index 55cfb2e8d..3860f1503 100644 --- a/src/functional-test/java/io/github/azagniotov/stubby4j/StubsPortalTest.java +++ b/src/functional-test/java/io/github/azagniotov/stubby4j/StubsPortalTest.java @@ -1108,7 +1108,7 @@ public void shouldMatchDistinctRequestBodiesForTheSameUrlWhenPutRequestMade() th } @Test - public void should_SuccessfullyMatchPostRegex_WhenPostRequestMade() throws Exception { + public void stubby4jIssue171() throws Exception { final String requestUrl = String.format("%s%s", STUBS_URL, "/azagniotov/stubby4j/issues/171"); @@ -1130,7 +1130,7 @@ public void should_SuccessfullyMatchPostRegex_WhenPostRequestMade() throws Excep } @Test - public void should_NotMatchPostRegex_WhenPostRequestMade_WithWrongPayload() throws Exception { + public void stubby4jIssue171_WithWrongPayload() throws Exception { final String requestUrl = String.format("%s%s", STUBS_URL, "/azagniotov/stubby4j/issues/171"); @@ -1146,4 +1146,39 @@ public void should_NotMatchPostRegex_WhenPostRequestMade_WithWrongPayload() thro final HttpResponse response = request.execute(); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND_404); } + + @Test + public void stubby4jIssue170() throws Exception { + + final String requestUrl = String.format("%s%s", STUBS_URL, "/azagniotov/stubby4j/issues/170"); + final HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(HEADER_APPLICATION_JSON); + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Making request#1 which matches rule_3 ONLY, this will cache stub for rule_3 by the above requestUrl + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + final String contentOne = "{\"rule\":\"rule_3\",\"request_id\":\"rule_3_request_id\"}"; + final HttpRequest requestOne = HttpUtils.constructHttpRequest(HttpMethods.POST, requestUrl, contentOne); + + requestOne.setHeaders(httpHeaders); + final HttpResponse responseOne = requestOne.execute(); + final String responseOneContentAsString = responseOne.parseAsString().trim(); + + assertThat(responseOne.getStatusCode()).isEqualTo(HttpStatus.CREATED_201); + assertThat("rule_3").isEqualTo(responseOneContentAsString); + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Making request#2 which matches rule_1 AND rule_3. But, in this case, + // we are expecting rule_1 as a response, because the rule_1 is defined earlier than rule_3 + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + final String contentTwo = "{\"rule\":\"rule_1\",\"request_id\":\"rule_1_request_id\"}"; + final HttpRequest requestTwo = HttpUtils.constructHttpRequest(HttpMethods.POST, requestUrl, contentTwo); + + requestTwo.setHeaders(httpHeaders); + final HttpResponse responseTwo = requestTwo.execute(); + final String responseTwoContentAsString = responseTwo.parseAsString().trim(); + + assertThat(responseTwo.getStatusCode()).isEqualTo(HttpStatus.CREATED_201); + assertThat("rule_1").isEqualTo(responseTwoContentAsString); + } } diff --git a/src/functional-test/resources/yaml/stubs.yaml b/src/functional-test/resources/yaml/stubs.yaml index 4a5569b6c..03cfc4221 100644 --- a/src/functional-test/resources/yaml/stubs.yaml +++ b/src/functional-test/resources/yaml/stubs.yaml @@ -803,3 +803,45 @@ headers: content-type: application/json status: 201 + + +- description: rule_1 + request: + url: /azagniotov/stubby4j/issues/170 + method: POST + headers: + content-type: application/json + post: > + {"rule":"rule_1","request_id":"rule_1_request_id"} + + response: + body: rule_1 + status: 201 + + +- description: rule_2 + request: + url: /azagniotov/stubby4j/issues/170 + method: POST + headers: + content-type: application/json + post: > + {"rule":"rule_2","request_id":"rule_2_request_id"} + + response: + body: rule_2 + status: 201 + + +- description: rule_3 + request: + url: /azagniotov/stubby4j/issues/170 + method: POST + headers: + content-type: application/json + post: > + {"rule":"(.*)","request_id":"(.*)"} + + response: + body: rule_3 + status: 201 diff --git a/src/main/java/io/github/azagniotov/stubby4j/stubs/StubRepository.java b/src/main/java/io/github/azagniotov/stubby4j/stubs/StubRepository.java index 89dd60d15..c3bd1f29d 100644 --- a/src/main/java/io/github/azagniotov/stubby4j/stubs/StubRepository.java +++ b/src/main/java/io/github/azagniotov/stubby4j/stubs/StubRepository.java @@ -182,32 +182,10 @@ private synchronized Optional matchStub(final StubHttpLifecyc final long initialStart = System.currentTimeMillis(); final String incomingRequestUrl = incomingStub.getUrl(); - final Optional cachedMatchCandidateOptional = stubMatchesCache.get(incomingRequestUrl); - - return cachedMatchCandidateOptional.map(cachedMatchCandidate -> { - - ANSITerminal.loaded(String.format("Local cache contains potential match for the URL [%s]", incomingRequestUrl)); - LOGGER.debug("Local cache contains potential match for the URL [{}].", incomingRequestUrl); - - // The order(?) in which equality is determined is important here (what object is "equal to" the other one) - if (incomingStub.equals(cachedMatchCandidate)) { - final long elapsed = System.currentTimeMillis() - initialStart; - logMatch(elapsed, cachedMatchCandidate); - ANSITerminal.loaded(String.format("Potential match for the URL [%s] was deemed as a full match", incomingRequestUrl)); - LOGGER.debug("Potential match for the URL [{}] was deemed as a full match.", incomingRequestUrl); - - return Optional.of(cachedMatchCandidate); - } - - ANSITerminal.warn(String.format("Cached match for the URL [%s] failed to match fully, invalidating match cache..", incomingRequestUrl)); - LOGGER.warn("Cached match for the URL [{}] failed to match fully, invalidating match cache.", incomingRequestUrl); - - stubMatchesCache.clearByKey(incomingRequestUrl); - - // Since we did not find match with cached match candidate, then: - return matchAll(incomingStub, initialStart, incomingRequestUrl); - - }).orElseGet(() -> matchAll(incomingStub, initialStart, incomingRequestUrl)); + // TODO Caching related behavior is disabled by https://github.com/azagniotov/stubby4j/pull/176 + // due to https://github.com/azagniotov/stubby4j/issues/170 until a more viable way to use + // the cache for matching optimization is identified + return matchAll(incomingStub, initialStart, incomingRequestUrl); } private Optional matchAll(final StubHttpLifecycle incomingStub, final long initialStart, final String incomingRequestUrl) { @@ -218,7 +196,11 @@ private Optional matchAll(final StubHttpLifecycle incomingStu ANSITerminal.status(String.format("Caching the found match for URL [%s]", incomingRequestUrl)); LOGGER.debug("Caching the found match for URL [{}].", incomingRequestUrl); - stubMatchesCache.putIfAbsent(incomingRequestUrl, stubbed); + + // TODO Caching related behavior is disabled by https://github.com/azagniotov/stubby4j/pull/176 + // due to https://github.com/azagniotov/stubby4j/issues/170 until a more viable way to use + // the cache for matching optimization is identified + // stubMatchesCache.putIfAbsent(incomingRequestUrl, stubbed); return Optional.of(stubbed); }