@Override public synchronized void record(Request request, Response response) { if (!mode.isWritable()) { throw new IllegalStateException("the tape is not writable"); } RecordedInteraction interaction = new RecordedInteraction(new Date(), recordRequest(request), recordResponse(response)); if (mode.isSequential()) { interactions.add(interaction.toYaml()); } else { int position = findMatch(request); if (position >= 0) { interactions.set(position, interaction.toYaml()); } else { interactions.add(interaction.toYaml()); } } }
@Override public boolean apply(YamlRecordedInteraction input) { return matchRule.isMatch(request, input.toImmutable().request()); } });
@Override public Response play(final Request request) { if (!mode.isReadable()) { throw new IllegalStateException("the tape is not readable"); } if (mode.isSequential()) { Integer nextIndex = orderedIndex.getAndIncrement(); RecordedInteraction nextInteraction = interactions.get(nextIndex).toImmutable(); if (nextInteraction == null) { throw new IllegalStateException(String.format("No recording found at position %s", nextIndex)); } if (!matchRule.isMatch(request, nextInteraction.request())) { throw new IllegalStateException(String.format("Request %s does not match recorded " + "request" + " %s", stringify(request), stringify(nextInteraction.request()))); } return nextInteraction.response(); } else { int position = findMatch(request); if (position < 0) { throw new IllegalStateException("no matching recording found"); } else { return interactions.get(position).toImmutable().response(); } } }
private RecordedInteraction createImmutable() { return new RecordedInteraction(recorded, request.toImmutable(), response.toImmutable()); } }
@Override public Response play(final Request request) { if (!mode.isReadable()) { throw new IllegalStateException("the tape is not readable"); } if (mode.isSequential()) { Integer nextIndex = orderedIndex.getAndIncrement(); RecordedInteraction nextInteraction = interactions.get(nextIndex).toImmutable(); if (nextInteraction == null) { throw new IllegalStateException(String.format("No recording found at position %s", nextIndex)); } if (!matchRule.isMatch(request, nextInteraction.request())) { throw new IllegalStateException(String.format("Request %s does not match recorded " + "request" + " %s", stringify(request), stringify(nextInteraction.request()))); } return nextInteraction.response(); } else { int position = findMatch(request); if (position < 0) { throw new IllegalStateException("no matching recording found"); } else { return interactions.get(position).toImmutable().response(); } } }
RecordedInteraction toImmutable() { return new RecordedInteraction(recorded, request.toImmutable(), response.toImmutable()); } }
@Override public synchronized void record(Request request, Response response) { if (!mode.isWritable()) { throw new IllegalStateException("the tape is not writable"); } RecordedInteraction interaction = new RecordedInteraction(new Date(), recordRequest(request), recordResponse(response)); if (mode.isSequential()) { interactions.add(interaction.toYaml()); } else { int position = findMatch(request); if (position >= 0) { interactions.set(position, interaction.toYaml()); } else { interactions.add(interaction.toYaml()); } } }
@Override public boolean apply(YamlRecordedInteraction input) { return matchRule.isMatch(request, input.toImmutable().request()); } });
@Override public boolean seek(Request request) { if (isSequential()) { try { // TODO: it's a complete waste of time using an AtomicInteger when this method is called // before play in a non-transactional way Integer index = orderedIndex.get(); RecordedInteraction interaction = interactions.get(index).toImmutable(); Request nextRequest = interaction == null ? null : interaction.request(); return nextRequest != null && matchRule.isMatch(request, nextRequest); } catch (IndexOutOfBoundsException e) { throw new NonWritableTapeException(); } } else { return findMatch(request) >= 0; } }
@Override public boolean seek(Request request) { if (isSequential()) { try { // TODO: it's a complete waste of time using an AtomicInteger when this method is called // before play in a non-transactional way Integer index = orderedIndex.get(); RecordedInteraction interaction = interactions.get(index).toImmutable(); Request nextRequest = interaction == null ? null : interaction.request(); return nextRequest != null && matchRule.isMatch(request, nextRequest); } catch (IndexOutOfBoundsException e) { throw new NonWritableTapeException(); } } else { return findMatch(request) >= 0; } }