Search in sources :

Example 16 with EasyCrudService

use of org.summerb.easycrud.api.EasyCrudService in project summerb by skarpushin.

the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectTwoDifferentObjectsLoadedOk.

@Test
public void testLoadObjectsByIds_ExpectTwoDifferentObjectsLoadedOk() throws Exception {
    DataSetLoaderImpl fixture = buildMockedInstance();
    EasyCrudService service1 = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service1);
    TestDto1 dto1 = new TestDto1();
    dto1.setId("d1");
    when(service1.findById("d1")).thenReturn(dto1);
    EasyCrudService service2 = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto2")).thenReturn(service2);
    TestDto2 dto2 = new TestDto2();
    dto2.setId(2L);
    when(service2.findById(2L)).thenReturn(dto2);
    DataSet ret = new DataSet();
    Map<String, Set<Object>> ids = new HashMap<>();
    ids.put("dto1", ids("d1"));
    ids.put("dto2", ids(2L));
    fixture.loadObjectsByIds(ids, ret);
    assertNotNull(ret.get("dto1"));
    assertNotNull(ret.get("dto1").find("d1"));
    assertNotNull(ret.get("dto2"));
    assertNotNull(ret.get("dto2").find(2L));
}
Also used : EasyCrudService(org.summerb.easycrud.api.EasyCrudService) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) HashMap(java.util.HashMap) TestDto1(integr.org.summerb.easycrud.TestDto1) TestDto2(integr.org.summerb.easycrud.TestDto2) Test(org.junit.Test)

Example 17 with EasyCrudService

use of org.summerb.easycrud.api.EasyCrudService in project summerb by skarpushin.

the class EasyCrudScaffoldImpl method discoverDtoClassFromServiceInterface.

@SuppressWarnings("unchecked")
protected <TId, TDto extends HasId<TId>, TService extends EasyCrudService<TId, TDto>> Class<TDto> discoverDtoClassFromServiceInterface(Class<TService> serviceInterface) {
    Preconditions.checkArgument(EasyCrudService.class.isAssignableFrom(serviceInterface), "Service interface is supposed to be a subclass of EasyCrudService");
    ParameterizedType easyCrudServiceType = null;
    for (int i = 0; i < serviceInterface.getGenericInterfaces().length; i++) {
        Type candidate = serviceInterface.getGenericInterfaces()[i];
        if (!(candidate instanceof ParameterizedType)) {
            continue;
        }
        ParameterizedType candidatePt = (ParameterizedType) candidate;
        if (EasyCrudService.class.equals(candidatePt.getRawType())) {
            easyCrudServiceType = candidatePt;
            break;
        }
    }
    Preconditions.checkState(easyCrudServiceType != null, "Wasn't able to located parent interface EasyCrudService");
    Type ret = easyCrudServiceType.getActualTypeArguments()[1];
    Preconditions.checkArgument(DtoBase.class.isAssignableFrom((Class<?>) ret), "DTO class supposed to impl DtoBase interface");
    Preconditions.checkArgument(HasId.class.isAssignableFrom((Class<?>) ret), "DTO class supposed to impl HasId interface");
    return (Class<TDto>) ret;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) DtoBase(org.summerb.utils.DtoBase)

Aggregations

EasyCrudService (org.summerb.easycrud.api.EasyCrudService)17 Test (org.junit.Test)8 HasId (org.summerb.easycrud.api.dto.HasId)8 TestDto1 (integr.org.summerb.easycrud.TestDto1)5 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)5 Query (org.summerb.easycrud.api.query.Query)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 PagerParams (org.summerb.easycrud.api.dto.PagerParams)3 DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Ref (org.summerb.easycrud.api.dto.relations.Ref)2 TestDto2 (integr.org.summerb.easycrud.TestDto2)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Entry (java.util.Map.Entry)1 UUID (java.util.UUID)1