HibernateSearchService { List<String> getNamesOfAnnotatedStringFields(Class<?> entityType) { if(classIndexedFieldMap.containsKey(entityType)) { return classIndexedFieldMap.get(entityType); } var indexedStringFields = Arrays.stream(entityType.getDeclaredFields()) .filter(field -> String.class.equals(field.getType()) && Objects.nonNull(field.getAnnotation(Field.class))) .map(field -> { var fieldAnnotation = field.getAnnotation(Field.class); if(!fieldAnnotation.name().isEmpty()) { return fieldAnnotation.name(); } return field.getName(); }) .collect(Collectors.toList()); classIndexedFieldMap.put(entityType, indexedStringFields); return indexedStringFields; } HibernateSearchService(@NotNull Collection<String> statusWithLobConstraint, @NotNull Collection<String> excludedStatus); @SuppressWarnings("unchecked") List<Project> searchProjects(@NonNull String simpleQueryString, String lob); @SuppressWarnings("unchecked") Page<Project> searchProjects(@NonNull String simpleQueryString, @NonNull Pageable pageable, String lob); @SuppressWarnings("unchecked") List<UserData> searchUserData(@NonNull List<User> users, @NonNull String simpleQueryString);  }
@Test @SuppressWarnings("unchecked") public void getNamesOfAnnotatedStringFieldsGetsNamesOfAnnotatedStringFieldsAndCachesResultWhenNotPresent() { var expectedFieldNames = List.of("renamed_field", "secondField"); var actualFieldNames = hibernateSearchService.getNamesOfAnnotatedStringFields(IndexedEntity.class); var softly = new SoftAssertions(); softly.assertThat(actualFieldNames).containsExactlyInAnyOrderElementsOf(expectedFieldNames); softly.assertThat(hibernateSearchService.classIndexedFieldMap).containsExactly( Map.entry(IndexedEntity.class, expectedFieldNames) ); softly.assertAll(); }
@Test public void getNamesOfAnnotatedStringFieldsReturnsCachedListWhenPresent() { var cachedFieldNames = List.of("field_1", "field_2"); hibernateSearchService.classIndexedFieldMap.put(IndexedEntity.class, cachedFieldNames); var actualFieldNames = hibernateSearchService.getNamesOfAnnotatedStringFields(IndexedEntity.class); assertThat(actualFieldNames).containsExactlyInAnyOrderElementsOf(cachedFieldNames); }