PlanningSolutionScoreHolderDeleteValidator implements DeleteValidator<DataObject> { @Override public Collection<ValidationMessage> validate(final Path dataObjectPath, final DataObject dataObject) { return validatePath(dataObjectPath); } @Inject  PlanningSolutionScoreHolderDeleteValidator(final DataModelerService dataModelerService,
                                                      @Named("ioStrategy") final IOService ioService,
                                                      final ScoreHolderUtils scoreHolderUtils,
                                                      final AssetsUsageService assetsUsageService); @Override Collection<ValidationMessage> validate(final Path dataObjectPath,
                                                  final DataObject dataObject); @Override Collection<ValidationMessage> validate(final Path dataObjectPath); @Override boolean accept(final Path path);  }
@Test public void planningSolution() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); DataObject originalDataObject = new DataObjectImpl("test", "Test"); originalDataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(PlanningSolution.class))); GenerationResult generationResult = new GenerationResult(); generationResult.setDataObject(originalDataObject); when(dataModelerService.loadDataObject(any(), anyString(), any())).thenReturn(generationResult); when(scoreHolderUtils.extractScoreTypeFqn(originalDataObject)).thenReturn(HardSoftScore.class.getName()); when(scoreHolderUtils.getScoreHolderTypeFqn(HardSoftScore.class.getName())).thenReturn(HardSoftScoreHolder.class.getName()); when(assetsUsageService.getAssetUsages(HardSoftScoreHolder.class.getName(), ResourceType.JAVA, dataObjectPath)).thenReturn(Arrays.asList(mock(Path.class))); DataObject updatedDataObject = new DataObjectImpl("test", "Test"); updatedDataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(PlanningSolution.class))); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertEquals(1, result.size()); ValidationMessage message = result.iterator().next(); assertTrue(message instanceof ScoreHolderGlobalToBeRemovedMessage); }
@Test public void noDataObjectInPath() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); GenerationResult generationResult = new GenerationResult(); generationResult.setDataObject(null); when(dataModelerService.loadDataObject(any(), anyString(), any())).thenReturn(generationResult); Collection<ValidationMessage> result = validator.validate(dataObjectPath, mock(DataObject.class)); assertTrue(result.isEmpty()); }
@Test public void notAPlanningSolution() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); DataObject originalDataObject = new DataObjectImpl("test", "Test"); GenerationResult generationResult = new GenerationResult(); generationResult.setDataObject(originalDataObject); when(dataModelerService.loadDataObject(any(), anyString(), any())).thenReturn(generationResult); DataObject updatedDataObject = new DataObjectImpl("test", "Test"); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertTrue(result.isEmpty()); }
@Test public void scoreHolderTypeNotRecognized() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); DataObject originalDataObject = new DataObjectImpl("test", "Test"); originalDataObject.addAnnotation(new AnnotationImpl(DriverUtils.buildAnnotationDefinition(PlanningSolution.class))); GenerationResult generationResult = new GenerationResult(); generationResult.setDataObject(originalDataObject); when(dataModelerService.loadDataObject(any(), anyString(), any())).thenReturn(generationResult); when(scoreHolderUtils.extractScoreTypeFqn(originalDataObject)).thenReturn("UnknownScoreClassName"); when(scoreHolderUtils.getScoreHolderTypeFqn("UnknownScoreClassName")).thenReturn(null); DataObject updatedDataObject = new DataObjectImpl("test", "Test"); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertEquals(1, result.size()); ValidationMessage message = result.iterator().next(); assertTrue(message instanceof ScoreHolderGlobalTypeNotRecognizedMessage); }