Package com.linkedin.restli.test

Source Code of com.linkedin.restli.test.TestCharacterEncoding

/*
   Copyright (c) 2012 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.test;


import com.linkedin.restli.client.RestliRequestOptions;
import com.linkedin.restli.client.uribuilders.RestliUriBuilderUtil;
import java.io.IOException;
import java.util.Collections;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.linkedin.common.callback.Callback;
import com.linkedin.data.DataMap;
import com.linkedin.data.codec.JacksonDataCodec;
import com.linkedin.data.template.DynamicRecordMetadata;
import com.linkedin.r2.message.RequestContext;
import com.linkedin.r2.message.rest.RestRequest;
import com.linkedin.r2.message.rest.RestRequestBuilder;
import com.linkedin.r2.message.rest.RestResponse;
import com.linkedin.restli.client.GetRequest;
import com.linkedin.restli.client.GetRequestBuilder;
import com.linkedin.restli.common.CompoundKey;
import com.linkedin.restli.common.ResourceMethod;
import com.linkedin.restli.common.ResourceSpecImpl;
import com.linkedin.restli.server.RestLiConfig;
import com.linkedin.restli.server.RestLiServer;
import com.linkedin.restli.server.resources.PrototypeResourceFactory;


/**
* @author Josh Walker
* @version $Revision: $
*/

public class TestCharacterEncoding
{

  @Test
  public void testQueryParamValueEncoding()
  {
    RestLiConfig config = new RestLiConfig();
    config.setResourcePackageNames(QueryParamMockCollection.class.getPackage().getName());
    RestLiServer server = new RestLiServer(config, new PrototypeResourceFactory(), null);

    for (char c = 0; c < 256; ++c)
    {
      final String testValue = String.valueOf(c);
      GetRequest<QueryParamMockCollection.DummyRecord> req =
              new GetRequestBuilder<String, QueryParamMockCollection.DummyRecord>(
                      QueryParamMockCollection.RESOURCE_NAME,
                      QueryParamMockCollection.DummyRecord.class,
                      new ResourceSpecImpl(Collections.<ResourceMethod> emptySet(),
                                           Collections.<String, DynamicRecordMetadata> emptyMap(),
                                           Collections.<String, DynamicRecordMetadata> emptyMap(),
                                           String.class,
                                           null,
                                           null,
                                           QueryParamMockCollection.DummyRecord.class,
                                           Collections.<String, CompoundKey.TypeInfo> emptyMap()),
                      RestliRequestOptions.DEFAULT_OPTIONS)
                      .id("dummy")
                      .setParam(QueryParamMockCollection.VALUE_KEY, testValue).build();
      RestRequest restRequest = new RestRequestBuilder(RestliUriBuilderUtil.createUriBuilder(req).build())
              .setMethod(req.getMethod().getHttpMethod().toString()).build();

      // N.B. since QueryParamMockCollection is implemented using the synchronous rest.li interface,
      // RestLiServer.handleRequest() will invoke the application resource *and* the callback
      // *synchronously*, ensuring that the all instances of the callback are invoked before the
      // loop terminates.
      server.handleRequest(restRequest, new RequestContext(), new Callback<RestResponse>()
      {
        @Override
        public void onError(Throwable e)
        {
          Assert.fail();
        }

        @Override
        public void onSuccess(RestResponse result)
        {
          try
          {
            DataMap data = new JacksonDataCodec().readMap(result.getEntity().asInputStream());
            Assert.assertEquals(data.get(QueryParamMockCollection.VALUE_KEY), testValue);
            Assert.assertEquals(QueryParamMockCollection._lastQueryParamValue, testValue);
          }
          catch (IOException e)
          {
            Assert.fail();
          }
        }
      });

    }
  }
}
TOP

Related Classes of com.linkedin.restli.test.TestCharacterEncoding

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.