PlanningSolutionSaveValidator implements SaveValidator<DataObject> { @Override public Collection<ValidationMessage> validate(final Path dataObjectPath, final DataObject dataObject) { if (dataObject != null && dataObject.getAnnotation(PLANNING_SOLUTION_ANNOTATION) != null) { List<Path> planningSolutionUsages = assetsUsageService.getAssetUsages(PLANNING_SOLUTION_ANNOTATION, ResourceType.JAVA, dataObjectPath); if (planningSolutionUsages.contains(dataObjectPath)) { return Collections.emptyList(); } if (!planningSolutionUsages.isEmpty()) { return Arrays.asList(new PlanningSolutionToBeDuplicatedMessage(Level.ERROR)); } } return Collections.emptyList(); } @Inject  PlanningSolutionSaveValidator(AssetsUsageService assetsUsageService); @Override Collection<ValidationMessage> validate(final Path dataObjectPath,
                                                  final DataObject dataObject); @Override boolean accept(final Path path);  }
@Test public void checkDataObjectIsNull() { Collection<ValidationMessage> result = saveValidator.validate(mock(Path.class), null); assertEquals(0, result.size()); }
@Test public void checkNotAPlanningSolution() { DataObject dataObject = new DataObjectImpl("test", "Test"); Collection<ValidationMessage> result = saveValidator.validate(mock(Path.class), dataObject); assertTrue(result.isEmpty()); verify(assetsUsageService, never()).getAssetUsages(any(), any(), any()); }
@Test public void checkPlanningSolutionNoOtherExists() { DataObject dataObject = new DataObjectImpl("test", "Test"); dataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(PlanningSolution.class))); Path dataObjectPath = mock(Path.class); when(assetsUsageService.getAssetUsages(PlanningSolution.class.getName(), ResourceType.JAVA, dataObjectPath)).thenReturn(Arrays.asList(dataObjectPath)); Collection<ValidationMessage> result = saveValidator.validate(dataObjectPath, dataObject); assertTrue(result.isEmpty()); }
@Test public void checkPlanningSolutionNoOtherExistsFirstSave() { DataObject dataObject = new DataObjectImpl("test", "Test"); dataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(PlanningSolution.class))); Path dataObjectPath = mock(Path.class); when(assetsUsageService.getAssetUsages(PlanningSolution.class.getName(), ResourceType.JAVA, dataObjectPath)).thenReturn(Collections.emptyList()); Collection<ValidationMessage> result = saveValidator.validate(dataObjectPath, dataObject); assertTrue(result.isEmpty()); }
@Test public void checkPlanningSolutionOtherExists() { DataObject dataObject = new DataObjectImpl("test", "Test"); dataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(PlanningSolution.class))); Path dataObjectPath = mock(Path.class); Path otherDataObjectPath = mock(Path.class); when(assetsUsageService.getAssetUsages(PlanningSolution.class.getName(), ResourceType.JAVA, dataObjectPath)).thenReturn(Arrays.asList(otherDataObjectPath)); Collection<ValidationMessage> result = saveValidator.validate(dataObjectPath, dataObject); assertEquals(1, result.size()); ValidationMessage message = result.iterator().next(); assertTrue(message instanceof PlanningSolutionToBeDuplicatedMessage); }