Source:
public MockResponse handlePatch(String path, String s) { MockResponse response = new MockResponse(); String body = doGet(path); if (body == null) { response.setResponseCode(404); } else { try { JsonNode patch = context.getMapper().readTree(s); JsonNode source = context.getMapper().readTree(body); JsonNode updated = JsonPatch.apply(patch, source); String updatedAsString = context.getMapper().writeValueAsString(updated); AttributeSet attributeSet = null; AttributeSet query = attributeExtractor.fromPath(path); attributeSet = map.entrySet().stream() .filter(entry -> entry.getKey().matches(query)) .findFirst().get().getKey(); map.remove(attributeSet); AttributeSet newAttributeSet = AttributeSet.merge(attributeSet, attributeExtractor.fromResource(updatedAsString)); map.put(newAttributeSet, updatedAsString); final AtomicBoolean flag = new AtomicBoolean(false); AttributeSet finalAttributeSet = attributeSet; watchEventListeners.stream() .filter(watchEventsListener -> watchEventsListener.attributeMatches(finalAttributeSet)) .forEach(watchEventsListener -> { flag.set(true); watchEventsListener.sendWebSocketResponse(updatedAsString, "MODIFIED"); }); if (!flag.get()) { watchEventListeners.stream() .filter(watchEventsListener -> watchEventsListener.attributeMatches(newAttributeSet)) .forEach(watchEventsListener -> { watchEventsListener.sendWebSocketResponse(updatedAsString, "ADDED"); }); } response.setResponseCode(202); response.setBody(updatedAsString); } catch (Exception e) { throw new RuntimeException(e); } } return response; }

Target:
public MockResponse handlePatch(String path, String s) { MockResponse response = new MockResponse(); String body = fetchResource(path); if (body == null) { response.setResponseCode(HttpURLConnection.HTTP_NOT_FOUND); } else { try { JsonNode patch = context.getMapper().readTree(s); JsonNode source = context.getMapper().readTree(body); JsonNode updated = JsonPatch.apply(patch, source); String updatedAsString = context.getMapper().writeValueAsString(updated); AttributeSet attributeSet = null; AttributeSet query = attributeExtractor.fromPath(path); attributeSet = map.entrySet().stream() .filter(entry -> entry.getKey().matches(query)) .findFirst().orElseThrow(IllegalStateException::new).getKey(); map.remove(attributeSet); AttributeSet newAttributeSet = AttributeSet.merge(attributeSet, attributeExtractor.fromResource(updatedAsString)); map.put(newAttributeSet, updatedAsString); final AtomicBoolean flag = new AtomicBoolean(false); AttributeSet finalAttributeSet = attributeSet; watchEventListeners.stream() .filter(watchEventsListener -> watchEventsListener.attributeMatches(finalAttributeSet)) .forEach(watchEventsListener -> { flag.set(true); watchEventsListener.sendWebSocketResponse(updatedAsString, "MODIFIED"); }); if (!flag.get()) { watchEventListeners.stream() .filter(watchEventsListener -> watchEventsListener.attributeMatches(newAttributeSet)) .forEach(watchEventsListener -> watchEventsListener.sendWebSocketResponse(updatedAsString, "ADDED")); } response.setResponseCode(HttpURLConnection.HTTP_ACCEPTED); response.setBody(updatedAsString); } catch (JsonProcessingException e) { throw new IllegalArgumentException(e); } } return response; }