Skip to content

Commit

Permalink
test: create test to proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Aug 6, 2024
1 parent 430e6a5 commit 86729b5
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2024 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.core.query;

import jakarta.nosql.MappingException;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import jakarta.enterprise.inject.spi.CDI;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.lang.reflect.Method;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import static org.junit.jupiter.api.Assertions.*;

class AbstractRepositoryProxyTest {

private final TestRepositoryProxy proxy = new TestRepositoryProxy();

@Test
void shouldInvokeExecuteFindByQuery() throws Throwable {
Method method = TestRepository.class.getMethod("findEntityById", UUID.class);
Object result = proxy.invoke(proxy, method, new Object[]{UUID.randomUUID()});

assertEquals("executeFindByQuery", result);
}

@Test
void shouldInvokeExecuteDeleteById() throws Throwable {
Method method = TestRepository.class.getMethod("deleteById");
Object result = proxy.invoke(proxy, method, new Object[]{});

assertEquals("executeDeleteByAll", result);
}

@Test
void shouldInvokeExecuteCountById() throws Throwable {
Method method = TestRepository.class.getMethod("countBy");
Object result = proxy.invoke(proxy, method, new Object[]{});
assertEquals("executeCountByQuery", result);
}

@Test
void shouldInvokeExecuteExistById() throws Throwable {
Method method = TestRepository.class.getMethod("existsBy");
Object result = proxy.invoke(proxy, method, new Object[]{});
assertEquals("executeExistByQuery", result);
}

@Test
void shouldInvokeExecuteFindAll() throws Throwable {
Method method = TestRepository.class.getMethod("findAll");
Object result = proxy.invoke(proxy, method, new Object[]{});
assertEquals("executeFindAll", result);
}

@Test
void shouldInvokeExecuteQuery() throws Throwable {
Method method = TestRepository.class.getMethod("query", int.class);
Object result = proxy.invoke(proxy, method, new Object[]{});
assertEquals("executeQuery", result);
}

@Test
void shouldInvokeExecuteCursor() throws Throwable {
Method method = TestRepository.class.getMethod("cursor");
Object result = proxy.invoke(proxy, method, new Object[]{});
assertEquals("executeCursorPagination", result);
}

@Test
void shouldInvokeExecuteFind() throws Throwable {
Method method = TestRepository.class.getMethod("find");
Object result = proxy.invoke(proxy, method, new Object[]{});
assertEquals("executeParameterBased", result);
}

@Test
void shouldInvokeThrowsMappingException() throws Throwable {
Method method = TestRepository.class.getMethod("customMethod");

assertThrows(UnsupportedOperationException.class, () -> {
proxy.invoke(proxy, method, new Object[]{});
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 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.core.query;

import java.util.UUID;

public class TestEntity {

private UUID id;
private String name;

public TestEntity(UUID id, String name) {
this.id = id;
this.name = name;
}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 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.core.query;

import jakarta.data.page.CursoredPage;
import jakarta.data.repository.Find;
import jakarta.data.repository.Query;

import java.util.List;
import java.util.UUID;

public interface TestRepository {

void customMethod();
TestEntity findEntityById(UUID id);
void deleteById();
long countBy();
boolean existsBy();
List<TestEntity> findAll();

@Query("")
List<TestEntity> query(int age);

CursoredPage<TestEntity> cursor();

@Find
List<TestEntity> find();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 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.core.query;

import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.mockito.Mockito;

import java.lang.reflect.Method;
import java.util.UUID;

public class TestRepositoryProxy extends AbstractRepositoryProxy<TestEntity, UUID> {

@Override
protected AbstractRepository<TestEntity, UUID> repository() {
return Mockito.mock(AbstractRepository.class);
}

@Override
protected Class<?> repositoryType() {
return TestRepository.class;
}

@Override
protected EntityMetadata entityMetadata() {
return Mockito.mock(EntityMetadata.class);
}

@Override
protected Object executeQuery(Object instance, Method method, Object[] params) {
return "executeQuery";
}

@Override
protected Object executeDeleteByAll(Object instance, Method method, Object[] params) {
return "executeDeleteByAll";
}

@Override
protected Object executeFindAll(Object instance, Method method, Object[] params) {
return "executeFindAll";
}

@Override
protected Object executeExistByQuery(Object instance, Method method, Object[] params) {
return "executeExistByQuery";
}

@Override
protected Object executeCountByQuery(Object instance, Method method, Object[] params) {
return "executeCountByQuery";
}

@Override
protected Object executeFindByQuery(Object instance, Method method, Object[] params) {
return "executeFindByQuery";
}

@Override
protected Object executeCursorPagination(Object instance, Method method, Object[] params) {
return "executeCursorPagination";
}

@Override
protected Object executeParameterBased(Object instance, Method method, Object[] params) {
return "executeParameterBased";
}
}

0 comments on commit 86729b5

Please sign in to comment.