ChainReferenceValidator implements ConstraintValidator<ValidChainReference, ProcessorReferenceChainDescription> { @Override public boolean isValid(ProcessorReferenceChainDescription chainReference, ConstraintValidatorContext context) { if (chainReference == null) return true; List<ProcessorReference> references = chainReference.getProcessors(); if (references == null || references.isEmpty()) return setAndReturnErrorMessage(context, "processor chain cannot be empty"); if (!specificationManager.getByName(references.get(0).getName()).getType().equals(ProcessorType.SOURCE_ADAPTER)) return setAndReturnErrorMessage(context, "processor chain must start with " + ProcessorType.SOURCE_ADAPTER); ProcessorReference lastReference = null; for (ProcessorReference reference : references) { if (lastReference != null) { ProcessorType lastType = specificationManager.getByName(lastReference.getName()).getType(); ProcessorType nextType = specificationManager.getByName(reference.getName()).getType(); if (!lastType.isValidNextFilter(nextType)) return setAndReturnErrorMessage(context, lastType + " cannot be followed by " + nextType); } if (!isValid(reference, context)) return false; lastReference = reference; } return true; } @Inject  ChainReferenceValidator(SpecificationManager specificationManager); @Override void initialize(ValidChainReference validReference); @Override boolean isValid(ProcessorReferenceChainDescription chainReference, ConstraintValidatorContext context);  }
@Test public void testValidReference() { new Expectations() {{ Map<String, Class<?>> argTypes = new HashMap<>(); argTypes.put("key1", String.class); descriptionManager.getByName(SourceAdapterFactory.NAME_JSON_SOURCE_ADAPTER); result = Deencapsulation.newInnerInstance(Specification.class, SourceAdapterFactory.NAME_JSON_SOURCE_ADAPTER, ProcessorType.SOURCE_ADAPTER, argTypes); argTypes = new HashMap<>(); argTypes.put("key2", Integer.class); descriptionManager.getByName(FilterFactory.NAME_DB_INSERTION_FILTER); result = Deencapsulation.newInnerInstance(Specification.class, FilterFactory.NAME_DB_INSERTION_FILTER, ProcessorType.FILTER, argTypes); argTypes = new HashMap<>(); argTypes.put("key3", Boolean.class); descriptionManager.getByName(FilterFactory.NAME_NOTIFICATION_FILTER); result = Deencapsulation.newInnerInstance(Specification.class, FilterFactory.NAME_NOTIFICATION_FILTER, ProcessorType.FILTER, argTypes); }}; ProcessorReferenceChainDescription reference = new ProcessorReferenceChainDescription( Arrays.asList(jsonSourceFilter, dbInsertionFilter, notificationFilter), new ExecutionInterval(2, TimeUnit.SECONDS)); Assert.assertTrue(validator.isValid(reference, context)); }
@Test public void testEmptyReference() { Assert.assertFalse(validator.isValid(new ProcessorReferenceChainDescription( new LinkedList<ProcessorReference>(), new ExecutionInterval(2, TimeUnit.SECONDS)), context)); }
@Test public void testMissingSourceAdapterReference() { new Expectations() {{ Map<String, Class<?>> argTypes = new HashMap<>(); argTypes.put("key3", Boolean.class); descriptionManager.getByName(FilterFactory.NAME_NOTIFICATION_FILTER); result = Deencapsulation.newInnerInstance(Specification.class, FilterFactory.NAME_NOTIFICATION_FILTER, ProcessorType.FILTER, argTypes); }}; Assert.assertFalse(validator.isValid(new ProcessorReferenceChainDescription( Arrays.asList(notificationFilter), new ExecutionInterval(2, TimeUnit.SECONDS)), context)); }
@Test public void testMissingParameter() { new Expectations() {{ final Map<String, Class<?>> argTypes = new HashMap<>(); argTypes.put("someMissingKey", String.class); descriptionManager.getByName(SourceAdapterFactory.NAME_JSON_SOURCE_ADAPTER); result = Deencapsulation.newInnerInstance(Specification.class, SourceAdapterFactory.NAME_JSON_SOURCE_ADAPTER, ProcessorType.SOURCE_ADAPTER, argTypes); }}; Assert.assertFalse(validator.isValid(new ProcessorReferenceChainDescription( Arrays.asList(jsonSourceFilter), new ExecutionInterval(2, TimeUnit.SECONDS)), context)); }