@Test public void insertComplexStructureTest() throws Exception { Library lib = null; { final String content = serialize(createTestDataInstance()); final String resultStr = doContentRequest( ModelUtils.getQualifiedNameFromEClass(LibraryModelPackage.INSTANCE.getWriterEClass()), content, HttpServletResponse.SC_OK, null, HttpMethod.POST); final ResultType result = (ResultType) deserialize(resultStr).get(0); lib = (Library) result.getInserted().get(0); Assert.assertTrue(lib.getDb_Id() != null); for (Book bk : lib.getBooks()) { Assert.assertTrue(bk.getDb_Id() != null); checkExists(bk, true); } for (Writer w : lib.getWriters()) { Assert.assertTrue(w.getDb_Id() != null); checkExists(w, true); } } // remove all books Writer w = lib.getWriters().get(0); final String newName = "" + System.currentTimeMillis(); // $NON-NLS-1$ w.setName(newName); final Book bk = w.getBooks().get(0); w.getBooks().clear(); { final String content = serialize(w); final String resultStr = doContentRequest( ModelUtils.getQualifiedNameFromEClass(LibraryModelPackage.INSTANCE.getWriterEClass()), content, HttpServletResponse.SC_OK, null, HttpMethod.POST); final ResultType resultType = (ResultType) deserialize(resultStr).get(0); final Writer wResult = (Writer) resultType.getUpdated().get(0); Assert.assertNotSame(w, wResult); Assert.assertEquals(w.getDb_Id(), wResult.getDb_Id()); Assert.assertEquals(0, wResult.getBooks().size()); // use the new writer as the version may have changed w = wResult; checkExists(w, true); } { final String content = doGetRequest( ModelUtils.getQualifiedNameFromEClass(LibraryModelPackage.INSTANCE.getWriterEClass()), null, HttpServletResponse.SC_OK); final List<Object> objects = deserialize(content); System.err.println(objects.size()); } // and add one book again { w.getBooks().add(bk); bk.setAuthor(w); final String content = serialize(w); final String resultStr = doContentRequest( ModelUtils.getQualifiedNameFromEClass(LibraryModelPackage.INSTANCE.getWriterEClass()), content, HttpServletResponse.SC_OK, null, HttpMethod.POST); final ResultType resultType = (ResultType) deserialize(resultStr).get(0); final Writer wResult = (Writer) resultType.getUpdated().get(0); Assert.assertNotSame(w, wResult); Assert.assertEquals(w.getDb_Id(), wResult.getDb_Id()); Assert.assertEquals(1, wResult.getBooks().size()); // disabled test as books of a writer are proxied // Assert.assertEquals(wResult.getBooks().get(0).getAuthor(), wResult); } { final Writer w2 = (Writer) deserialize(getOneObject(w)).get(0); Assert.assertNotSame(w, w2); Assert.assertEquals(newName, w2.getName()); Assert.assertEquals(w.getDb_Id(), w2.getDb_Id()); Assert.assertEquals(1, w2.getBooks().size()); Assert.assertEquals(bk.getDb_Id(), w2.getBooks().get(0).getDb_Id()); } }
@Test public void createAndPageTest() { final int COUNT = 35; final List<Writer> insertedWs = new ArrayList<Writer>(); for (int i = 0; i < COUNT; i++) { final Writer w = LibraryModelPackage.MODELFACTORY.createWriter(); w.setName("name" + i); // $NON-NLS-1$ final String content = serialize(w); final String resultStr = doContentRequest( LibraryModelPackage.INSTANCE.getWriterEClass().getName(), content, HttpServletResponse.SC_OK, null, HttpMethod.POST); final ResultType result = (ResultType) deserialize(resultStr).get(0); Assert.assertEquals(1, result.getInserted().size()); Assert.assertEquals(0, result.getUpdated().size()); Assert.assertEquals(0, result.getDeleted().size()); final Writer insertedW = (Writer) result.getInserted().get(0); Assert.assertEquals(w.getName(), insertedW.getName()); checkExists(insertedW, true); insertedWs.add(insertedW); } final int[] expectedCount = new int[] {10, 10, 10, 5, 0}; int startRow = 0; final List<Writer> checkedWs = new ArrayList<Writer>(insertedWs); for (int i = 0; i < expectedCount.length; i++) { final String wsPartUrl = ModelUtils.getQualifiedNameFromEClass(LibraryModelPackage.INSTANCE.getWriterEClass()) + "?firstResult=" + startRow //$NON-NLS-1$ + "&maxResults=10"; //$NON-NLS-1$ final String content = doGetRequest(wsPartUrl, null, HttpServletResponse.SC_OK); final ResponseType result = (ResponseType) deserialize(content).get(0); Assert.assertEquals(COUNT, result.getTotalRows()); Assert.assertEquals(expectedCount[i], result.getData().size()); if (result.getData().size() > 0) { Assert.assertEquals(startRow, result.getStartRow()); Assert.assertEquals(startRow + expectedCount[i] - 1, result.getEndRow()); } // check that the data exists for (Object o : result.getData()) { final Writer w = (Writer) o; boolean found = false; for (Writer insertedW : insertedWs) { if (w.getDb_Id() == insertedW.getDb_Id()) { checkedWs.remove(insertedW); found = true; break; } } Assert.assertTrue(found); } startRow += expectedCount[i]; } Assert.assertTrue(checkedWs.isEmpty()); }