@Test public void shouldCreateAndSaveCompositeDefaultValuesBeExecutedForAllCompositeDefinition() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Project project = Mockito.mock(Project.class); DefaultValue.DefaultValueType type = DefaultValue.DefaultValueType.PROCESSING_SESSION; Bar entity = new Bar(null, null, null, null, null, null, new Foo[]{new Foo(1L)}, null); Field field = Bar.class.getDeclaredField("array"); tested.createAndSaveCompositeDefaultValues(project, type, entity, field); Mockito.verify(repository, Mockito.times(2)).save(Mockito.any(DefaultValue.class)); }
protected <U extends Persistable<Long>> void createAndSaveCompositeDefaultValues( @NonNull final Project project, @NonNull final DefaultValue.DefaultValueType type, @NonNull final U entity, @NonNull final Field field) { List<DefaultValue> defaultValues = new ArrayList<>(); Arrays.stream(field.getAnnotation(GP2SDefaultComposite.class).definition()) .forEach(definition -> { try { defaultValues.add(createDefaultValue(project, type, entity, field, definition)); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { log.error("Unexpected error when accessing filed: " + field.getName() + " on class " + field.getType() .getCanonicalName(), e); } }); defaultValues.stream() .filter(defaultValue -> StringUtils.isNotBlank(defaultValue.getValue())) .map(this::mapToExistingIfPresent) .forEach(repository::save); }
DefaultValueService { protected <U extends Persistable<Long>> void createAndSaveCompositeDefaultValues( @NonNull final Project project, @NonNull final DefaultValue.DefaultValueType type, @NonNull final U entity, @NonNull final Field field) { List<DefaultValue> defaultValues = new ArrayList<>(); Arrays.stream(field.getAnnotation(GP2SDefaultComposite.class).definition()) .forEach(definition -> { try { defaultValues.add(createDefaultValue(project, type, entity, field, definition)); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { log.error("Unexpected error when accessing filed: " + field.getName() + " on class " + field.getType() .getCanonicalName(), e); } }); defaultValues.stream() .filter(defaultValue -> StringUtils.isNotBlank(defaultValue.getValue())) .map(this::mapToExistingIfPresent) .forEach(repository::save); } }
DefaultValueService { protected <U extends Persistable<Long>> void createAndSaveCompositeDefaultValues( @NonNull final Project project, @NonNull final DefaultValue.DefaultValueType type, @NonNull final U entity, @NonNull final Field field) { List<DefaultValue> defaultValues = new ArrayList<>(); Arrays.stream(field.getAnnotation(GP2SDefaultComposite.class).definition()) .forEach(definition -> { try { defaultValues.add(createDefaultValue(project, type, entity, field, definition)); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { log.error("Unexpected error when accessing filed: " + field.getName() + " on class " + field.getType() .getCanonicalName(), e); } }); defaultValues.stream() .filter(defaultValue -> StringUtils.isNotBlank(defaultValue.getValue())) .map(this::mapToExistingIfPresent) .forEach(repository::save); } @Autowired  DefaultValueService(@NonNull final DefaultValueRepository repository,
                        @NonNull final ProjectRepository projectRepository); }
DefaultValueService { protected <U extends Persistable<Long>> void createAndSaveCompositeDefaultValues( @NonNull final Project project, @NonNull final DefaultValue.DefaultValueType type, @NonNull final U entity, @NonNull final Field field) { List<DefaultValue> defaultValues = new ArrayList<>(); Arrays.stream(field.getAnnotation(GP2SDefaultComposite.class).definition()) .forEach(definition -> { try { defaultValues.add(createDefaultValue(project, type, entity, field, definition)); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { log.error("Unexpected error when accessing filed: " + field.getName() + " on class " + field.getType() .getCanonicalName(), e); } }); defaultValues.stream() .filter(defaultValue -> StringUtils.isNotBlank(defaultValue.getValue())) .map(this::mapToExistingIfPresent) .forEach(repository::save); } @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 { protected <U extends Persistable<Long>> void createAndSaveCompositeDefaultValues( @NonNull final Project project, @NonNull final DefaultValue.DefaultValueType type, @NonNull final U entity, @NonNull final Field field) { List<DefaultValue> defaultValues = new ArrayList<>(); Arrays.stream(field.getAnnotation(GP2SDefaultComposite.class).definition()) .forEach(definition -> { try { defaultValues.add(createDefaultValue(project, type, entity, field, definition)); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { log.error("Unexpected error when accessing filed: " + field.getName() + " on class " + field.getType() .getCanonicalName(), e); } }); defaultValues.stream() .filter(defaultValue -> StringUtils.isNotBlank(defaultValue.getValue())) .map(this::mapToExistingIfPresent) .forEach(repository::save); } @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);  }