SequenceService { public void checkRequest(final WilmaHttpRequest request, final SequenceDescriptor sequenceDescriptor) { if (sequenceEvaluator.evaluate(request, sequenceDescriptor)) { SequenceHandler handler = sequenceDescriptor.getHandler(); String sequenceKey = handler.getExistingSequence(request, sequenceDescriptor.getSequences(), sequenceDescriptor.getParameters()); if (sequenceKey == null) { sequenceKey = handler.generateNewSequenceKey(request, sequenceDescriptor.getParameters()); handlerKeyValidator.validateGeneratedKey(sequenceKey, handler.getClass().getName()); WilmaSequence sequence = sequenceFactory.createNewSequence(sequenceKey, request, sequenceDescriptor.getDefaultTimeout()); sequenceDescriptor.putIntoSequences(sequence); } else { appendSequence(sequenceKey, request, sequenceDescriptor); } String sequenceId = sequenceIdUtil.createSequenceId(sequenceKey, sequenceDescriptor); appendSequenceKeyToHeader(sequenceId, request); } }  void checkRequest(final WilmaHttpRequest request, final SequenceDescriptor sequenceDescriptor);  }
@Test public void testCheckRequestWhenEvaluationFalse() { String sequenceKeyFirst = "testKey1"; given(sequenceEvaluator.evaluate(request, sequenceDescriptor)).willReturn(false); underTest.checkRequest(request, sequenceDescriptor); verify(request, never()).addSequenceId(sequenceKeyFirst); }
@Test public void testCheckRequestShouldAppendRequestToAnExistingSequence() { String sequenceKeyFirst = "testKey1"; Map<String, WilmaSequence> sequences = new ConcurrentHashMap<>(); sequences.put(sequenceKeyFirst, sequenceFirst); given(sequenceEvaluator.evaluate(request, sequenceDescriptor)).willReturn(true); given(sequenceDescriptor.getSequences()).willReturn(sequences); given(handler.getExistingSequence(request, sequences, null)).willReturn(sequenceKeyFirst); given(sequenceDescriptor.getSequence(sequenceKeyFirst)).willReturn(sequenceFirst); given(request.getWilmaMessageId()).willReturn("TestLoggerId"); given(sequenceIdUtil.createSequenceId(sequenceKeyFirst, sequenceDescriptor)).willReturn("newID"); given(request.getSequenceId()).willReturn(null); given(headerUtil.createSequenceHeader(null, "newID")).willReturn("newID"); underTest.checkRequest(request, sequenceDescriptor); verify(sequenceFirst).setTimeout(Mockito.any(Timestamp.class)); verify(sequenceFirst).addPair(Mockito.eq("TestLoggerId"), Mockito.any(RequestResponsePair.class)); verify(request).addSequenceId("newID"); }
@Test public void testCheckRequestShouldCreateANewSequence() { String sequenceKeyFirst = "testKey1"; String sequenceKeySecond = "testKey2"; Map<String, WilmaSequence> sequences = new ConcurrentHashMap<>(); sequences.put(sequenceKeyFirst, sequenceFirst); given(sequenceEvaluator.evaluate(request, sequenceDescriptor)).willReturn(true); given(handler.getExistingSequence(request, sequences, null)).willReturn(null); given(handler.generateNewSequenceKey(request, null)).willReturn(sequenceKeySecond); given(sequenceDescriptor.getDefaultTimeout()).willReturn(1000L); given(sequenceFactory.createNewSequence(sequenceKeySecond, request, 1000L)).willReturn(sequenceSecond); given(sequenceSecond.getSequenceKey()).willReturn(sequenceKeySecond); given(sequenceIdUtil.createSequenceId(sequenceKeySecond, sequenceDescriptor)).willReturn("newID"); given(request.getSequenceId()).willReturn(null); given(headerUtil.createSequenceHeader(null, "newID")).willReturn("newID"); underTest.checkRequest(request, sequenceDescriptor); verify(sequenceDescriptor).putIntoSequences(sequenceSecond); verify(request).addSequenceId("newID"); }
@Test public void testCheckRequestWhenTheMethodShouldConcatANewSequenceKeyToAnExistingHeader() { String sequenceKeyFirst = "testKey1"; String sequenceKeySecond = "testKey2"; Map<String, WilmaSequence> sequences = new ConcurrentHashMap<>(); sequences.put(sequenceKeyFirst, sequenceFirst); given(sequenceEvaluator.evaluate(request, sequenceDescriptor)).willReturn(true); given(sequenceDescriptor.getHandler()).willReturn(handler); given(handler.getExistingSequence(request, sequences, null)).willReturn(null); given(handler.generateNewSequenceKey(request, null)).willReturn(sequenceKeySecond); given(sequenceDescriptor.getDefaultTimeout()).willReturn(1000L); given(sequenceFactory.createNewSequence(sequenceKeySecond, request, 1000L)).willReturn(sequenceSecond); given(sequenceIdUtil.createSequenceId(sequenceKeySecond, sequenceDescriptor)).willReturn("newID"); given(request.getSequenceId()).willReturn(sequenceKeyFirst); given(headerUtil.createSequenceHeader(sequenceKeyFirst, "newID")).willReturn("TwoKey"); underTest.checkRequest(request, sequenceDescriptor); verify(sequenceDescriptor).putIntoSequences(sequenceSecond); verify(request).addSequenceId("TwoKey"); }