PageRenderTime 3746ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/jclouds/jclouds
Java | 133 lines | 97 code | 17 blank | 19 comment | 0 complexity | 57d9ffdd5de8147ec1553ea8b7b57219 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 com.google.common.io.BaseEncoding.base16;
  19. import static org.testng.Assert.assertEquals;
  20. import java.io.IOException;
  21. import java.net.URI;
  22. import org.jclouds.chef.ChefApiMetadata;
  23. import org.jclouds.chef.config.ChefParserModule;
  24. import org.jclouds.chef.domain.CookbookVersion;
  25. import org.jclouds.chef.domain.Grouping;
  26. import org.jclouds.chef.domain.Metadata;
  27. import org.jclouds.chef.domain.Resource;
  28. import org.jclouds.http.HttpResponse;
  29. import org.jclouds.http.functions.ParseJson;
  30. import org.jclouds.json.Json;
  31. import org.jclouds.json.config.GsonModule;
  32. import org.jclouds.rest.annotations.ApiVersion;
  33. import org.testng.annotations.BeforeTest;
  34. import org.testng.annotations.Test;
  35. import com.google.inject.AbstractModule;
  36. import com.google.inject.Guice;
  37. import com.google.inject.Injector;
  38. import com.google.inject.Key;
  39. import com.google.inject.TypeLiteral;
  40. /**
  41. * Tests behavior of {@code ParseCookbookVersionFromJson}
  42. */
  43. @Test(groups = { "unit" }, singleThreaded = true)
  44. public class ParseCookbookVersionFromJsonTest {
  45. private ParseJson<CookbookVersion> handler;
  46. private Injector injector;
  47. private Json json;
  48. @BeforeTest
  49. protected void setUpInjector() throws IOException {
  50. injector = Guice.createInjector(new AbstractModule() {
  51. @Override
  52. protected void configure() {
  53. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
  54. }
  55. }, new ChefParserModule(), new GsonModule());
  56. json = injector.getInstance(Json.class);
  57. handler = injector.getInstance(Key.get(new TypeLiteral<ParseJson<CookbookVersion>>() {
  58. }));
  59. }
  60. public void testBrew() throws IOException {
  61. CookbookVersion cookbook = handler.apply(HttpResponse.builder().statusCode(200).message("ok")
  62. .payload(ParseCookbookVersionFromJsonTest.class.getResourceAsStream("/brew-cookbook.json")).build());
  63. assertEquals(cookbook,
  64. handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json.toJson(cookbook)).build()));
  65. }
  66. public void testTomcat() {
  67. CookbookVersion cookbook = handler.apply(HttpResponse.builder().statusCode(200).message("ok")
  68. .payload(ParseCookbookVersionFromJsonTest.class.getResourceAsStream("/tomcat-cookbook.json")).build());
  69. assertEquals(cookbook,
  70. handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json.toJson(cookbook)).build()));
  71. }
  72. public void testMysql() throws IOException {
  73. CookbookVersion cookbook = handler.apply(HttpResponse.builder().statusCode(200).message("ok")
  74. .payload(ParseCookbookVersionFromJsonTest.class.getResourceAsStream("/mysql-cookbook.json")).build());
  75. assertEquals(cookbook,
  76. handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json.toJson(cookbook)).build()));
  77. }
  78. public void testApache() {
  79. CookbookVersion fromJson = handler.apply(HttpResponse.builder().statusCode(200).message("ok")
  80. .payload(ParseCookbookVersionFromJsonTest.class.getResourceAsStream("/apache-chef-demo-cookbook.json"))
  81. .build());
  82. CookbookVersion expected = CookbookVersion
  83. .builder("apache-chef-demo", "0.0.0")
  84. .metadata(Metadata.builder() //
  85. .license("Apache v2.0") //
  86. .maintainer("Your Name") //
  87. .maintainerEmail("youremail@example.com") //
  88. .description("A fabulous new cookbook") //
  89. .version("0.0.0") //
  90. .name("apache-chef-demo") //
  91. .longDescription("") //
  92. .grouping("one", Grouping.builder().title("One title").description("One description").build()) //
  93. .grouping("two", Grouping.builder().title("Two title").description("Two description").build()) //
  94. .build())
  95. .rootFile(
  96. Resource
  97. .builder()
  98. .name("README")
  99. .path("README")
  100. .checksum(base16().lowerCase().decode("11637f98942eafbf49c71b7f2f048b78"))
  101. .url(URI
  102. .create("https://s3.amazonaws.com/opscode-platform-production-data/organization-486ca3ac66264fea926aa0b4ff74341c/checksum-11637f98942eafbf49c71b7f2f048b78?AWSAccessKeyId=AKIAJOZTD2N26S7W6APA&Expires=1277766181&Signature=zgpNl6wSxjTNovqZu2nJq0JztU8%3D")) //
  103. .build())
  104. .rootFile(
  105. Resource
  106. .builder()
  107. .name("Rakefile")
  108. .path("Rakefile")
  109. .checksum(base16().lowerCase().decode("ebcf925a1651b4e04b9cd8aac2bc54eb"))
  110. .url(URI
  111. .create("https://s3.amazonaws.com/opscode-platform-production-data/organization-486ca3ac66264fea926aa0b4ff74341c/checksum-ebcf925a1651b4e04b9cd8aac2bc54eb?AWSAccessKeyId=AKIAJOZTD2N26S7W6APA&Expires=1277766181&Signature=EFzzDSKKytTl7b%2FxrCeNLh05zj4%3D"))
  112. .build()) //
  113. .build();
  114. assertEquals(fromJson, expected);
  115. }
  116. }