@Test public void testCount() throws Exception { IMocksControl control = createControl(); Response resp = control.createMock(Response.class); expect(resp.getStatus()).andReturn(200).times(3); Capture<Class<CloudantCount>> classCapture = new Capture<Class<CloudantCount>>(); expect(resp.readEntity(capture(classCapture))).andReturn(count); replay(resp); WebTarget wt = createMockWebTarget(); Invocation.Builder builder = createBuilder(); expect(builder.get()).andReturn(resp).times(3); replay(builder); expect(wt.path(eq("bluemix-todo"))).andReturn(wt).times(2); expect(wt.path(eq("todos"))).andReturn(wt).times(2); expect(wt.path(eq("_design"))).andReturn(wt).times(2); expect(wt.path(eq("_view"))).andReturn(wt); expect(wt.path(eq("allTodos"))).andReturn(wt); expect(wt.request(eq("application/json"))).andReturn(builder).anyTimes(); replay(wt); CloudantStore store = new CloudantStore(wt); assertEquals(123, store.count()); assertEquals(CloudantCount.class, classCapture.getValue()); verify(resp); verify(wt); verify(builder); }
@Override public int count() throws ToDoStoreException { Response allDocsResp = target.path("_design").path(DESIGN_DOC_NAME).path("_view").path(VIEW_NAME). request(MediaType.APPLICATION_JSON).get(); int status = allDocsResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantCount count = allDocsResp.readEntity(CloudantCount.class); if(count.getRows().size() > 0) { return count.getRows().get(0).getValue(); } else { return 0; } } else { throw new ToDoStoreException("There was an error retrieving the tasks from Cloudant. Error " + status); } }
CloudantStore implements ToDoStore { @Override public int count() throws ToDoStoreException { Response allDocsResp = target.path("_design").path(DESIGN_DOC_NAME).path("_view").path(VIEW_NAME). request(MediaType.APPLICATION_JSON).get(); int status = allDocsResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantCount count = allDocsResp.readEntity(CloudantCount.class); if(count.getRows().size() > 0) { return count.getRows().get(0).getValue(); } else { return 0; } } else { throw new ToDoStoreException("There was an error retrieving the tasks from Cloudant. Error " + status); } } }
CloudantStore implements ToDoStore { @Override public int count() throws ToDoStoreException { Response allDocsResp = target.path("_design").path(DESIGN_DOC_NAME).path("_view").path(VIEW_NAME). request(MediaType.APPLICATION_JSON).get(); int status = allDocsResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantCount count = allDocsResp.readEntity(CloudantCount.class); if(count.getRows().size() > 0) { return count.getRows().get(0).getValue(); } else { return 0; } } else { throw new ToDoStoreException("There was an error retrieving the tasks from Cloudant. Error " + status); } } CloudantStore(WebTarget target); }
CloudantStore implements ToDoStore { @Override public int count() throws ToDoStoreException { Response allDocsResp = target.path("_design").path(DESIGN_DOC_NAME).path("_view").path(VIEW_NAME). request(MediaType.APPLICATION_JSON).get(); int status = allDocsResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantCount count = allDocsResp.readEntity(CloudantCount.class); if(count.getRows().size() > 0) { return count.getRows().get(0).getValue(); } else { return 0; } } else { throw new ToDoStoreException("There was an error retrieving the tasks from Cloudant. Error " + status); } } CloudantStore(WebTarget target); @Override Collection<ToDo> getAll(); @Override ToDo get(String id); @Override ToDo persist(ToDo td); @Override ToDo update(String id, ToDo td); @Override void delete(String id); @Override int count(); }
CloudantStore implements ToDoStore { @Override public int count() throws ToDoStoreException { Response allDocsResp = target.path("_design").path(DESIGN_DOC_NAME).path("_view").path(VIEW_NAME). request(MediaType.APPLICATION_JSON).get(); int status = allDocsResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantCount count = allDocsResp.readEntity(CloudantCount.class); if(count.getRows().size() > 0) { return count.getRows().get(0).getValue(); } else { return 0; } } else { throw new ToDoStoreException("There was an error retrieving the tasks from Cloudant. Error " + status); } } CloudantStore(WebTarget target); @Override Collection<ToDo> getAll(); @Override ToDo get(String id); @Override ToDo persist(ToDo td); @Override ToDo update(String id, ToDo td); @Override void delete(String id); @Override int count();  }