### Question:
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);  }### Answer:
@Test(expected = NullPointerException.class) public void shouldNotAcceptWhenMissingTypeAnnotation() { tested.saveDefaultValues(Project.builder().build(), new Foo()); }
@Test public void shouldNotSaveNotAnnotatedFields() { tested.saveDefaultValues(Project.builder().build(), new Bar(1L, null, null, null, null, "test", null, null)); Mockito.verify(repository, Mockito.times(0)).save(Mockito.any(DefaultValue.class)); }
@Test public void shouldSaveValuesAsStrings() { tested.saveDefaultValues(project, new Bar(null, "val1", 2L, 3.0f, null, null, null, null)); Mockito.verify(repository, Mockito.times(1)) .save(Matchers.refEq(DefaultValue.builder() .entityType(DefaultValue.DefaultValueType.GRID) .project(project) .fieldName("test1") .value("val1") .build())); Mockito.verify(repository, Mockito.times(1)) .save(Matchers.refEq(DefaultValue.builder() .entityType(DefaultValue.DefaultValueType.GRID) .project(project) .fieldName("test2") .value("2") .build())); Mockito.verify(repository, Mockito.times(1)) .save(Matchers.refEq(DefaultValue.builder() .entityType(DefaultValue.DefaultValueType.GRID) .project(project) .fieldName("test3") .value("3.0") .build())); }
@Test public void shouldSaveOnlyIdOfObjectAtField() { tested.saveDefaultValues(project, new Bar(null, null, null, null, new Foo(5L), null, null, null)); Mockito.verify(repository, Mockito.times(1)) .save(Matchers.refEq(DefaultValue.builder() .entityType(DefaultValue.DefaultValueType.GRID) .project(project) .fieldName("test4") .value("5") .build())); }
@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); }