Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAREST-1424 - Implement onBeforeLinkDelete and onAfterLinkDelete in… #373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Chris Midgley
*/
public class ValidatingRepositoryEventListener extends AbstractRepositoryEventListener<Object> {

Expand Down Expand Up @@ -141,6 +142,24 @@ protected void onAfterLinkSave(Object parent, Object linked) {
validate("afterLinkSave", parent);
}

/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.event.AbstractRepositoryEventListener#onBeforeLinkDelete(java.lang.Object, java.lang.Object)
*/
@Override
protected void onBeforeLinkDelete(Object parent, Object linked) {
validate("beforeLinkDelete", parent);
}

/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.event.AbstractRepositoryEventListener#onAfterLinkDelete(java.lang.Object, java.lang.Object)
*/
@Override
protected void onAfterLinkDelete(Object parent, Object linked) {
validate("afterLinkDelete", parent);
}

/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.event.AbstractRepositoryEventListener#onBeforeDelete(java.lang.Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.springframework.data.rest.core.domain.JpaRepositoryConfig;
import org.springframework.data.rest.core.domain.Person;
import org.springframework.data.rest.core.domain.PersonNameValidator;
import org.springframework.data.rest.core.event.AfterLinkDeleteEvent;
import org.springframework.data.rest.core.event.BeforeLinkDeleteEvent;
import org.springframework.data.rest.core.event.BeforeSaveEvent;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -40,6 +42,7 @@
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Chris Midgley
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
Expand All @@ -54,6 +57,8 @@ public ValidatingRepositoryEventListener validatingListener(ObjectFactory<Persis

ValidatingRepositoryEventListener listener = new ValidatingRepositoryEventListener(persistentEntities);
listener.addValidator("beforeSave", new PersonNameValidator());
listener.addValidator("beforeLinkDelete", new PersonNameValidator());
listener.addValidator("afterLinkDelete", new PersonNameValidator());

return listener;
}
Expand All @@ -70,4 +75,22 @@ public void shouldValidateLastName() throws Exception {
// Empty name should be rejected by PersonNameValidator
context.publishEvent(new BeforeSaveEvent(new Person("Dave", "")));
}

@Test(expected = RepositoryConstraintViolationException.class) // DATAREST-1424
public void shouldValidateBeforeLinkDelete() throws Exception {

mappingContext.getPersistentEntity(Person.class);
context.publishEvent(new BeforeLinkDeleteEvent(createInvalidPerson(), null));
}

@Test(expected = RepositoryConstraintViolationException.class) // DATAREST-1424
public void shouldValidateAfterLinkDelete() throws Exception {

mappingContext.getPersistentEntity(Person.class);
context.publishEvent(new AfterLinkDeleteEvent(createInvalidPerson(), null));
}

private Person createInvalidPerson() {
return new Person("Dave", "");
}
}