PlanningSolutionScoreHolderSaveValidator implements SaveValidator<DataObject> { @Override public Collection<ValidationMessage> validate(final Path dataObjectPath, final DataObject dataObject) { if (dataObjectPath != null) { String dataObjectSource = ioService.readAllString(Paths.convert(dataObjectPath)); GenerationResult generationResult = dataModelerService.loadDataObject(dataObjectPath, dataObjectSource, dataObjectPath); if (generationResult.hasErrors()) { return Collections.emptyList(); } else { DataObject originalDataObject = generationResult.getDataObject(); if (originalDataObject != null && originalDataObject.getAnnotation(PLANNING_SOLUTION_ANNOTATION) != null) { String originalDataObjectScoreTypeFqn = scoreHolderUtils.extractScoreTypeFqn(originalDataObject); String originalDataObjectScoreHolderTypeFqn = scoreHolderUtils.getScoreHolderTypeFqn(originalDataObjectScoreTypeFqn); if (originalDataObjectScoreHolderTypeFqn == null) { return Arrays.asList(new ScoreHolderGlobalTypeNotRecognizedMessage(Level.WARNING)); } List<Path> scoreHolderGlobalUsages = assetsUsageService.getAssetUsages(originalDataObjectScoreHolderTypeFqn, ResourceType.JAVA, dataObjectPath); if (scoreHolderGlobalUsages.isEmpty()) { return Collections.emptyList(); } else { if (dataObject.getAnnotation(PLANNING_SOLUTION_ANNOTATION) == null) { return Arrays.asList(new ScoreHolderGlobalToBeRemovedMessage(Level.WARNING)); } else { String dataObjectScoreTypeFqn = scoreHolderUtils.extractScoreTypeFqn(dataObject); String dataObjectScoreHolderTypeFqn = scoreHolderUtils.getScoreHolderTypeFqn(dataObjectScoreTypeFqn); if (dataObjectScoreHolderTypeFqn == null) { return Arrays.asList(new ScoreHolderGlobalTypeNotRecognizedMessage(Level.WARNING)); } if (!originalDataObjectScoreHolderTypeFqn.equals(dataObjectScoreHolderTypeFqn)) { return Arrays.asList(new ScoreHolderGlobalTypeToBeChangedMessage(Level.WARNING)); } } } } } } return Collections.emptyList(); } @Inject  PlanningSolutionScoreHolderSaveValidator(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 boolean accept(final Path path);  }
@Test public void dataObjectToADataObject() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); DataObject originalDataObject = createDataObject(null); GenerationResult generationResult = new GenerationResult(); generationResult.setDataObject(originalDataObject); when(dataModelerService.loadDataObject(any(), anyString(), any())).thenReturn(generationResult); DataObject updatedDataObject = createDataObject(null); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertTrue(result.isEmpty()); }
@Test public void dataObjectNull() { 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); DataObject updatedDataObject = createDataObject(null); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertTrue(result.isEmpty()); }
@Test public void scoreTypeChanged() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); DataObject originalDataObject = createDataObject(HardSoftScore.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 = createDataObject(SimpleScore.class); when(scoreHolderUtils.extractScoreTypeFqn(updatedDataObject)).thenReturn(SimpleScore.class.getName()); when(scoreHolderUtils.getScoreHolderTypeFqn(SimpleScore.class.getName())).thenReturn(SimpleScoreHolder.class.getName()); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertEquals(1, result.size()); ValidationMessage message = result.iterator().next(); assertTrue(message instanceof ScoreHolderGlobalTypeToBeChangedMessage); }
@Test public void dataObjectToAPlanningSolution() { Path dataObjectPath = PathFactory.newPath("Test.java", "file: when(ioService.readAllString(Paths.convert(dataObjectPath))).thenReturn("testResult"); DataObject originalDataObject = createDataObject(null); GenerationResult generationResult = new GenerationResult(); generationResult.setDataObject(originalDataObject); when(dataModelerService.loadDataObject(any(), anyString(), any())).thenReturn(generationResult); DataObject updatedDataObject = createDataObject(SimpleScore.class); Collection<ValidationMessage> result = validator.validate(dataObjectPath, updatedDataObject); assertTrue(result.isEmpty()); }