PageRenderTime 3766ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 9ms

/apis/chef/src/test/java/org/jclouds/chef/functions/ParseDataBagItemFromJsonTest.java

http://github.com/jclouds/jclouds
Java | 65 lines | 41 code | 8 blank | 16 comment | 0 complexity | 31b283b554d6dada0b52f1ec5b889969 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.chef.functions;
  18. import static org.testng.Assert.assertEquals;
  19. import java.io.IOException;
  20. import org.jclouds.chef.ChefApiMetadata;
  21. import org.jclouds.chef.config.ChefParserModule;
  22. import org.jclouds.chef.domain.DatabagItem;
  23. import org.jclouds.http.HttpResponse;
  24. import org.jclouds.http.functions.ParseJson;
  25. import org.jclouds.json.Json;
  26. import org.jclouds.json.config.GsonModule;
  27. import org.jclouds.rest.annotations.ApiVersion;
  28. import org.testng.annotations.BeforeTest;
  29. import org.testng.annotations.Test;
  30. import com.google.inject.AbstractModule;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. import com.google.inject.Key;
  34. import com.google.inject.TypeLiteral;
  35. @Test(groups = { "unit" })
  36. public class ParseDataBagItemFromJsonTest {
  37. private ParseJson<DatabagItem> handler;
  38. private Json mapper;
  39. @BeforeTest
  40. protected void setUpInjector() throws IOException {
  41. Injector injector = Guice.createInjector(new AbstractModule() {
  42. @Override
  43. protected void configure() {
  44. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
  45. }
  46. }, new ChefParserModule(), new GsonModule());
  47. handler = injector.getInstance(Key.get(new TypeLiteral<ParseJson<DatabagItem>>() {
  48. }));
  49. mapper = injector.getInstance(Json.class);
  50. }
  51. public void test1() {
  52. String json = "{\"my_key\":\"my_data\",\"id\":\"item1\"}";
  53. DatabagItem item = new DatabagItem("item1", json);
  54. assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()), item);
  55. assertEquals(mapper.toJson(item), json);
  56. }
  57. }