All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.restli.client.testutils.MockBatchEntityResponseFactory Maven / Gradle / Ivy

Go to download

Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.

There is a newer version: 6.0.12
Show newest version
/*
   Copyright (c) 2014 LinkedIn Corp.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.linkedin.restli.client.testutils;


import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.linkedin.data.DataMap;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.restli.client.response.BatchKVResponse;
import com.linkedin.restli.common.BatchResponse;
import com.linkedin.restli.common.ComplexKeySpec;
import com.linkedin.restli.common.ComplexResourceKey;
import com.linkedin.restli.common.CompoundKey;
import com.linkedin.restli.common.EntityResponse;
import com.linkedin.restli.common.ErrorResponse;
import com.linkedin.restli.common.HttpStatus;
import com.linkedin.restli.common.ProtocolVersion;
import com.linkedin.restli.common.TypeSpec;
import com.linkedin.restli.internal.client.response.BatchEntityResponse;
import com.linkedin.restli.internal.common.AllProtocolVersions;
import com.linkedin.restli.internal.common.URIParamUtils;


/**
 * Factory for creating a {@link BatchKVResponse} that can be used for tests.
 *
 * @author xma
 */
public class MockBatchEntityResponseFactory
{
  private MockBatchEntityResponseFactory() { }

  /**
   * Create a {@link BatchKVResponse} where the key is a class that extends {@link CompoundKey}
   *
   * @param keyClass the class of the key
   * @param keyParts the pieces that make up the {@link CompoundKey}
   * @param valueClass the value class
   * @param statuses The HTTP status codes that will be returned as part of {@link EntityResponse}s returned in {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   * @param 
   * @param 
   * @return
   */
  public static  BatchKVResponse> createWithCompoundKey(Class keyClass,
                                                                                                                              Map keyParts,
                                                                                                                              Class valueClass,
                                                                                                                              Map recordTemplates,
                                                                                                                              Map statuses,
                                                                                                                              Map errorResponses)
  {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;

    return create(keyClass, valueClass, keyParts, recordTemplates, statuses, errorResponses, version);
  }

  /**
   * Create a {@link BatchKVResponse} where the key is a {@link ComplexResourceKey}
   *
   * @param valueClass the value class
   * @param keyKeyClass the class of the key part of the {@link ComplexResourceKey}
   * @param keyParamsClass the class of the params part of the {@link ComplexResourceKey}
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   *                        NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
   *                        instance of the params class is created with no data in it.
   * @param statuses The HTTP status codes that will be returned as part of {@link EntityResponse}s
   *                 returned in {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   *                       NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
   *                       instance of the params class is created with no data in it.
   * @param 
   * @return
   */
  @SuppressWarnings("rawtypes")
  public static  BatchKVResponse, EntityResponse> createWithComplexKey(Class valueClass,
                                                                                                                                                                                     Class keyKeyClass,
                                                                                                                                                                                     Class keyParamsClass,
                                                                                                                                                                                     Map, V> recordTemplates,
                                                                                                                                                                                     Map, HttpStatus> statuses,
                                                                                                                                                                                     Map, ErrorResponse> errorResponses)
  {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;

    DataMap batchResponseDataMap = buildDataMap(recordTemplates, statuses, errorResponses, version);

    @SuppressWarnings("unchecked")
    BatchKVResponse, EntityResponse> response =
      (BatchKVResponse, EntityResponse>) (Object) new BatchEntityResponse(batchResponseDataMap,
                                                                                                                               new TypeSpec(ComplexResourceKey.class),
                                                                                                                               TypeSpec.forClassMaybeNull(valueClass),
                                                                                                                               null,
                                                                                                                               ComplexKeySpec.forClassesMaybeNull(keyKeyClass, keyParamsClass),
                                                                                                                               version);
    return response;
  }

