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

https://github.com/andreisavu/jclouds · Java · 99 lines · 59 code · 18 blank · 22 comment · 0 complexity · 1cdd098a24d15b84298415e0f5e78153 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. */
  19. package org.jclouds.json;
  20. import static org.testng.Assert.assertEquals;
  21. import java.lang.annotation.ElementType;
  22. import java.lang.annotation.Retention;
  23. import java.lang.annotation.RetentionPolicy;
  24. import java.lang.annotation.Target;
  25. import javax.inject.Qualifier;
  26. import org.jclouds.http.HttpResponse;
  27. import org.jclouds.io.Payload;
  28. import org.jclouds.io.Payloads;
  29. import org.jclouds.json.config.GsonModule;
  30. import org.jclouds.rest.internal.RestAnnotationProcessor;
  31. import org.testng.annotations.Test;
  32. import com.google.common.base.Function;
  33. import com.google.common.base.Throwables;
  34. import com.google.inject.Guice;
  35. import com.google.inject.Injector;
  36. /**
  37. *
  38. * @author Adrian Cole
  39. */
  40. public abstract class BaseParserTest<T, G> {
  41. @Retention(value = RetentionPolicy.RUNTIME)
  42. @Target(value = { ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
  43. @Qualifier
  44. public @interface Nested {
  45. }
  46. @SuppressWarnings("unchecked")
  47. protected Function<HttpResponse, T> parser(Injector i) {
  48. try {
  49. return (Function<HttpResponse, T>) RestAnnotationProcessor.getTransformerForMethod(getClass()
  50. .getMethod("expected"), i);
  51. } catch (Exception e) {
  52. throw Throwables.propagate(e);
  53. }
  54. }
  55. @Test
  56. public void test() {
  57. T expects = expected();
  58. Function<HttpResponse, T> parser = parser(injector());
  59. T response = parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload(payload()).build());
  60. compare(expects, response);
  61. }
  62. protected Payload payload() {
  63. return Payloads.newInputStreamPayload(getClass().getResourceAsStream(resource()));
  64. }
  65. public void compare(T expects, T response) {
  66. assertEquals(response.toString(), expects.toString());
  67. }
  68. protected Injector injector() {
  69. return Guice.createInjector(new GsonModule() {
  70. @Override
  71. protected void configure() {
  72. bind(DateAdapter.class).to(Iso8601DateAdapter.class);
  73. super.configure();
  74. }
  75. });
  76. }
  77. protected String resource() {
  78. throw new IllegalStateException("please define resource such as \"/testaddresses.json\"");
  79. }
  80. public abstract T expected();
  81. }