@Test public void testUpdate() throws Exception { IMocksControl control = createControl(); Response resp = control.createMock(Response.class); expect(resp.getStatus()).andReturn(200).times(3); expect(resp.getStatus()).andReturn(201); Capture<Class<CloudantToDo>> classToDoCapture = new Capture<Class<CloudantToDo>>(); expect(resp.readEntity(capture(classToDoCapture))).andReturn(ctd1); CloudantPostResponse postResp = new CloudantPostResponse(); postResp.setId("123"); postResp.setOk(true); postResp.setRev("def"); Capture<Class<CloudantPostResponse>> classCapture = new Capture<Class<CloudantPostResponse>>(); expect(resp.readEntity(capture(classCapture))).andReturn(postResp); replay(resp); WebTarget wt = createMockWebTarget(); Invocation.Builder builder = createBuilder(); ToDo td = new ToDo(); td.setTitle("new text"); td.setId("123"); expect(builder.put(isA(Entity.class))).andReturn(resp); 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); expect(wt.path(eq("_design"))).andReturn(wt).times(1); expect(wt.queryParam(eq("rev"), eq("abc"))).andReturn(wt); expect(wt.path(eq("123"))).andReturn(wt).times(2); expect(wt.request(eq("application/json"))).andReturn(builder).anyTimes(); replay(wt); CloudantStore store = new CloudantStore(wt); ToDo testTd = new ToDo(); testTd.setTitle("new text"); testTd.setId("123"); assertEquals(testTd, store.update("123", td)); assertEquals(CloudantPostResponse.class, classCapture.getValue()); assertEquals(CloudantToDo.class, classToDoCapture.getValue()); verify(resp); verify(wt); verify(builder); }
@Override public ToDo update(String id, ToDo td) throws ToDoStoreException { Response docResp = getRequest(id); int status = docResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantToDo ctd = docResp.readEntity(CloudantToDo.class); CloudantToDo updatedCtd = new CloudantToDo(td); updatedCtd.set_rev(ctd.get_rev()); Response updateReq = target.queryParam(REVISION_PARAM, ctd.get_rev()).path(id). request(MediaType.APPLICATION_JSON).put(Entity.entity(updatedCtd, MediaType.APPLICATION_JSON)); status = updateReq.getStatus(); if(status == HttpURLConnection.HTTP_CREATED) { CloudantPostResponse post = updateReq.readEntity(CloudantPostResponse.class); td.setId(post.getId()); return td; } else { throw new ToDoStoreException("There was an error POSTing the ToDo to Cloudant. Error " + status); } } else { throw new ToDoStoreException("there was an error fetching the ToDo from Cloudant. Error " + status); } }
CloudantStore implements ToDoStore { @Override public ToDo update(String id, ToDo td) throws ToDoStoreException { Response docResp = getRequest(id); int status = docResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantToDo ctd = docResp.readEntity(CloudantToDo.class); CloudantToDo updatedCtd = new CloudantToDo(td); updatedCtd.set_rev(ctd.get_rev()); Response updateReq = target.queryParam(REVISION_PARAM, ctd.get_rev()).path(id). request(MediaType.APPLICATION_JSON).put(Entity.entity(updatedCtd, MediaType.APPLICATION_JSON)); status = updateReq.getStatus(); if(status == HttpURLConnection.HTTP_CREATED) { CloudantPostResponse post = updateReq.readEntity(CloudantPostResponse.class); td.setId(post.getId()); return td; } else { throw new ToDoStoreException("There was an error POSTing the ToDo to Cloudant. Error " + status); } } else { throw new ToDoStoreException("there was an error fetching the ToDo from Cloudant. Error " + status); } } }
CloudantStore implements ToDoStore { @Override public ToDo update(String id, ToDo td) throws ToDoStoreException { Response docResp = getRequest(id); int status = docResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantToDo ctd = docResp.readEntity(CloudantToDo.class); CloudantToDo updatedCtd = new CloudantToDo(td); updatedCtd.set_rev(ctd.get_rev()); Response updateReq = target.queryParam(REVISION_PARAM, ctd.get_rev()).path(id). request(MediaType.APPLICATION_JSON).put(Entity.entity(updatedCtd, MediaType.APPLICATION_JSON)); status = updateReq.getStatus(); if(status == HttpURLConnection.HTTP_CREATED) { CloudantPostResponse post = updateReq.readEntity(CloudantPostResponse.class); td.setId(post.getId()); return td; } else { throw new ToDoStoreException("There was an error POSTing the ToDo to Cloudant. Error " + status); } } else { throw new ToDoStoreException("there was an error fetching the ToDo from Cloudant. Error " + status); } } CloudantStore(WebTarget target); }
CloudantStore implements ToDoStore { @Override public ToDo update(String id, ToDo td) throws ToDoStoreException { Response docResp = getRequest(id); int status = docResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantToDo ctd = docResp.readEntity(CloudantToDo.class); CloudantToDo updatedCtd = new CloudantToDo(td); updatedCtd.set_rev(ctd.get_rev()); Response updateReq = target.queryParam(REVISION_PARAM, ctd.get_rev()).path(id). request(MediaType.APPLICATION_JSON).put(Entity.entity(updatedCtd, MediaType.APPLICATION_JSON)); status = updateReq.getStatus(); if(status == HttpURLConnection.HTTP_CREATED) { CloudantPostResponse post = updateReq.readEntity(CloudantPostResponse.class); td.setId(post.getId()); return td; } else { throw new ToDoStoreException("There was an error POSTing the ToDo to Cloudant. Error " + status); } } else { throw new ToDoStoreException("there was an error fetching the ToDo 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 ToDo update(String id, ToDo td) throws ToDoStoreException { Response docResp = getRequest(id); int status = docResp.getStatus(); if(status == HttpURLConnection.HTTP_OK) { CloudantToDo ctd = docResp.readEntity(CloudantToDo.class); CloudantToDo updatedCtd = new CloudantToDo(td); updatedCtd.set_rev(ctd.get_rev()); Response updateReq = target.queryParam(REVISION_PARAM, ctd.get_rev()).path(id). request(MediaType.APPLICATION_JSON).put(Entity.entity(updatedCtd, MediaType.APPLICATION_JSON)); status = updateReq.getStatus(); if(status == HttpURLConnection.HTTP_CREATED) { CloudantPostResponse post = updateReq.readEntity(CloudantPostResponse.class); td.setId(post.getId()); return td; } else { throw new ToDoStoreException("There was an error POSTing the ToDo to Cloudant. Error " + status); } } else { throw new ToDoStoreException("there was an error fetching the ToDo 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();  }