@Override
  public RestLiResponseData<BatchCreateResponseEnvelope> buildRestLiResponseData(Request request,
                                                    RoutingResult routingResult,
                                                    Object result,
                                                    Map<String, String> headers,
                                                    List<HttpCookie> cookies)
  {
    Object altKey = null;
    if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM))
    {
      altKey = routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM);
    }
    final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);

    final ResourceContext resourceContext = routingResult.getContext();

    if (result instanceof BatchCreateKVResult && resourceContext.isReturnEntityRequested())
    {
      BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
      if (list.getResults() == null)
      {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                         "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult
                                             .getResourceMethod());
      }
      List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(list.getResults().size());

      TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(),
          FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());

      for (CreateKVResponse<?, ?> createKVResponse : list.getResults())
      {
        if (createKVResponse == null)
        {
          throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                           "Unexpected null encountered. Null element inside of List inside of a BatchCreateKVResult returned by the resource method: "
                                               + routingResult.getResourceMethod());
        }
        else
        {
          Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createKVResponse.getId(), routingResult);
          if (createKVResponse.getError() == null)
          {
            DataMap entityData = createKVResponse.getEntity() != null ? createKVResponse.getEntity().data() : null;

            final DataMap data = RestUtils.projectFields(entityData, resourceContext);

            CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<>(
                    createKVResponse.getStatus().getCode(),
                    id,
                    new AnyRecord(data),
                    getLocationUri(request, id, altKey, protocolVersion), // location uri
                    null,
                    protocolVersion);
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));

          }
          else
          {
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createKVResponse.getError()));
          }
        }
      }

      TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(),
          FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());

      return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, true), headers, cookies);
    }
    else
    {
      List<? extends CreateResponse> createResponses = extractCreateResponseList(result);

      //Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
      if (createResponses == null)
      {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                         "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult
                                             .getResourceMethod());
      }

      List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(createResponses.size());
      for (CreateResponse createResponse : createResponses)
      {
        //Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
        if (createResponse == null)
        {
          throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                           "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: "
                                               + routingResult.getResourceMethod());
        }
        else
        {
          Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
          if (createResponse.getError() == null)
          {
            CreateIdStatus<Object> entry = new CreateIdStatus<>(
                    createResponse.getStatus().getCode(),
                    id,
                    getLocationUri(request, id, altKey, protocolVersion), // location uri
                    null,
                    protocolVersion);
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
          }
          else
          {
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createResponse.getError()));
          }
        }
      }

      return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, false), headers, cookies);
    }
  }
@Test(dataProvider = "returnEntityData")
  @SuppressWarnings({"Duplicates", "unchecked"})
  public void testReturnEntityInBuildRestLiResponseData(Object batchCreateResult, List<CreateResponse> createResponses, boolean isReturnEntityRequested, boolean expectEntityReturned) throws URISyntaxException
  {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false).atLeastOnce();
    EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(isReturnEntityRequested);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(null);
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(mockContext);

    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);

    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request,
        routingResult,
        batchCreateResult,
        Collections.emptyMap(),
        Collections.emptyList());

    BatchCreateResponseEnvelope responseEnvelope = responseData.getResponseEnvelope();

    Assert.assertEquals(responseEnvelope.isGetAfterCreate(), expectEntityReturned);
    Assert.assertEquals(responseEnvelope.getCreateResponses().size(), createResponses.size());

    for (int i = 0; i < createResponses.size(); i++)
    {
      CreateIdStatus<Long> createIdStatus = (CreateIdStatus<Long>) responseEnvelope.getCreateResponses().get(i).getRecord();
      CreateResponse createResponse = createResponses.get(i);

      Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
      Assert.assertEquals(createIdStatus.getLocation(), "/foo/" + createResponse.getId());

      if (expectEntityReturned)
      {
        CreateIdEntityStatus<Long, Foo> createIdEntityStatus = (CreateIdEntityStatus<Long, Foo>) createIdStatus;
        Assert.assertEquals(createIdEntityStatus.getEntity(), ((CreateKVResponse) createResponse).getEntity());
      }
    }
  }