Search in sources :

Example 1 with GeneralAccessor

use of org.mx.dal.service.GeneralAccessor in project main by JohnPeng739.

the class TestElastic method test.

@Test
public void test() {
    GeneralAccessor accessor = context.getBean("generalAccessorElastic", GeneralAccessor.class);
    assertNotNull(accessor);
    assertEquals(0, accessor.count(UserEntity.class));
    UserEntity user1 = EntityFactory.createEntity(UserEntity.class);
    user1.setAge(45);
    user1.setCode("john");
    user1.setName("John Peng");
    user1.setDesc("我是一个正高级工程师。");
    assertNull(user1.getId());
    UserEntity u1 = accessor.save(user1);
    assertNotNull(u1);
    assertNotNull(user1.getId());
    assertEquals(1, accessor.count(UserEntity.class));
    assertEquals(1, accessor.count(UserEntity.class, true));
    u1 = accessor.getById(user1.getId(), UserEntity.class);
    assertNotNull(u1);
    assertEquals(user1.getCode(), u1.getCode());
    UserEntity user2 = EntityFactory.createEntity(UserEntity.class);
    user2.setId(DigestUtils.uuid());
    user2.setAge(16);
    user2.setCode("joy");
    user2.setName("Joy Peng");
    user2.setDesc("我是一个好学生。");
    assertNotNull(user2.getId());
    u1 = accessor.save(user2);
    assertNotNull(u1);
    assertNotNull(user2.getId());
    assertEquals(2, accessor.count(UserEntity.class));
    assertEquals(2, accessor.count(UserEntity.class, true));
    u1 = accessor.getById(user2.getId(), UserEntity.class);
    assertNotNull(u1);
    assertEquals(user2.getCode(), u1.getCode());
    u1.setAge(17);
    u1.setDesc(u1.getDesc() + "I am a student.");
    accessor.save(u1);
    assertEquals(2, accessor.count(UserEntity.class));
    assertEquals(2, accessor.count(UserEntity.class, true));
    u1 = accessor.getById(user2.getId(), UserEntity.class);
    assertNotNull(u1);
    assertEquals(user2.getCode(), u1.getCode());
    List<UserEntity> list = accessor.find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "john")), UserEntity.class);
    assertNotNull(list);
    assertEquals(1, list.size());
    list = accessor.find(Arrays.asList(new GeneralAccessor.ConditionTuple("name", "peng")), UserEntity.class);
    assertNotNull(list);
    assertEquals(2, list.size());
    list = accessor.find(Arrays.asList(new GeneralAccessor.ConditionTuple("name", "joy")), UserEntity.class);
    assertNotNull(list);
    assertEquals(1, list.size());
    list = accessor.find(Arrays.asList(new GeneralAccessor.ConditionTuple("desc", "高")), UserEntity.class);
    assertNotNull(list);
    assertEquals(1, list.size());
    list = accessor.find(Arrays.asList(new GeneralAccessor.ConditionTuple("desc", "student")), UserEntity.class);
    assertNotNull(list);
    assertEquals(1, list.size());
    accessor.remove(user1);
    assertEquals(1, accessor.count(UserEntity.class));
    assertEquals(1, accessor.count(UserEntity.class, true));
    assertEquals(2, accessor.count(UserEntity.class, false));
    accessor.remove(user1, false);
    accessor.remove(user2, false);
}
Also used : GeneralAccessor(org.mx.dal.service.GeneralAccessor) UserEntity(org.mx.dal.test.entity.UserEntity) Test(org.junit.Test)

Example 2 with GeneralAccessor

use of org.mx.dal.service.GeneralAccessor in project main by JohnPeng739.

the class TestDatabase method testUserInterface.

@Test
public void testUserInterface() {
    GeneralDictAccessor accessor = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(accessor);
    try {
        assertEquals(0, accessor.count(User.class));
        User user = EntityFactory.createEntity(User.class);
        user.setCode("john");
        user.setName("John Peng");
        user.setAddress("address");
        user.setEmail("email");
        user.setPostCode("zip");
        user.setDesc("description");
        User check = accessor.save(user);
        assertNotNull(check);
        assertNotNull(check.getId());
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        assertEquals(user.getAddress(), check.getAddress());
        assertTrue(user.getCreatedTime() > 0);
        assertEquals(1, accessor.count(User.class));
        check = accessor.getById(user.getId(), User.class);
        assertNotNull(check);
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        check = accessor.getByCode(user.getCode(), User.class);
        assertNotNull(check);
        assertEquals(user.getId(), check.getId());
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        user = accessor.getByCode("john", User.class);
        user.setCode("john-1");
        user.setName(user.getName() + "(editable)");
        user.setDesc("I'm John Peng.");
        check = accessor.save(user);
        assertNotNull(check);
        assertEquals(1, accessor.count(User.class));
        assertEquals(user.getId(), check.getId());
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        assertEquals(user.getDesc(), check.getDesc());
        check = accessor.getByCode("john", User.class);
        assertNotNull(check);
        check = accessor.getById(user.getId(), User.class);
        assertNotNull(check);
        assertEquals(user.getCode(), check.getCode());
        check = accessor.getByCode("john-1", User.class);
        // 证明修改代码无效
        assertNull(check);
        user = EntityFactory.createEntity(User.class);
        user.setId(DigestUtils.uuid());
        user.setCode("josh");
        user.setName("Josh Peng");
        check = accessor.save(user);
        assertNotNull(check);
        assertEquals(2, accessor.count(User.class));
        check = accessor.getByCode("john", User.class);
        assertNotNull(check);
        assertTrue(check.isValid());
        check = accessor.getByCode("josh", User.class);
        assertNotNull(check);
        assertTrue(check.isValid());
        user = accessor.getByCode("john", User.class);
        assertNotNull(user);
        check = accessor.remove(user);
        assertNotNull(check);
        assertEquals(1, accessor.count(User.class));
        assertEquals(2, accessor.count(User.class, false));
        assertFalse(check.isValid());
        List<User> list = ((GeneralAccessor) accessor).find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "john"), new GeneralAccessor.ConditionTuple("valid", true)), User.class);
        assertEquals(0, list.size());
        list = ((GeneralAccessor) accessor).find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "josh"), new GeneralAccessor.ConditionTuple("valid", true)), User.class);
        assertEquals(1, list.size());
        user = accessor.getByCode("john", User.class);
        check = accessor.remove(user, false);
        assertNotNull(check);
        assertEquals(1, accessor.count(User.class));
        check = accessor.getById(user.getId(), User.class);
        assertNull(check);
        check = accessor.getByCode(user.getCode(), User.class);
        assertNull(check);
        check = accessor.getByCode("josh", User.class);
        assertNotNull(check);
        user = accessor.getByCode("josh", User.class);
        accessor.remove(user, false);
        assertEquals(0, accessor.count(User.class));
    } catch (UserInterfaceException ex) {
        fail(ex.getMessage());
    }
}
Also used : User(org.mx.test.entity.User) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserInterfaceException(org.mx.error.UserInterfaceException) GeneralAccessor(org.mx.dal.service.GeneralAccessor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 GeneralAccessor (org.mx.dal.service.GeneralAccessor)2 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)1 UserEntity (org.mx.dal.test.entity.UserEntity)1 UserInterfaceException (org.mx.error.UserInterfaceException)1 User (org.mx.test.entity.User)1