PageRenderTime 100ms CodeModel.GetById 29ms RepoModel.GetById 6ms app.codeStats 0ms

/core/src/test/java/org/jclouds/ohai/config/OhaiModuleTest.java

https://github.com/jclouds/legacy-jclouds-chef
Java | 151 lines | 102 code | 26 blank | 23 comment | 0 complexity | a7ac49a01925c42e18d14f741b24714f 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.ohai.config;
  20. import static org.jclouds.chef.util.ChefUtils.ohaiAutomaticAttributeBinder;
  21. import static org.testng.Assert.assertEquals;
  22. import java.net.SocketException;
  23. import java.util.Map;
  24. import java.util.Properties;
  25. import javax.inject.Inject;
  26. import org.jclouds.chef.ChefApi;
  27. import org.jclouds.chef.config.ChefParserModule;
  28. import org.jclouds.domain.JsonBall;
  29. import org.jclouds.json.Json;
  30. import org.jclouds.json.config.GsonModule;
  31. import org.jclouds.ohai.Automatic;
  32. import org.jclouds.ohai.config.multibindings.MapBinder;
  33. import org.jclouds.rest.annotations.ApiVersion;
  34. import org.testng.annotations.Test;
  35. import com.google.common.base.Supplier;
  36. import com.google.common.base.Suppliers;
  37. import com.google.inject.AbstractModule;
  38. import com.google.inject.Guice;
  39. import com.google.inject.Injector;
  40. import com.google.inject.TypeLiteral;
  41. import com.google.inject.util.Providers;
  42. /**
  43. * Tests behavior of {@code OhaiModule}
  44. *
  45. * @author Adrian Cole
  46. */
  47. @Test(groups = { "unit" })
  48. public class OhaiModuleTest {
  49. @Test
  50. public void test() throws SocketException {
  51. final Properties sysProperties = new Properties();
  52. sysProperties.setProperty("os.name", "Mac OS X");
  53. sysProperties.setProperty("os.version", "10.3.0");
  54. sysProperties.setProperty("user.name", "user");
  55. Injector injector = Guice.createInjector(new AbstractModule() {
  56. @Override
  57. protected void configure() {
  58. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApi.VERSION);
  59. }
  60. }, new ChefParserModule(), new GsonModule(), new OhaiModule() {
  61. @Override
  62. protected Long millis() {
  63. return 127999291932529l;
  64. }
  65. @Override
  66. protected Properties systemProperties() {
  67. return sysProperties;
  68. }
  69. });
  70. Ohai ohai = injector.getInstance(Ohai.class);
  71. Json json = injector.getInstance(Json.class);
  72. assertEquals(
  73. json.toJson(ohai.ohai.get(), new TypeLiteral<Map<String, JsonBall>>() {
  74. }.getType()),
  75. "{\"ohai_time\":127999291932529,\"platform\":\"macosx\",\"platform_version\":\"10.3.0\",\"current_user\":\"user\",\"jvm\":{\"system\":{\"user.name\":\"user\",\"os.version\":\"10.3.0\",\"os.name\":\"Mac OS X\"}}}");
  76. }
  77. public void test2modules() throws SocketException {
  78. final Properties sysProperties = new Properties();
  79. sysProperties.setProperty("os.name", "Mac OS X");
  80. sysProperties.setProperty("os.version", "10.3.0");
  81. sysProperties.setProperty("user.name", "user");
  82. Injector injector = Guice.createInjector(new AbstractModule() {
  83. @Override
  84. protected void configure() {
  85. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApi.VERSION);
  86. }
  87. }, new ChefParserModule(), new GsonModule(), new OhaiModule() {
  88. @Override
  89. protected Long millis() {
  90. return 1279992919l;
  91. }
  92. @Override
  93. protected Properties systemProperties() {
  94. return sysProperties;
  95. }
  96. }, new AbstractModule() {
  97. @Override
  98. protected void configure() {
  99. MapBinder<String, Supplier<JsonBall>> mapbinder = ohaiAutomaticAttributeBinder(binder());
  100. mapbinder.addBinding("test").toProvider(
  101. Providers.of(Suppliers.ofInstance(new JsonBall("{\"prop1\":\"test1\"}"))));
  102. }
  103. }, new AbstractModule() {
  104. @Override
  105. protected void configure() {
  106. MapBinder<String, Supplier<JsonBall>> mapbinder = ohaiAutomaticAttributeBinder(binder());
  107. mapbinder.addBinding("test").toProvider(
  108. Providers.of(Suppliers.ofInstance(new JsonBall("{\"prop2\":\"test2\"}"))));
  109. }
  110. });
  111. Ohai ohai = injector.getInstance(Ohai.class);
  112. Json json = injector.getInstance(Json.class);
  113. assertEquals(
  114. json.toJson(ohai.ohai.get(), new TypeLiteral<Map<String, JsonBall>>() {
  115. }.getType()),
  116. "{\"ohai_time\":1279992919,\"platform\":\"macosx\",\"platform_version\":\"10.3.0\",\"current_user\":\"user\",\"test\":{\"prop2\":\"test2\",\"prop1\":\"test1\"},\"jvm\":{\"system\":{\"user.name\":\"user\",\"os.version\":\"10.3.0\",\"os.name\":\"Mac OS X\"}}}");
  117. }
  118. static class Ohai {
  119. private Supplier<Map<String, JsonBall>> ohai;
  120. @Inject
  121. public Ohai(@Automatic Supplier<Map<String, JsonBall>> ohai) {
  122. this.ohai = ohai;
  123. }
  124. }
  125. }