StubDescriptorEvaluator { public ResponseDescriptorDTO findResponseDescriptor(final Map<String, StubDescriptor> stubDescriptors, final WilmaHttpRequest request) { ResponseDescriptorDTO responseDescriptorDTO = null; for (StubDescriptor stubDescriptor : stubDescriptors.values()) { StubDescriptorAttributes attributes = stubDescriptor.getAttributes(); if (attributes.isActive()) { Iterator<DialogDescriptor> iterator = stubDescriptor.getDialogDescriptors().iterator(); while (iterator.hasNext() && responseDescriptorDTO == null) { DialogDescriptor dialogDescriptor = iterator.next(); responseDescriptorDTO = evaluateDialogDescriptor(request, dialogDescriptor); } } } return responseDescriptorDTO; }  ResponseDescriptorDTO findResponseDescriptor(final Map<String, StubDescriptor> stubDescriptors, final WilmaHttpRequest request);  }
@Test public void testFindResponseDescriptorWhenConditionThrowsExceptionShouldLogError() { NullPointerException nullPointerException = new NullPointerException(); given(stubDescriptor.getDialogDescriptors()).willReturn(dialogDescriptors); given(dialogDescriptor.getConditionDescriptor()).willReturn(conditionDescriptor); given(dialogDescriptor.getAttributes().getName()).willReturn(DIALOG_DESCRIPTOR_NAME); given(conditionDescriptor.getCondition()).willReturn(condition); given(conditionEvaluator.evaluate(condition, clonedRequest)).willThrow(nullPointerException); given(stackTraceConverter.getStackTraceAsString(nullPointerException)).willReturn("error"); Whitebox.setInternalState(underTest, "logger", logger); underTest.findResponseDescriptor(stubDescriptors, request); verify(logger).error(Mockito.eq("Error during condition evaluation in the dialog descriptor 'dialog-descriptor'!"), Mockito.any(NullPointerException.class)); }
@Test public void testFindResponseDescriptorWhenConditionThrowsExceptionShouldReturnRespDescDTOWithError() { NullPointerException nullPointerException = new NullPointerException(); given(stubDescriptor.getDialogDescriptors()).willReturn(dialogDescriptors); given(dialogDescriptor.getConditionDescriptor()).willReturn(conditionDescriptor); given(dialogDescriptor.getAttributes().getName()).willReturn(DIALOG_DESCRIPTOR_NAME); given(conditionDescriptor.getCondition()).willReturn(condition); given(conditionEvaluator.evaluate(condition, clonedRequest)).willThrow(nullPointerException); given(request.getBody()).willReturn(REQUEST_BODY); given(stackTraceConverter.getStackTraceAsString(nullPointerException)).willReturn("error"); given(responseDescriptorDtoFactory.createResponseDescriptorDTOWithError(dialogDescriptor, REQUEST_BODY, "error".getBytes())).willReturn( responseDescriptorDTOWithError); Whitebox.setInternalState(underTest, "logger", logger); ResponseDescriptorDTO actual = underTest.findResponseDescriptor(stubDescriptors, request); assertEquals(actual, responseDescriptorDTOWithError); }
@Test public void testFindResponseDescriptorWhenConditionIsFalseShouldReturnNull() { given(stubDescriptor.getDialogDescriptors()).willReturn(dialogDescriptors); given(dialogDescriptor.getConditionDescriptor()).willReturn(conditionDescriptor); given(conditionDescriptor.getCondition()).willReturn(condition); given(conditionEvaluator.evaluate(condition, clonedRequest)).willReturn(false); ResponseDescriptorDTO actual = underTest.findResponseDescriptor(stubDescriptors, request); assertNull(actual); }
@Test public void testFindResponseDescriptorWhenDialogDescriptorIsDisabledShouldReturnNull() { given(stubDescriptor.getDialogDescriptors()).willReturn(dialogDescriptors); given(dialogDescriptorService.isEnabled(dialogDescriptor)).willReturn(false); ResponseDescriptorDTO actual = underTest.findResponseDescriptor(stubDescriptors, request); assertNull(actual); }
@Test public void testFindResponseDescriptorShouldReturnNullBecauseTheOneStubDescriptorIsDisabled() { attributes = new StubDescriptorAttributes(DEFAULT_GROUPNAME, false); given(stubDescriptor.getAttributes()).willReturn(attributes); ResponseDescriptorDTO actual = underTest.findResponseDescriptor(stubDescriptors, request); verify(stubDescriptor, never()).getDialogDescriptors(); }
@Test public void testFindResponseDescriptorShouldNotEvaluateDialogDescriptorWhichWasEvaluatedTrueBySequenceHandling() { given(request.popEvaluationResult(dialogDescriptor)).willReturn(Boolean.TRUE); underTest.findResponseDescriptor(stubDescriptors, request); verify(conditionEvaluator, never()).evaluate(condition, clonedRequest); }
@Test public void testFindResponseDescriptorShouldNotEvaluateDialogDescriptorWhichWasEvaluatedFalseBySequenceHandling() { given(request.popEvaluationResult(dialogDescriptor)).willReturn(Boolean.FALSE); underTest.findResponseDescriptor(stubDescriptors, request); verify(conditionEvaluator, never()).evaluate(condition, clonedRequest); }