Search in sources :

Example 11 with HasId

use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.

the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectManyLoadByLongsOk.

@Test
public void testLoadObjectsByIds_ExpectManyLoadByLongsOk() throws Exception {
    DataSetLoaderImpl fixture = buildMockedInstance();
    EasyCrudService service = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service);
    Matcher<Query> matcher = IsEqual.equalTo(Query.n().in(HasId.FN_ID, new Long[] { 1L, 2L }));
    PaginatedList mockret = new PaginatedList<>(new PagerParams(), Arrays.asList(new TestDto1(), new TestDto1()), 2);
    when(service.query(any(PagerParams.class), argThat(matcher))).thenReturn(mockret);
    List<HasId> ret = fixture.loadObjectsByIds(ids(1L, 2L), "dto1");
    assertNotNull(ret);
    assertEquals(2, ret.size());
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) Query(org.summerb.easycrud.api.query.Query) PagerParams(org.summerb.easycrud.api.dto.PagerParams) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) TestDto1(integr.org.summerb.easycrud.TestDto1) Test(org.junit.Test)

Example 12 with HasId

use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.

the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectOneLoadOk.

@Test
public void testLoadObjectsByIds_ExpectOneLoadOk() throws Exception {
    DataSetLoaderImpl fixture = buildMockedInstance();
    EasyCrudService service = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service);
    TestDto1 dto = new TestDto1();
    when(service.findById(1)).thenReturn(dto);
    List<HasId> ret = fixture.loadObjectsByIds(ids(1), "dto1");
    assertNotNull(ret);
    assertEquals(1, ret.size());
    assertEquals(dto, ret.get(0));
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) TestDto1(integr.org.summerb.easycrud.TestDto1) Test(org.junit.Test)

Example 13 with HasId

use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.

the class DomLoaderDeviceGatewayTest method testResolveCollectionElementType_expectCorrectFieldTypeResolution.

@Test
public void testResolveCollectionElementType_expectCorrectFieldTypeResolution() {
    // Deps and fixture
    DataSetLoader dataSetLoader = mock(DataSetLoader.class);
    EasyCrudServiceResolver easyCrudServiceResolver = mock(EasyCrudServiceResolver.class);
    DomLoaderImpl f = new DomLoaderImpl(dataSetLoader, easyCrudServiceResolver);
    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(Env.class, "devices");
    // test
    Class<HasId<Object>> elType = f.resolveCollectionElementType(pd);
    assertEquals(Device.class, elType);
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudServiceResolver(org.summerb.easycrud.api.EasyCrudServiceResolver) PropertyDescriptor(java.beans.PropertyDescriptor) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 14 with HasId

use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.

the class DataSetLoaderImpl method loadObjectsByIds.

@Override
public List<HasId> loadObjectsByIds(Set<Object> ids, String entityTypeName) throws EntityNotFoundException, NotAuthorizedException {
    Preconditions.checkArgument(!CollectionUtils.isEmpty(ids));
    EasyCrudService service = easyCrudServiceResolver.resolveByEntityType(entityTypeName);
    Object firstId = ids.iterator().next();
    List<HasId> ret = new ArrayList<>(ids.size());
    if (ids.size() == 1) {
        HasId loaded = loadOne(firstId, service);
        ret.add(loaded);
    } else if (firstId instanceof Long || firstId instanceof String) {
        Query query;
        if (firstId instanceof String) {
            query = Query.n().in(HasId.FN_ID, ids.toArray(new String[0]));
        } else {
            query = Query.n().in(HasId.FN_ID, ids.toArray(new Long[0]));
        }
        List loaded = loadMultipleByQuery(query, service);
        assertFound(loaded.size() == ids.size(), entityTypeName, "One of: " + Arrays.toString(ids.toArray()));
        ret.addAll(loaded);
    } else {
        List<HasId> loaded = loadOneByOne(ids, service);
        ret.addAll(loaded);
    }
    return ret;
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) Query(org.summerb.easycrud.api.query.Query) ArrayList(java.util.ArrayList) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 15 with HasId

use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.

the class DataSetLoaderImpl method loadOneByOne.

private List<HasId> loadOneByOne(Collection ids, EasyCrudService<Object, HasId> service) throws NotAuthorizedException, GenericEntityNotFoundException {
    List<HasId> ret = new LinkedList<>();
    for (Object id : ids) {
        HasId loaded = loadOne(id, service);
        ret.add(loaded);
    }
    return ret;
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) LinkedList(java.util.LinkedList)

Aggregations

HasId (org.summerb.easycrud.api.dto.HasId)20 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)9 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)5 Ref (org.summerb.easycrud.api.dto.relations.Ref)5 TestDto1 (integr.org.summerb.easycrud.TestDto1)4 LinkedList (java.util.LinkedList)4 DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)4 Query (org.summerb.easycrud.api.query.Query)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 DataTable (org.summerb.easycrud.api.dto.datapackage.DataTable)3 DataSetLoader (org.summerb.easycrud.api.relations.DataSetLoader)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 PropertyAccessor (org.springframework.beans.PropertyAccessor)2