PageRenderTime 30ms CodeModel.GetById 26ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/json/BaseParserTest.java

https://github.com/richardcloudsoft/legacy-jclouds
Java | 102 lines | 64 code | 16 blank | 22 comment | 0 complexity | 1e264a5eff9d9bc58b99668a9973e10f MD5 | raw file
  1/**
  2 * Licensed to jclouds, Inc. (jclouds) under one or more
  3 * contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  jclouds licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *   http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing,
 13 * software distributed under the License is distributed on an
 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 * KIND, either express or implied.  See the License for the
 16 * specific language governing permissions and limitations
 17 * under the License.
 18 */
 19package org.jclouds.json;
 20
 21import static org.jclouds.reflect.Reflection2.method;
 22import static org.testng.Assert.assertEquals;
 23
 24import java.lang.annotation.ElementType;
 25import java.lang.annotation.Retention;
 26import java.lang.annotation.RetentionPolicy;
 27import java.lang.annotation.Target;
 28
 29import javax.inject.Qualifier;
 30
 31import org.jclouds.http.HttpResponse;
 32import org.jclouds.http.functions.config.SaxParserModule;
 33import org.jclouds.io.Payload;
 34import org.jclouds.io.Payloads;
 35import org.jclouds.json.config.GsonModule;
 36import org.jclouds.reflect.Invocation;
 37import org.jclouds.rest.internal.TransformerForRequest;
 38import org.testng.annotations.Test;
 39
 40import com.google.common.base.Function;
 41import com.google.common.base.Throwables;
 42import com.google.common.collect.ImmutableList;
 43import com.google.inject.Guice;
 44import com.google.inject.Injector;
 45
 46/**
 47 * 
 48 * @author Adrian Cole
 49 */
 50public abstract class BaseParserTest<T, G> {
 51
 52   @Retention(value = RetentionPolicy.RUNTIME)
 53   @Target(value = { ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
 54   @Qualifier
 55   public @interface Nested {
 56
 57   }
 58
 59   @SuppressWarnings("unchecked")
 60   protected Function<HttpResponse, T> parser(Injector i) {
 61      try {
 62         return (Function<HttpResponse, T>) i
 63               .createChildInjector(new SaxParserModule())
 64               .getInstance(TransformerForRequest.class)
 65               .getTransformerForMethod(Invocation.create(method(getClass(), "expected"), ImmutableList.of()), i);
 66      } catch (Exception e) {
 67         throw Throwables.propagate(e);
 68      }
 69   }
 70
 71   @Test
 72   public void test() {
 73      T expects = expected();
 74      Function<HttpResponse, T> parser = parser(injector());
 75      T response = parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload(payload()).build());
 76      compare(expects, response);
 77   }
 78
 79   protected Payload payload() {
 80      return Payloads.newInputStreamPayload(getClass().getResourceAsStream(resource()));
 81   }
 82
 83   public void compare(T expects, T response) {
 84      assertEquals(response.toString(), expects.toString());
 85   }
 86
 87   protected Injector injector() {
 88      return Guice.createInjector(new GsonModule() {
 89         protected void configure() {
 90            bind(DateAdapter.class).to(Iso8601DateAdapter.class);
 91            super.configure();
 92         }
 93      });
 94
 95   }
 96
 97   protected String resource() {
 98      throw new IllegalStateException("please define resource such as \"/testaddresses.json\"");
 99   }
100
101   public abstract T expected();
102}