  /**
   * Creates a {@link BatchKVResponse} where the key is a primitive, or a typeref to a primitive.
   *
   * @param keyClass class for the key
   * @param valueClass class for the value
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param statuses The HTTP status codes that will be returned as part of {@link EntityResponse}s returned in {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   * @param 
   * @param 
   * @return
   */
  public static  BatchKVResponse> createWithPrimitiveKey(Class keyClass,
                                                                                           Class valueClass,
                                                                                           Map recordTemplates,
                                                                                           Map statuses,
                                                                                           Map errorResponses)
  {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;

    return create(keyClass, valueClass, null, recordTemplates, statuses, errorResponses, version);
  }

  /**
   * Creates a {@link BatchKVResponse} where the key is a typeref to a custom Java class.
   *
   * @param keyClass the custom Java class
   * @param typerefClass the typeref class (the generated class that extends {@link RecordTemplate})
   * @param valueClass class for the value
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param statuses The HTTP status codes that will be returned as part of {@link EntityResponse}s returned in {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   * @param 
   * @param 
   * @param 
   * @return
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static  BatchKVResponse> createWithCustomTyperefKey(Class keyClass,
                                                                                                                   Class typerefClass,
                                                                                                                   Class valueClass,
                                                                                                                   Map recordTemplates,
                                                                                                                   Map statuses,
                                                                                                                   Map errorResponses)
  {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;

    DataMap batchResponseDataMap = buildDataMap(recordTemplates, statuses, errorResponses, version);

    return new BatchEntityResponse(batchResponseDataMap,
                                   TypeSpec.forClassMaybeNull(typerefClass),
                                   TypeSpec.forClassMaybeNull(valueClass),
                                   Collections.emptyMap(),
                                   null,
                                   version);
  }

  private static  DataMap buildDataMap(Map recordTemplates,
                                                                    Map statuses,
                                                                    Map errorResponses,
                                                                    ProtocolVersion version)
  {
    Set mergedKeys = new HashSet();
    mergedKeys.addAll(recordTemplates.keySet());
    mergedKeys.addAll(statuses.keySet());
    mergedKeys.addAll(errorResponses.keySet());

    DataMap batchResponseDataMap = new DataMap();
    DataMap rawBatchData = new DataMap();
    for (K key : mergedKeys)
    {
      DataMap entityResponseData = new DataMap();
      RecordTemplate recordTemplate = recordTemplates.get(key);
      if (recordTemplate != null)
      {
        entityResponseData.put(EntityResponse.ENTITY, recordTemplate.data());
      }
      HttpStatus status = statuses.get(key);
      if (status != null)
      {
        entityResponseData.put(EntityResponse.STATUS, status.getCode());
      }
      ErrorResponse errorResponse = errorResponses.get(key);
      if (errorResponse != null)
      {
        entityResponseData.put(EntityResponse.ERROR, errorResponse.data());
      }

      String stringKey = URIParamUtils.encodeKeyForBody(key, false, version);
      rawBatchData.put(stringKey, entityResponseData);
    }
    batchResponseDataMap.put(BatchResponse.RESULTS, rawBatchData);

    DataMap rawErrorData = new DataMap();
    for (Map.Entry errorResponse : errorResponses.entrySet())
    {
      rawErrorData.put(URIParamUtils.encodeKeyForBody(errorResponse.getKey(), false, version), errorResponse.getValue().data());
    }
    batchResponseDataMap.put(BatchResponse.ERRORS, rawErrorData);
    return batchResponseDataMap;
  }

  private static  BatchKVResponse> create(Class keyClass,
                                                                                            Class valueClass,
                                                                                            Map keyParts,
                                                                                            Map recordTemplates,
                                                                                            Map statuses,
                                                                                            Map errorResponses,
                                                                                            ProtocolVersion version)
  {
    DataMap batchResponseDataMap = buildDataMap(recordTemplates, statuses, errorResponses, version);

    return new BatchEntityResponse(batchResponseDataMap,
                                         TypeSpec.forClassMaybeNull(keyClass),
                                         TypeSpec.forClassMaybeNull(valueClass),
                                         keyParts,
                                         null,
                                         version);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy