@Test public void shouldNotAddRelation() { List<Long> relationIds = Arrays.asList(1l, 2l, 3l, 4l); OneToManyModel model = new OneToManyModel(1l); save(model); for (long i : relationIds) { save(new OneToManyRelationModel(i, model)); } save(new OneToManyRelationModel(5l, null)); OneToManyModel result = findById(OneToManyModel.class, 1l); Assert.assertEquals(4, result.getModels().size()); Assert.assertTrue(relationIds.contains(result.getModels().get(0).getId())); Assert.assertTrue(relationIds.contains(result.getModels().get(1).getId())); Assert.assertTrue(relationIds.contains(result.getModels().get(2).getId())); Assert.assertTrue(relationIds.contains(result.getModels().get(3).getId())); Assert.assertEquals(result, result.getModels().get(0).getModel()); Assert.assertEquals(result, result.getModels().get(1).getModel()); Assert.assertEquals(result, result.getModels().get(2).getModel()); Assert.assertEquals(result, result.getModels().get(3).getModel()); }
@Test public void shouldNotRemoveRelation() { OneToManyModel model = new OneToManyModel(1l); save(model); for (long i : Arrays.asList(1l, 2l, 3l, 4l)) { save(new OneToManyRelationModel(i, model)); } OneToManyModel result = findById(OneToManyModel.class, 1l); result.getModels().clear(); save(model); result = findById(OneToManyModel.class, 1l); Assert.assertEquals(4, result.getModels().size()); }
@Test public void shouldRemoveOneOfManyToOneRelation() { OneToManyModel model = new OneToManyModel(1l); save(model); for (long i : Arrays.asList(1l, 2l, 3l, 4l)) { save(new OneToManyRelationModel(i, model)); } OneToManyModel result = findById(OneToManyModel.class, 1l); Assert.assertEquals(4, result.getModels().size()); OneToManyRelationModel.deleteAll(OneToManyRelationModel.class, "id = ?", String.valueOf(3l)); result = findById(OneToManyModel.class, 1l); Assert.assertEquals(3, result.getModels().size()); Assert.assertTrue(result.getModels().get(0).getId() != 3l); Assert.assertTrue(result.getModels().get(1).getId() != 3l); Assert.assertTrue(result.getModels().get(2).getId() != 3l); }