ClassConverter { public ClassMapping create(Class<?> entityClass) { long start = System.currentTimeMillis(); String entityName = reflections.getEntityName(entityClass); List<FieldMapping> fields = reflections.getFields(entityClass) .stream().map(this::to).collect(toList()); List<String> fieldsName = fields.stream().map(FieldMapping::getName).collect(toList()); Map<String, NativeMapping> nativeFieldGroupByJavaField = getNativeFieldGroupByJavaField(fields, "", ""); Map<String, FieldMapping> fieldsGroupedByName = fields.stream() .collect(collectingAndThen(toMap(FieldMapping::getName, Function.identity()), Collections::unmodifiableMap)); InstanceSupplier instanceSupplier = instanceSupplierFactory.apply(reflections.makeAccessible(entityClass)); ClassMapping mapping = DefaultClassMapping.builder().withName(entityName) .withClassInstance(entityClass) .withFields(fields) .withFieldsName(fieldsName) .withInstanceSupplier(instanceSupplier) .withJavaFieldGroupedByColumn(nativeFieldGroupByJavaField) .withFieldsGroupedByName(fieldsGroupedByName) .build(); long end = System.currentTimeMillis() - start; LOGGER.info(String.format("Scanned %s loaded with time %d ms", entityClass.getName(), end)); return mapping; } @Inject  ClassConverter(Reflections reflections);   ClassConverter(); ClassMapping create(Class<?> entityClass);  }
@Test public void shouldCreateClassMapping() { ClassMapping classMapping = classConverter.create(Person.class); assertEquals("Person", classMapping.getName()); assertEquals(Person.class, classMapping.getClassInstance()); assertEquals(4, classMapping.getFields().size()); assertThat(classMapping.getFieldsName(), containsInAnyOrder("_id", "name", "age", "phones")); }
@Test public void shouldCreateClassMapping2() { ClassMapping classMapping = classConverter.create(Actor.class); assertEquals("Actor", classMapping.getName()); assertEquals(Actor.class, classMapping.getClassInstance()); assertEquals(6, classMapping.getFields().size()); assertThat(classMapping.getFieldsName(), containsInAnyOrder("_id", "name", "age", "phones", "movieCharacter", "movieRating")); }
@Test public void shouldCreateClassMappingWithEmbeddedClass() { ClassMapping classMapping = classConverter.create(Director.class); assertEquals("Director", classMapping.getName()); assertEquals(Director.class, classMapping.getClassInstance()); assertEquals(5, classMapping.getFields().size()); assertThat(classMapping.getFieldsName(), containsInAnyOrder("_id", "name", "age", "phones", "movie")); }
@Test public void shouldReturnFalseWhenThereIsNotKey() { ClassMapping classMapping = classConverter.create(Worker.class); boolean allMatch = classMapping.getFields().stream().noneMatch(FieldMapping::isId); assertTrue(allMatch); }
@Test public void shouldReturnTrueWhenThereIsKey() { ClassMapping classMapping = classConverter.create(User.class); List<FieldMapping> fields = classMapping.getFields(); Predicate<FieldMapping> hasKeyAnnotation = FieldMapping::isId; assertTrue(fields.stream().anyMatch(hasKeyAnnotation)); FieldMapping fieldMapping = fields.stream().filter(hasKeyAnnotation).findFirst().get(); assertEquals("_id", fieldMapping.getName()); assertEquals(DEFAULT, fieldMapping.getType()); }
@Test public void shouldReturnErrorWhenThereIsNotConstructor() { Assertions.assertThrows(ConstructorException.class, () -> classConverter.create(Animal.class)); }
@Test public void shouldReturnWhenIsDefaultConstructor() { ClassMapping classMapping = classConverter.create(Machine.class); List<FieldMapping> fields = classMapping.getFields(); assertEquals(1, fields.size()); }