Program: 160298
Project: 160298
Buggy source:
/** 
 * Do a pull replication TODO - instead calling server.takeRequest, call dispatcher.takeRequest and pass a path regex
 * @param shutdownMockWebserver - should this test shutdown the mockwebserverwhen done?  if another test wants to pick up where this left off, you should pass false.
 * @param serverType - should the mock return the Sync Gateway server type inthe "Server" HTTP Header?  this changes the behavior of the replicator to use bulk_get and POST reqeusts for _changes feeds.
 * @param addAttachments - should the mock sync gateway return docs with attachments?
 * @return a map that contains the mockwebserver (key="server") and the mock dispatcher(key="dispatcher")
 */
public Map<String,Object> mockSinglePull(boolean shutdownMockWebserver,MockDispatcher.ServerType serverType,boolean addAttachments) throws Exception {
  String doc1Id="String_Node_Str";
  String doc1Rev="String_Node_Str";
  String doc2Id="String_Node_Str";
  String doc2Rev="String_Node_Str";
  String doc1AttachName="String_Node_Str";
  String doc2AttachName="String_Node_Str";
  int doc1Seq=1;
  int doc2Seq=2;
  MockWebServer server=MockHelper.getMockWebServer();
  MockDispatcher dispatcher=new MockDispatcher();
  dispatcher.setServerType(serverType);
  server.setDispatcher(dispatcher);
  MockResponse fakeCheckpointResponse=new MockResponse();
  MockHelper.set404NotFoundJson(fakeCheckpointResponse);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,fakeCheckpointResponse);
  MockChangesFeed mockChangesFeed=new MockChangesFeed();
  MockChangedDoc mockChangedDoc1=new MockChangedDoc().setSeq(doc1Seq).setDocId(doc1Id).setChangedRevIds(Arrays.asList(doc1Rev));
  mockChangesFeed.add(mockChangedDoc1);
  MockChangedDoc mockChangedDoc2=new MockChangedDoc().setSeq(doc2Seq).setDocId(doc2Id).setChangedRevIds(Arrays.asList(doc2Rev));
  mockChangesFeed.add(mockChangedDoc2);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHANGES,mockChangesFeed.generateMockResponse());
  Map<String,Object> doc1JsonMap=MockHelper.generateRandomJsonMap();
  MockDocumentGet mockDocumentGet=new MockDocumentGet().setDocId(doc1Id).setRev(doc1Rev).setJsonMap(doc1JsonMap);
  if (addAttachments) {
    mockDocumentGet.addAttachmentFilename(doc1AttachName);
  }
  dispatcher.enqueueResponse("String_Node_Str",mockDocumentGet.generateMockResponse());
  Map<String,Object> doc2JsonMap=MockHelper.generateRandomJsonMap();
  mockDocumentGet=new MockDocumentGet().setDocId(doc2Id).setRev(doc2Rev).setJsonMap(doc2JsonMap);
  if (addAttachments) {
    mockDocumentGet.addAttachmentFilename(doc2AttachName);
  }
  dispatcher.enqueueResponse("String_Node_Str",mockDocumentGet.generateMockResponse());
  MockCheckpointPut mockCheckpointPut=new MockCheckpointPut();
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointPut);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointPut);
  server.play();
  Replication pullReplication=doPullReplication(server.getUrl("String_Node_Str"));
  assertNotNull(database);
  Document doc1=database.getDocument(doc1Id);
  assertNotNull(doc1);
  assertNotNull(doc1.getCurrentRevisionId());
  assertTrue(doc1.getCurrentRevisionId().startsWith("String_Node_Str"));
  assertNotNull(doc1.getProperties());
  assertEquals(doc1JsonMap,doc1.getUserProperties());
  Document doc2=database.getDocument(doc2Id);
  assertNotNull(doc2);
  assertNotNull(doc2.getCurrentRevisionId());
  assertNotNull(doc2.getProperties());
  assertTrue(doc2.getCurrentRevisionId().startsWith("String_Node_Str"));
  assertEquals(doc2JsonMap,doc2.getUserProperties());
  if (addAttachments) {
    attachmentAsserts(doc1AttachName,doc1);
    attachmentAsserts(doc2AttachName,doc2);
  }
  RecordedRequest getCheckpointRequest=server.takeRequest();
  assertTrue(getCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(getCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  RecordedRequest getChangesFeedRequest=server.takeRequest();
  if (serverType == MockDispatcher.ServerType.SYNC_GW) {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
 else {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
  assertTrue(getChangesFeedRequest.getPath().matches(MockHelper.PATH_REGEX_CHANGES));
  RecordedRequest doc1Request=server.takeRequest();
  assertTrue(doc1Request.getMethod().equals("String_Node_Str"));
  assertTrue(doc1Request.getPath().matches("String_Node_Str"));
  RecordedRequest doc2Request=server.takeRequest();
  assertTrue(doc2Request.getMethod().equals("String_Node_Str"));
  assertTrue(doc2Request.getPath().matches("String_Node_Str"));
  RecordedRequest putCheckpointRequest=server.takeRequest();
  assertTrue(putCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(putCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  putCheckpointRequest=server.takeRequest();
  assertTrue(putCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(putCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  if (shutdownMockWebserver) {
    server.shutdown();
  }
  Map<String,Object> returnVal=new HashMap<String,Object>();
  returnVal.put("String_Node_Str",server);
  returnVal.put("String_Node_Str",dispatcher);
  return returnVal;
}

Fixed version (ground truth):
/** 
 * Do a pull replication TODO - instead calling server.takeRequest, call dispatcher.takeRequest and pass a path regex
 * @param shutdownMockWebserver - should this test shutdown the mockwebserverwhen done?  if another test wants to pick up where this left off, you should pass false.
 * @param serverType - should the mock return the Sync Gateway server type inthe "Server" HTTP Header?  this changes the behavior of the replicator to use bulk_get and POST reqeusts for _changes feeds.
 * @param addAttachments - should the mock sync gateway return docs with attachments?
 * @return a map that contains the mockwebserver (key="server") and the mock dispatcher(key="dispatcher")
 */
public Map<String,Object> mockSinglePull(boolean shutdownMockWebserver,MockDispatcher.ServerType serverType,boolean addAttachments) throws Exception {
  String doc1Id="String_Node_Str";
  String doc1Rev="String_Node_Str";
  String doc2Id="String_Node_Str";
  String doc2Rev="String_Node_Str";
  String doc1AttachName="String_Node_Str";
  String doc2AttachName="String_Node_Str";
  int doc1Seq=1;
  int doc2Seq=2;
  MockWebServer server=MockHelper.getMockWebServer();
  MockDispatcher dispatcher=new MockDispatcher();
  dispatcher.setServerType(serverType);
  server.setDispatcher(dispatcher);
  MockResponse fakeCheckpointResponse=new MockResponse();
  MockHelper.set404NotFoundJson(fakeCheckpointResponse);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,fakeCheckpointResponse);
  MockChangesFeed mockChangesFeed=new MockChangesFeed();
  MockChangedDoc mockChangedDoc1=new MockChangedDoc().setSeq(doc1Seq).setDocId(doc1Id).setChangedRevIds(Arrays.asList(doc1Rev));
  mockChangesFeed.add(mockChangedDoc1);
  MockChangedDoc mockChangedDoc2=new MockChangedDoc().setSeq(doc2Seq).setDocId(doc2Id).setChangedRevIds(Arrays.asList(doc2Rev));
  mockChangesFeed.add(mockChangedDoc2);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHANGES,mockChangesFeed.generateMockResponse());
  Map<String,Object> doc1JsonMap=MockHelper.generateRandomJsonMap();
  MockDocumentGet mockDocumentGet=new MockDocumentGet().setDocId(doc1Id).setRev(doc1Rev).setJsonMap(doc1JsonMap);
  if (addAttachments) {
    mockDocumentGet.addAttachmentFilename(doc1AttachName);
  }
  String doc1PathRegex="String_Node_Str";
  dispatcher.enqueueResponse(doc1PathRegex,mockDocumentGet.generateMockResponse());
  Map<String,Object> doc2JsonMap=MockHelper.generateRandomJsonMap();
  mockDocumentGet=new MockDocumentGet().setDocId(doc2Id).setRev(doc2Rev).setJsonMap(doc2JsonMap);
  if (addAttachments) {
    mockDocumentGet.addAttachmentFilename(doc2AttachName);
  }
  String doc2PathRegex="String_Node_Str";
  dispatcher.enqueueResponse(doc2PathRegex,mockDocumentGet.generateMockResponse());
  MockCheckpointPut mockCheckpointPut=new MockCheckpointPut();
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointPut);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointPut);
  server.play();
  Replication pullReplication=doPullReplication(server.getUrl("String_Node_Str"));
  assertNotNull(database);
  Document doc1=database.getDocument(doc1Id);
  assertNotNull(doc1);
  assertNotNull(doc1.getCurrentRevisionId());
  assertTrue(doc1.getCurrentRevisionId().startsWith("String_Node_Str"));
  assertNotNull(doc1.getProperties());
  assertEquals(doc1JsonMap,doc1.getUserProperties());
  Document doc2=database.getDocument(doc2Id);
  assertNotNull(doc2);
  assertNotNull(doc2.getCurrentRevisionId());
  assertNotNull(doc2.getProperties());
  assertTrue(doc2.getCurrentRevisionId().startsWith("String_Node_Str"));
  assertEquals(doc2JsonMap,doc2.getUserProperties());
  if (addAttachments) {
    attachmentAsserts(doc1AttachName,doc1);
    attachmentAsserts(doc2AttachName,doc2);
  }
  RecordedRequest getCheckpointRequest=dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
  assertTrue(getCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(getCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  RecordedRequest getChangesFeedRequest=dispatcher.takeRequest(MockHelper.PATH_REGEX_CHANGES);
  if (serverType == MockDispatcher.ServerType.SYNC_GW) {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
 else {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
  assertTrue(getChangesFeedRequest.getPath().matches(MockHelper.PATH_REGEX_CHANGES));
  RecordedRequest doc1Request=dispatcher.takeRequest(doc1PathRegex);
  assertTrue(doc1Request.getMethod().equals("String_Node_Str"));
  assertTrue(doc1Request.getPath().matches(doc1PathRegex));
  RecordedRequest doc2Request=dispatcher.takeRequest(doc2PathRegex);
  assertTrue(doc2Request.getMethod().equals("String_Node_Str"));
  assertTrue(doc2Request.getPath().matches(doc2PathRegex));
  RecordedRequest putCheckpointRequest=dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
  assertNotNull(putCheckpointRequest);
  assertTrue(putCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(putCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  if (shutdownMockWebserver) {
    server.shutdown();
  }
  Map<String,Object> returnVal=new HashMap<String,Object>();
  returnVal.put("String_Node_Str",server);
  returnVal.put("String_Node_Str",dispatcher);
  return returnVal;
}