Search in sources :

Example 1 with Query

use of jakarta.nosql.mapping.Query in project jnosql-diana by eclipse.

the class DynamicQueryMethodReturnTest method shouldReturnFromPrepareStatement.

@Test
public void shouldReturnFromPrepareStatement() throws NoSuchMethodException {
    PreparedStatement preparedStatement = Mockito.mock(PreparedStatement.class);
    Mockito.when(preparedStatement.<Person>getResult()).thenReturn(Stream.of(new Person("Ada")));
    Method method = getMethod(PersonRepository.class, "query");
    Function<String, Stream<?>> stream = q -> Stream.of(new Person("Ada"));
    DynamicQueryMethodReturn dynamicReturn = DynamicQueryMethodReturn.builder().withTypeClass(Person.class).withMethod(method).withQueryConverter(stream).withArgs(new Object[] { "Ada" }).withPrepareConverter(s -> preparedStatement).build();
    Object execute = dynamicReturn.execute();
    Assertions.assertTrue(execute instanceof Iterable);
    Iterable<Person> persons = (List) execute;
    Assertions.assertEquals(new Person("Ada"), persons.iterator().next());
}
Also used : NonUniqueResultException(jakarta.nosql.NonUniqueResultException) Collection(java.util.Collection) Set(java.util.Set) Function(java.util.function.Function) Query(jakarta.nosql.mapping.Query) Repository(jakarta.nosql.mapping.Repository) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PreparedStatement(jakarta.nosql.mapping.PreparedStatement) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) Queue(java.util.Queue) Method(java.lang.reflect.Method) Param(jakarta.nosql.mapping.Param) PreparedStatement(jakarta.nosql.mapping.PreparedStatement) Stream(java.util.stream.Stream) List(java.util.List) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

NonUniqueResultException (jakarta.nosql.NonUniqueResultException)1 Param (jakarta.nosql.mapping.Param)1 PreparedStatement (jakarta.nosql.mapping.PreparedStatement)1 Query (jakarta.nosql.mapping.Query)1 Repository (jakarta.nosql.mapping.Repository)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Queue (java.util.Queue)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 Assertions (org.junit.jupiter.api.Assertions)1 Test (org.junit.jupiter.api.Test)1 Mockito (org.mockito.Mockito)1