Search in sources :

Example 11 with DataSet

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

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectOneToManyDirectsLoadedOk.

@Test
public void testResolveReferencedObjects_ExpectOneToManyDirectsLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("required");
    d2i1.setLinkToFullDonwload("required");
    d2i1 = testDto2Service.create(d2i1);
    TestDto3 d3i1 = new TestDto3();
    d3i1.setLinkToDtoTwo(d2i1.getId());
    d3i1 = testDto3Service.create(d3i1);
    TestDto3 d3i2 = new TestDto3();
    d3i2.setLinkToDtoTwo(d2i1.getId());
    d3i2 = testDto3Service.create(d3i2);
    DataSet result = new DataSet();
    result.get(testDto2Service.getEntityTypeMessageCode()).put(d2i1);
    loader.resolveReferencedObjects(result, Refs.ref3to2mand.reverse());
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i1.getId()));
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i2.getId()));
}
Also used : DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 12 with DataSet

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

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectM2mLoadedOkOk.

@Test
public void testResolveReferencedObjects_ExpectM2mLoadedOkOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto3 d3i1 = new TestDto3();
    // non-existing link
    d3i1.setLinkToDtoTwo(1L);
    d3i1 = testDto3Service.create(d3i1);
    TestDto1 d1i1 = new TestDto1();
    d1i1.setEnv(d3i1.getId());
    d1i1.setLinkToFullDonwload("required");
    d1i1 = testDto1Service.create(d1i1);
    TestDto1 d1i2 = new TestDto1();
    d1i2.setEnv(d3i1.getId());
    d1i2.setLinkToFullDonwload("required");
    d1i2 = testDto1Service.create(d1i2);
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("required");
    d2i1.setLinkToFullDonwload("required");
    d2i1 = testDto2Service.create(d2i1);
    m2mService.addReferencee(d2i1.getId(), d1i1.getId());
    m2mService.addReferencee(d2i1.getId(), d1i2.getId());
    // -----------------------------------
    String t1 = testDto1Service.getEntityTypeMessageCode();
    String t2 = testDto2Service.getEntityTypeMessageCode();
    String t3 = testDto3Service.getEntityTypeMessageCode();
    DataSet result = new DataSet();
    loader.loadObjectAndItsRefs(d2i1.getId(), t2, result, Refs.ref2to1m2m, Refs.ref1to3);
    assertNotNull(result.get(t2).find(d2i1.getId()));
    assertNotNull(result.get(t1).find(d1i1.getId()));
    assertNotNull(result.get(t1).find(d1i2.getId()));
    assertNotNull(result.get(t3).find(d3i1.getId()));
    Set<Object> d2refsTo1 = result.get(t2).getBackRefs().getForRow(d2i1).getForRef(Refs.ref2to1m2m);
    assertEquals(2, d2refsTo1.size());
    assertTrue(d2refsTo1.contains(d1i1.getId()));
    assertTrue(d2refsTo1.contains(d1i2.getId()));
}
Also used : DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 13 with DataSet

use of org.summerb.easycrud.api.dto.datapackage.DataSet 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 14 with DataSet

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

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectOneToManyRecoursiveLoadedOk.

@Test
public void testResolveReferencedObjects_ExpectOneToManyRecoursiveLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("required");
    d2i1.setLinkToFullDonwload("required");
    d2i1 = testDto2Service.create(d2i1);
    TestDto3 d3i1 = new TestDto3();
    d3i1.setLinkToDtoTwo(d2i1.getId());
    d3i1 = testDto3Service.create(d3i1);
    TestDto1 d1i1 = new TestDto1();
    d1i1.setEnv("d1i1");
    d1i1.setLinkToFullDonwload("required");
    d1i1 = testDto1Service.create(d1i1);
    TestDto3 d3i2 = new TestDto3();
    d3i2.setLinkToDtoTwo(d2i1.getId());
    d3i2.setLinkToDtoOneOptional(d1i1.getId());
    d3i2 = testDto3Service.create(d3i2);
    DataSet result = new DataSet();
    result.get(testDto2Service.getEntityTypeMessageCode()).put(d2i1);
    loader.resolveReferencedObjects(result, Refs.ref3to2mand.reverse(), Refs.ref3to1);
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i1.getId()));
    assertNotNull(result.get(testDto3Service.getEntityTypeMessageCode()).find(d3i2.getId()));
    assertNotNull(result.get(testDto1Service.getEntityTypeMessageCode()).find(d1i1.getId()));
}
Also used : DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 15 with DataSet

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

the class DataSetLoaderTest method testResolveReferencedObjects_ExpectBackReferencesLoadedOk.

@Test
public void testResolveReferencedObjects_ExpectBackReferencesLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
    DataSetLoader loader = buildLoaderCase1();
    TestDto2 d2i1 = new TestDto2();
    d2i1.setEnv("required");
    d2i1.setLinkToFullDonwload("required");
    d2i1 = testDto2Service.create(d2i1);
    TestDto1 d1i1 = new TestDto1();
    d1i1.setEnv("required");
    d1i1.setLinkToFullDonwload("required");
    d1i1 = testDto1Service.create(d1i1);
    TestDto3 d3i1 = new TestDto3();
    d3i1.setLinkToDtoTwo(d2i1.getId());
    d3i1 = testDto3Service.create(d3i1);
    TestDto3 d3i2 = new TestDto3();
    d3i2.setLinkToDtoTwo(d2i1.getId());
    d3i2.setLinkToDtoOneOptional(d1i1.getId());
    d3i2 = testDto3Service.create(d3i2);
    String t1 = testDto1Service.getEntityTypeMessageCode();
    String t2 = testDto2Service.getEntityTypeMessageCode();
    String t3 = testDto3Service.getEntityTypeMessageCode();
    DataSet result = new DataSet();
    result.get(t2).put(d2i1);
    result.get(t1).put(d1i1);
    loader.resolveReferencedObjects(result, Refs.ref3to2mand.reverse(), Refs.ref3to1.reverse());
    // NOTE: This is ugly construction! We definitely need some sugar here
    assertEquals(1, result.get(t1).getBackRefs().getForRow(d1i1).getForRef(Refs.ref3to1.reverse()).size());
    assertEquals(2, result.get(t2).getBackRefs().getForRow(d2i1).getForRef(Refs.ref3to2mand.reverse()).size());
    assertNotNull(result.get(t3).find(d3i1.getId()));
    assertNotNull(result.get(t3).find(d3i2.getId()));
}
Also used : DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Aggregations

DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)16 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Test (org.junit.Test)9 DataSetLoader (org.summerb.easycrud.api.relations.DataSetLoader)9 Ref (org.summerb.easycrud.api.dto.relations.Ref)7 HasId (org.summerb.easycrud.api.dto.HasId)4 DataTable (org.summerb.easycrud.api.dto.datapackage.DataTable)3 List (java.util.List)2 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)2 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)2 Preconditions (com.google.common.base.Preconditions)1 TestDto1 (integr.org.summerb.easycrud.TestDto1)1 TestDto2 (integr.org.summerb.easycrud.TestDto2)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Function (java.util.function.Function)1