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