Skip to content

Commit

Permalink
Merge pull request #438 from eclipse/exception-when-entity-empty
Browse files Browse the repository at this point in the history
Fixes the Lack of descriptive error when trying to inject a Repository with type without @entity annotation
  • Loading branch information
otaviojava authored Sep 18, 2023
2 parents 941ff42 + e718802 commit 6a6bb57
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version

- Remove exception at the delete methods at the repositories proxies
- Add support to LIKE conditions parameterized at Repository methods annotated with `@Query`
- Enhance the error message when the entity in the repository does not have the Entity annotation

== [1.0.1] - 2023-7-31

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import jakarta.data.exceptions.MappingException;
import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.DataRepository;
import jakarta.data.repository.PageableRepository;
Expand Down Expand Up @@ -57,16 +58,18 @@ enum ClassGraphClassScanner implements ClassScanner {
Logger logger = Logger.getLogger(ClassGraphClassScanner.class.getName());
logger.fine("Starting scan class to find entities, embeddable and repositories.");
try (ScanResult result = new ClassGraph().enableAllInfo().scan()) {
checkInvalidRepositories(loadInvalidRepositories(result));
this.entities.addAll(loadEntities(result));
embeddables.addAll(loadEmbeddable(result));
this.embeddables.addAll(loadEmbeddable(result));
this.repositories.addAll(loadRepositories(result));
}
logger.fine(String.format("Finished the class scan with entities %d, embeddables %d and repositories: %d"
, entities.size(), embeddables.size(), repositories.size()));

}

@Override

@Override
public Set<Class<?>> entities() {
return unmodifiableSet(entities);
}
Expand Down Expand Up @@ -110,6 +113,27 @@ private static List<Class<DataRepository>> loadRepositories(ScanResult scan) {
.toList();
}

@SuppressWarnings("rawtypes")
private static void checkInvalidRepositories(List<Class<DataRepository>> classes) {
if (!classes.isEmpty()) {
String repositories = classes.stream()
.map(Class::getName)
.collect(Collectors.joining(","));
throw new MappingException("The following repositories are invalid because the Entities must have the " +
jakarta.nosql.Entity.class.getName() + " annotation: " + repositories);
}
}


@SuppressWarnings("rawtypes")
private static List<Class<DataRepository>> loadInvalidRepositories(ScanResult scan) {
return scan.getClassesWithAnnotation(Repository.class)
.getInterfaces()
.loadClasses(DataRepository.class)
.stream().filter(RepositoryFilter.INSTANCE::isInvalid)
.toList();
}

private static List<Class<?>> loadEmbeddable(ScanResult scan) {
return scan.getClassesWithAnnotation(Embeddable.class).loadClasses();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public boolean test(Class<?> type) {
.isPresent();
}

public boolean isInvalid(Class<?> type) {
Optional<Class<?>> entity = getEntity(type);
return entity.map(c -> c.getAnnotation(Entity.class))
.isEmpty();
}


private Optional<Class<?>> getEntity(Class<?> repository) {
Type[] interfaces = repository.getGenericInterfaces();
Expand All @@ -57,4 +63,5 @@ private Optional<Class<?>> getEntity(Class<?> repository) {
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,39 @@

class RepositoryFilterTest {

private Predicate<Class<?>> predicate;
private Predicate<Class<?>> valid;

private Predicate<Class<?>> invalid;

@BeforeEach
public void setUp() {
this.predicate = RepositoryFilter.INSTANCE;
this.valid = RepositoryFilter.INSTANCE;
this.invalid = RepositoryFilter.INSTANCE::isInvalid;
}

@Test
public void shouldReturnTrueWhenHasSupportRepository() {
assertThat(predicate.test(PersonRepository.class)).isTrue();
assertThat(predicate.test(People.class)).isTrue();
assertThat(predicate.test(Persons.class)).isTrue();
assertThat(valid.test(PersonRepository.class)).isTrue();
assertThat(valid.test(People.class)).isTrue();
assertThat(valid.test(Persons.class)).isTrue();

}

@Test
public void shouldReturnFalseWhenHasNotSupportRepository() {
assertThat(predicate.test(NoSQLVendor.class)).isFalse();
assertThat(predicate.test(Server.class)).isFalse();
assertThat(predicate.test(StringSupplier.class)).isFalse();
assertThat(predicate.test(Repository.class)).isFalse();
assertThat(valid.test(NoSQLVendor.class)).isFalse();
assertThat(valid.test(Server.class)).isFalse();
assertThat(valid.test(StringSupplier.class)).isFalse();
assertThat(valid.test(Repository.class)).isFalse();
}

@Test
public void shouldReturnInvalid(){
assertThat(invalid.test(PersonRepository.class)).isFalse();
assertThat(invalid.test(People.class)).isFalse();
assertThat(invalid.test(Persons.class)).isFalse();
assertThat(invalid.test(NoSQLVendor.class)).isTrue();
assertThat(invalid.test(Server.class)).isTrue();
}


Expand Down

0 comments on commit 6a6bb57

Please sign in to comment.