Skip to content

Commit

Permalink
feat: include document validation to person
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Jun 1, 2024
1 parent 2af7b0c commit 40ec744
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.document.query;

import jakarta.enterprise.context.spi.CreationalContext;
import org.eclipse.jnosql.mapping.DatabaseQualifier;
import org.eclipse.jnosql.mapping.DatabaseType;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.spi.AbstractBean;
import org.eclipse.jnosql.mapping.core.util.AnnotationLiteralUtil;
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.semistructured.query.CustomRepositoryHandler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;


/**
* This class serves as a JNoSQL discovery bean for CDI extension, responsible for registering Custom Repository instances.
* It extends {@link AbstractBean} and is parameterized with type {@code T} representing the repository type.
* <p>
* Upon instantiation, it initializes with the provided repository type, provider name, and qualifiers.
* The provider name specifies the database provider for the repository.
* </p>
*
* @param <T> the type of the repository
* @see AbstractBean
*/
public class CustomRepositoryDocumentBean<T> extends AbstractBean<T> {

private final Class<T> type;

private final Set<Type> types;

private final String provider;

private final Set<Annotation> qualifiers;

/**
* Constructor
*
* @param type the tye
* @param provider the provider name, that must be a
*/
@SuppressWarnings("unchecked")
public CustomRepositoryDocumentBean(Class<?> type, String provider) {
this.type = (Class<T>) type;
this.types = Collections.singleton(type);
this.provider = provider;
if (provider.isEmpty()) {
this.qualifiers = new HashSet<>();
qualifiers.add(DatabaseQualifier.ofDocument());
qualifiers.add(AnnotationLiteralUtil.DEFAULT_ANNOTATION);
qualifiers.add(AnnotationLiteralUtil.ANY_ANNOTATION);
} else {
this.qualifiers = Collections.singleton(DatabaseQualifier.ofDocument(provider));
}
}

@Override
public Class<?> getBeanClass() {
return type;
}

@SuppressWarnings("unchecked")
@Override
public T create(CreationalContext<T> context) {
var entities = getInstance(EntitiesMetadata.class);
var template = provider.isEmpty() ? getInstance(DocumentTemplate.class) :
getInstance(DocumentTemplate.class, DatabaseQualifier.ofDocument(provider));

var converters = getInstance(Converters.class);

var handler = CustomRepositoryHandler.builder()
.entitiesMetadata(entities)
.template(template)
.customRepositoryType(type)
.converters(converters)
.build();

return (T) Proxy.newProxyInstance(type.getClassLoader(),
new Class[]{type},
handler);
}


@Override
public Set<Type> getTypes() {
return types;
}

@Override
public Set<Annotation> getQualifiers() {
return qualifiers;
}

@Override
public String getId() {
return type.getName() + '@' + DatabaseType.DOCUMENT + "-" + provider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jnosql.mapping.DatabaseMetadata;
import org.eclipse.jnosql.mapping.DatabaseType;
import org.eclipse.jnosql.mapping.Databases;
import org.eclipse.jnosql.mapping.document.query.CustomRepositoryDocumentBean;
import org.eclipse.jnosql.mapping.document.query.RepositoryDocumentBean;
import org.eclipse.jnosql.mapping.metadata.ClassScanner;

Expand Down Expand Up @@ -56,8 +57,10 @@ void onAfterBeanDiscovery(@Observes final AfterBeanDiscovery afterBeanDiscovery)

Set<Class<?>> crudTypes = scanner.repositoriesStandard();

LOGGER.info(String.format("Processing Document extension: %d databases crud %d found",
databases.size(), crudTypes.size()));
Set<Class<?>> customRepositories = scanner.customRepositories();

LOGGER.info(String.format("Processing Document extension: %d databases crud %d found, custom repositories: %d",
databases.size(), crudTypes.size(), customRepositories.size()));
LOGGER.info("Processing repositories as a Document implementation: " + crudTypes);

databases.forEach(type -> {
Expand All @@ -76,6 +79,14 @@ void onAfterBeanDiscovery(@Observes final AfterBeanDiscovery afterBeanDiscovery)
afterBeanDiscovery.addBean(new RepositoryDocumentBean<>(type, database.getProvider())));
});

customRepositories.forEach(type -> {
if (!databases.contains(DatabaseMetadata.DEFAULT_DOCUMENT)) {
afterBeanDiscovery.addBean(new CustomRepositoryDocumentBean<>(type, ""));
}
databases.forEach(database ->
afterBeanDiscovery.addBean(new CustomRepositoryDocumentBean<>(type, database.getProvider())));
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
import jakarta.data.repository.Insert;
import jakarta.data.repository.Repository;

import java.util.List;

@Repository
public interface People {

@Insert
List<Person> insert(List<Person> people);
Person insert(Person person);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package org.eclipse.jnosql.mapping.document.spi;

import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.mapping.Database;
import org.eclipse.jnosql.mapping.DatabaseType;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension;
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
import org.eclipse.jnosql.mapping.document.MockProducer;
import org.eclipse.jnosql.mapping.document.entities.People;
import org.eclipse.jnosql.mapping.document.entities.Person;
import org.eclipse.jnosql.mapping.reflection.Reflections;
import org.eclipse.jnosql.mapping.semistructured.EntityConverter;
import org.jboss.weld.junit5.auto.AddExtensions;
Expand Down Expand Up @@ -55,15 +57,32 @@ class DocumentCustomExtensionTest {
@Test
void shouldInitiate() {
assertNotNull(people);
Person person = people.insert(Person.builder().build());
SoftAssertions.assertSoftly(soft -> {
soft.assertThat(person).isNotNull();
soft.assertThat(person.getName()).isEqualTo("Default");
});
}

@Test
void shouldUseMock(){
assertNotNull(pepoleMock);

Person person = pepoleMock.insert(Person.builder().build());
SoftAssertions.assertSoftly(soft -> {
soft.assertThat(person).isNotNull();
soft.assertThat(person.getName()).isEqualTo("documentRepositoryMock");
});
}

@Test
void shouldUseDefault(){
assertNotNull(repository);

Person person = repository.insert(Person.builder().build());
SoftAssertions.assertSoftly(soft -> {
soft.assertThat(person).isNotNull();
soft.assertThat(person.getName()).isEqualTo("Default");
});
}
}

0 comments on commit 40ec744

Please sign in to comment.