@Test public void shouldUpdateDefaultValueIfExists() { DefaultValue dv = Mockito.mock(DefaultValue.class); Mockito.when(repository.findByEntityTypeAndProject_IdAndFieldName(DefaultValue.DefaultValueType.GRID, project.getId(), "test1")).thenReturn(dv); tested.saveDefaultValues(project, new Bar(null, "val1", null, null, null, null, null, null)); Mockito.verify(dv, Mockito.times(1)).setValue("val1"); Mockito.verify(repository, Mockito.times(1)).save(dv); }
public <U extends Persistable<Long>> void saveDefaultValues(@NonNull final Project project, @NonNull final U entity) { final GP2SDefaultType annotation = entity.getClass() .getAnnotation(GP2SDefaultType.class); Objects.requireNonNull(annotation, "Missing GP2SDefaultType annotation"); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultAnnotationPresent) .map(field -> DefaultValue.builder() .entityType(annotation.value()) .project(project) .fieldName(field.getName()) .value(getFieldValue(entity, field)) .build()) .filter(defaultValue -> defaultValue.getValue() != null) .map(this::mapToExistingIfPresent) .forEach(defaultValue -> repository.save(defaultValue)); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultCompositeAnnotationPresent) .forEach(field -> createAndSaveCompositeDefaultValues(project, annotation.value(), entity, field)); }
DefaultValueService { public <U extends Persistable<Long>> void saveDefaultValues(@NonNull final Project project, @NonNull final U entity) { final GP2SDefaultType annotation = entity.getClass() .getAnnotation(GP2SDefaultType.class); Objects.requireNonNull(annotation, "Missing GP2SDefaultType annotation"); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultAnnotationPresent) .map(field -> DefaultValue.builder() .entityType(annotation.value()) .project(project) .fieldName(field.getName()) .value(getFieldValue(entity, field)) .build()) .filter(defaultValue -> defaultValue.getValue() != null) .map(this::mapToExistingIfPresent) .forEach(defaultValue -> repository.save(defaultValue)); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultCompositeAnnotationPresent) .forEach(field -> createAndSaveCompositeDefaultValues(project, annotation.value(), entity, field)); } }
DefaultValueService { public <U extends Persistable<Long>> void saveDefaultValues(@NonNull final Project project, @NonNull final U entity) { final GP2SDefaultType annotation = entity.getClass() .getAnnotation(GP2SDefaultType.class); Objects.requireNonNull(annotation, "Missing GP2SDefaultType annotation"); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultAnnotationPresent) .map(field -> DefaultValue.builder() .entityType(annotation.value()) .project(project) .fieldName(field.getName()) .value(getFieldValue(entity, field)) .build()) .filter(defaultValue -> defaultValue.getValue() != null) .map(this::mapToExistingIfPresent) .forEach(defaultValue -> repository.save(defaultValue)); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultCompositeAnnotationPresent) .forEach(field -> createAndSaveCompositeDefaultValues(project, annotation.value(), entity, field)); } @Autowired  DefaultValueService(@NonNull final DefaultValueRepository repository,
                        @NonNull final ProjectRepository projectRepository); }
DefaultValueService { public <U extends Persistable<Long>> void saveDefaultValues(@NonNull final Project project, @NonNull final U entity) { final GP2SDefaultType annotation = entity.getClass() .getAnnotation(GP2SDefaultType.class); Objects.requireNonNull(annotation, "Missing GP2SDefaultType annotation"); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultAnnotationPresent) .map(field -> DefaultValue.builder() .entityType(annotation.value()) .project(project) .fieldName(field.getName()) .value(getFieldValue(entity, field)) .build()) .filter(defaultValue -> defaultValue.getValue() != null) .map(this::mapToExistingIfPresent) .forEach(defaultValue -> repository.save(defaultValue)); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultCompositeAnnotationPresent) .forEach(field -> createAndSaveCompositeDefaultValues(project, annotation.value(), entity, field)); } @Autowired  DefaultValueService(@NonNull final DefaultValueRepository repository,
                        @NonNull final ProjectRepository projectRepository); Map<String, Object> getValues(DefaultValue.DefaultValueType type, Long projectId); Map<String, Object> getValues(DefaultValue.DefaultValueType type, String projectSlug); void saveDefaultValues(@NonNull final Project project, @NonNull final U entity); }
DefaultValueService { public <U extends Persistable<Long>> void saveDefaultValues(@NonNull final Project project, @NonNull final U entity) { final GP2SDefaultType annotation = entity.getClass() .getAnnotation(GP2SDefaultType.class); Objects.requireNonNull(annotation, "Missing GP2SDefaultType annotation"); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultAnnotationPresent) .map(field -> DefaultValue.builder() .entityType(annotation.value()) .project(project) .fieldName(field.getName()) .value(getFieldValue(entity, field)) .build()) .filter(defaultValue -> defaultValue.getValue() != null) .map(this::mapToExistingIfPresent) .forEach(defaultValue -> repository.save(defaultValue)); Arrays.stream(entity.getClass() .getDeclaredFields()) .filter(this::isGp2sDefaultCompositeAnnotationPresent) .forEach(field -> createAndSaveCompositeDefaultValues(project, annotation.value(), entity, field)); } @Autowired  DefaultValueService(@NonNull final DefaultValueRepository repository,
                        @NonNull final ProjectRepository projectRepository); Map<String, Object> getValues(DefaultValue.DefaultValueType type, Long projectId); Map<String, Object> getValues(DefaultValue.DefaultValueType type, String projectSlug); void saveDefaultValues(@NonNull final Project project, @NonNull final U entity);  }