Skip to content

Commit 045a220

Browse files
committed
Adapt tests to MappingException cause when entity creation fails.
See #3045
1 parent b63b461 commit 045a220

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

src/test/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jPersistentEntityTests.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
import static org.assertj.core.api.Assertions.assertThat;
4747
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
48-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4948

5049
/**
5150
* @author Gerrit Meier
@@ -608,25 +607,32 @@ class DuplicateProperties {
608607

609608
@Test
610609
void failsOnDuplicatedProperties() {
611-
assertThatIllegalStateException()
610+
assertThatExceptionOfType(MappingException.class)
612611
.isThrownBy(() -> new Neo4jMappingContext().getPersistentEntity(EntityWithDuplicatedProperties.class))
612+
.withCauseInstanceOf(IllegalStateException.class)
613+
.havingCause()
613614
.withMessage("Duplicate definition of property [name] in entity class "
614615
+ "org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentEntityTests$EntityWithDuplicatedProperties");
615616
}
616617

617618
@Test
618619
void failsOnMultipleDuplicatedProperties() {
619-
assertThatIllegalStateException().isThrownBy(
620-
() -> new Neo4jMappingContext().getPersistentEntity(EntityWithMultipleDuplicatedProperties.class))
620+
assertThatExceptionOfType(MappingException.class)
621+
.isThrownBy(() -> new Neo4jMappingContext()
622+
.getPersistentEntity(EntityWithMultipleDuplicatedProperties.class))
623+
.withCauseInstanceOf(IllegalStateException.class)
624+
.havingCause()
621625
.withMessage("Duplicate definition of properties [foo, name] in entity class "
622626
+ "org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentEntityTests$EntityWithMultipleDuplicatedProperties");
623627
}
624628

625629
@Test // GH-1903
626630
void failsOnMultipleInheritedDuplicatedProperties() {
627-
assertThatIllegalStateException()
631+
assertThatExceptionOfType(MappingException.class)
628632
.isThrownBy(() -> new Neo4jMappingContext()
629633
.getPersistentEntity(EntityWithInheritedMultipleDuplicatedProperties.class))
634+
.withCauseInstanceOf(IllegalStateException.class)
635+
.havingCause()
630636
.withMessage("Duplicate definition of property [name] in entity class "
631637
+ "org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentEntityTests$EntityWithInheritedMultipleDuplicatedProperties");
632638
}
@@ -651,8 +657,10 @@ class Relationships {
651657
void failsOnDynamicRelationshipsWithExplicitType(Class<?> entityToTest) {
652658

653659
String expectedMessage = "Dynamic relationships cannot be used with a fixed type\\; omit @Relationship or use @Relationship\\(direction = (OUTGOING|INCOMING)\\) without a type in class .*MixedDynamicAndExplicitRelationship\\d on field dynamicRelationships";
654-
assertThatIllegalStateException()
660+
assertThatExceptionOfType(MappingException.class)
655661
.isThrownBy(() -> new Neo4jMappingContext().getPersistentEntity(entityToTest))
662+
.withCauseInstanceOf(IllegalStateException.class)
663+
.havingCause()
656664
.withMessageMatching(expectedMessage);
657665
}
658666

@@ -664,7 +672,9 @@ void multipleDynamicAssociationsToTheSameEntityAreNotAllowed(Class<?> entityToTe
664672
String expectedMessage = ".*TypeWithInvalidDynamicRelationshipMappings\\d already contains a dynamic relationship to class org\\.springframework\\.data\\.neo4j\\.core\\.mapping\\.Neo4jMappingContextTests\\$BikeNode; only one dynamic relationship between to entities is permitted";
665673
Neo4jMappingContext schema = new Neo4jMappingContext();
666674
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(entityToTest)));
667-
assertThatIllegalStateException().isThrownBy(() -> schema.initialize())
675+
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> schema.initialize())
676+
.withCauseInstanceOf(IllegalStateException.class)
677+
.havingCause()
668678
.withMessageMatching(expectedMessage);
669679
}
670680

@@ -682,6 +692,7 @@ void doesFailOnRelationshipPropertiesWithMissingTargetNode() {
682692
assertThatExceptionOfType(MappingException.class)
683693
.isThrownBy(() -> new Neo4jMappingContext()
684694
.getPersistentEntity(EntityWithInCorrectRelationshipProperties.class))
695+
.havingCause()
685696
.withMessageContaining("Missing @TargetNode declaration in");
686697
}
687698

@@ -870,26 +881,32 @@ void shouldDetectValidInheritedDynamicLabels() {
870881
@Test
871882
void shouldDetectInvalidInheritedDynamicLabels() {
872883

873-
assertThatIllegalStateException()
884+
assertThatExceptionOfType(MappingException.class)
874885
.isThrownBy(() -> new Neo4jMappingContext().getPersistentEntity(InvalidInheritedDynamicLabels.class))
886+
.withCauseInstanceOf(IllegalStateException.class)
887+
.havingCause()
875888
.withMessageMatching(
876889
"Multiple properties in entity class .*DefaultNeo4jPersistentEntityTests\\$InvalidInheritedDynamicLabels are annotated with @DynamicLabels: \\[dynamicLabels, localDynamicLabels]");
877890
}
878891

879892
@Test
880893
void shouldDetectInvalidDynamicLabels() {
881894

882-
assertThatIllegalStateException()
895+
assertThatExceptionOfType(MappingException.class)
883896
.isThrownBy(() -> new Neo4jMappingContext().getPersistentEntity(NodeWithInvalidDynamicLabels.class))
897+
.withCauseInstanceOf(IllegalStateException.class)
898+
.havingCause()
884899
.withMessageMatching(
885900
"Multiple properties in entity class .*DefaultNeo4jPersistentEntityTests\\$NodeWithInvalidDynamicLabels are annotated with @DynamicLabels: \\[dynamicLabels, moarDynamicLabels]");
886901
}
887902

888903
@Test
889904
void shouldDetectInvalidDynamicLabelsTarget() {
890905

891-
assertThatIllegalStateException()
906+
assertThatExceptionOfType(MappingException.class)
892907
.isThrownBy(() -> new Neo4jMappingContext().getPersistentEntity(InvalidDynamicLabels.class))
908+
.withCauseInstanceOf(IllegalStateException.class)
909+
.havingCause()
893910
.withMessageMatching(
894911
"Property dynamicLabels on class .*DefaultNeo4jPersistentEntityTests\\$InvalidDynamicLabels must extends java\\.util\\.Collection");
895912
}
@@ -909,8 +926,10 @@ void validVectorProperties() {
909926

910927
@Test
911928
void invalidVectorProperties() {
912-
assertThatIllegalStateException()
929+
assertThatExceptionOfType(MappingException.class)
913930
.isThrownBy(() -> new Neo4jMappingContext().getPersistentEntity(VectorInvalid.class))
931+
.withCauseInstanceOf(IllegalStateException.class)
932+
.havingCause()
914933
.withMessageContaining(
915934
"There are multiple fields of type interface org.springframework.data.domain.Vector in entity org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentEntityTests$VectorInvalid:")
916935
// the order of properties might be not the same all the time

src/test/java/org/springframework/data/neo4j/core/mapping/Neo4jMappingContextTests.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.core.convert.converter.GenericConverter;
4545
import org.springframework.data.annotation.Transient;
4646
import org.springframework.data.mapping.Association;
47+
import org.springframework.data.mapping.MappingException;
4748
import org.springframework.data.mapping.SimpleAssociationHandler;
4849
import org.springframework.data.neo4j.config.Neo4jEntityScanner;
4950
import org.springframework.data.neo4j.core.convert.Neo4jConversionService;
@@ -77,8 +78,8 @@
7778
import org.springframework.data.neo4j.test.LogbackCapturingExtension;
7879

7980
import static org.assertj.core.api.Assertions.assertThat;
81+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
8082
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
81-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
8283

8384
/**
8485
* @author Michael J. Simons
@@ -158,7 +159,9 @@ void shouldPreventIllegalIdAnnotations() {
158159

159160
Neo4jMappingContext schema = new Neo4jMappingContext();
160161
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(InvalidId.class)));
161-
assertThatIllegalArgumentException().isThrownBy(() -> schema.initialize())
162+
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> schema.initialize())
163+
.withCauseInstanceOf(IllegalArgumentException.class)
164+
.havingCause()
162165
.withMessageMatching(
163166
"Cannot use internal id strategy with custom property getMappingFunctionFor on entity .*");
164167
}
@@ -168,15 +171,19 @@ void shouldPreventIllegalIdTypes() {
168171

169172
Neo4jMappingContext schema = new Neo4jMappingContext();
170173
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(InvalidIdType.class)));
171-
assertThatIllegalArgumentException().isThrownBy(schema::initialize)
174+
assertThatExceptionOfType(MappingException.class).isThrownBy(schema::initialize)
175+
.withCauseInstanceOf(IllegalArgumentException.class)
176+
.havingCause()
172177
.withMessageMatching("Internally generated ids can only be assigned to one of .*");
173178
}
174179

175180
@Test
176181
void missingIdDefinitionShouldRaiseError() {
177182

178183
Neo4jMappingContext schema = new Neo4jMappingContext();
179-
assertThatIllegalStateException().isThrownBy(() -> schema.getPersistentEntity(MissingId.class))
184+
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> schema.getPersistentEntity(MissingId.class))
185+
.withCauseInstanceOf(IllegalStateException.class)
186+
.havingCause()
180187
.withMessage("Missing id property on " + MissingId.class);
181188
}
182189

@@ -383,11 +390,14 @@ void shouldNotOverwriteDiscoveredBaseClassWhenSeeingClassAsGenericPropertyAgain(
383390
InvalidMultiDynamics4.class })
384391
void shouldDetectAllVariantsOfMultipleDynamicRelationships(Class<?> thingWithRelations) {
385392

386-
assertThatIllegalStateException().isThrownBy(() -> {
393+
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
387394
Neo4jMappingContext schema = new Neo4jMappingContext();
388395
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(TripNode.class, thingWithRelations)));
389396
schema.initialize();
390-
}).withMessageMatching(".*; only one dynamic relationship between to entities is permitted");
397+
})
398+
.withCauseInstanceOf(IllegalStateException.class)
399+
.havingCause()
400+
.withMessageMatching(".*; only one dynamic relationship between to entities is permitted");
391401
}
392402

393403
@ParameterizedTest // GH-2201
@@ -1237,11 +1247,12 @@ class InvalidRelationshipProperties {
12371247
void startupWithoutInternallyGeneratedIDShouldFail() {
12381248

12391249
Neo4jMappingContext schema = new Neo4jMappingContext();
1240-
assertThatIllegalStateException().isThrownBy(() -> {
1250+
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
12411251
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(IrrelevantSourceContainer.class,
12421252
InvalidRelationshipPropertyContainer.class, IrrelevantTargetContainer.class)));
12431253
schema.initialize();
12441254
})
1255+
.havingRootCause()
12451256
.withMessage(
12461257
"The class `org.springframework.data.neo4j.core.mapping.Neo4jMappingContextTests$InvalidRelationshipPropertyContainer` for the properties of a relationship is missing a property for the generated, internal ID (`@Id @GeneratedValue Long id` or `@Id @GeneratedValue String id`) which is needed for safely updating properties");
12471258
}
@@ -1250,11 +1261,12 @@ void startupWithoutInternallyGeneratedIDShouldFail() {
12501261
void startupWithWrongKindOfGeneratedIDShouldFail() {
12511262

12521263
Neo4jMappingContext schema = new Neo4jMappingContext();
1253-
assertThatIllegalStateException().isThrownBy(() -> {
1264+
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> {
12541265
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(IrrelevantSourceContainer3.class,
12551266
InvalidRelationshipPropertyContainer2.class, IrrelevantTargetContainer.class)));
12561267
schema.initialize();
12571268
})
1269+
.havingRootCause()
12581270
.withMessage(
12591271
"The class `org.springframework.data.neo4j.core.mapping.Neo4jMappingContextTests$InvalidRelationshipPropertyContainer2` for the properties of a relationship is missing a property for the generated, internal ID (`@Id @GeneratedValue Long id` or `@Id @GeneratedValue String id`) which is needed for safely updating properties");
12601272
}

0 commit comments

Comments
 (0)