ValidateFormDataActor { public void validate(final ValidateFormDataMessage message) throws ValidateFormException { try { IObject formFields = message.getForm(); IObject clientData = message.getFormFromRequest(); IObject resultObject = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); Iterator<Map.Entry<IFieldName, Object>> fieldsIterator = formFields.iterator(); Map.Entry<IFieldName, Object> entry = fieldsIterator.next(); while (entry != null) { IFieldName key = entry.getKey(); List<IObject> rules = validationRulesF.in((IObject) formFields.getValue(key)); if (rules != null) { for (IObject ruleObject : rules) { if (!new Parser((String) ruleF.in(ruleObject), (String) clientData.getValue(key)).validate()) { throw new ValidateFormException("Fields is not correct"); } } } resultObject.setValue(key, clientData.getValue(key)); if (!fieldsIterator.hasNext()) { break; } entry = fieldsIterator.next(); } message.setFormData(resultObject); } catch (Exception e) { throw new ValidateFormException(e); } } ValidateFormDataActor(final IObject params); void validate(final ValidateFormDataMessage message);  }
@Test(expected = ValidateFormException.class) public void shouldThrowExceptionWhenFieldsIsInvalid() throws Exception { IObject serverForm = mock(IObject.class); IObject clientForm = mock(IObject.class); ValidateFormDataMessage message = mock(ValidateFormDataMessage.class); when(message.getFormFromRequest()).thenReturn(clientForm); when(message.getForm()).thenReturn(serverForm); Iterator serverIterator = mock(Iterator.class); when(serverForm.iterator()).thenReturn(serverIterator); Map.Entry entry = mock(Map.Entry.class); IFieldName fieldFieldName = mock(IFieldName.class); when(serverIterator.next()).thenReturn(entry, null); when(serverIterator.hasNext()).thenReturn(false); when(entry.getKey()).thenReturn(fieldFieldName); IObject fieldIObject = mock(IObject.class); when(serverForm.getValue(fieldFieldName)).thenReturn(fieldIObject); when(rulesF.in(fieldIObject)).thenReturn("обязательное"); when(clientForm.getValue(fieldFieldName)).thenReturn(""); IObject resultObject = mock(IObject.class); IKey iobjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iobjectKey); when(IOC.resolve(iobjectKey)).thenReturn(resultObject); actor.validate(message); }
@Test public void shouldNotThrowExceptions() throws Exception { IObject serverForm = mock(IObject.class); IObject clientForm = mock(IObject.class); ValidateFormDataMessage message = mock(ValidateFormDataMessage.class); when(message.getFormFromRequest()).thenReturn(clientForm); when(message.getForm()).thenReturn(serverForm); Iterator serverIterator = mock(Iterator.class); when(serverForm.iterator()).thenReturn(serverIterator); Map.Entry entry = mock(Map.Entry.class); IFieldName fieldFieldName = mock(IFieldName.class); when(serverIterator.next()).thenReturn(entry, null); when(serverIterator.hasNext()).thenReturn(false); when(entry.getKey()).thenReturn(fieldFieldName); IObject fieldIObject = mock(IObject.class); when(serverForm.getValue(fieldFieldName)).thenReturn(fieldIObject); List<IObject> rules = new ArrayList<>(); IObject rule = mock(IObject.class); rules.add(rule); when(rulesF.in(fieldIObject)).thenReturn(rules); when(rulesF.in(rule)).thenReturn("обязательное"); when(clientForm.getValue(fieldFieldName)).thenReturn("valueFromClient"); IObject resultObject = mock(IObject.class); IKey iobjectKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(iobjectKey); when(IOC.resolve(iobjectKey)).thenReturn(resultObject); actor.validate(message); verify(resultObject).setValue(eq(fieldFieldName), eq("valueFromClient")); verify(message).setFormData(resultObject); }