/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseCredentialsFromJsonResponseTest.java

https://github.com/regularfry/jclouds · Java · 97 lines · 59 code · 16 blank · 22 comment · 0 complexity · 985077418ea84b974de3968814582555 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.gogrid.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.InputStream;
  22. import java.lang.reflect.Type;
  23. import java.net.UnknownHostException;
  24. import java.util.Map;
  25. import javax.inject.Singleton;
  26. import org.jclouds.domain.Credentials;
  27. import org.jclouds.gogrid.config.DateSecondsAdapter;
  28. import org.jclouds.gogrid.domain.IpState;
  29. import org.jclouds.gogrid.domain.ServerImageState;
  30. import org.jclouds.gogrid.domain.ServerImageType;
  31. import org.jclouds.gogrid.domain.ServerState;
  32. import org.jclouds.gogrid.functions.internal.CustomDeserializers;
  33. import org.jclouds.http.HttpResponse;
  34. import org.jclouds.io.Payloads;
  35. import org.jclouds.json.config.GsonModule;
  36. import org.testng.annotations.Test;
  37. import com.google.common.collect.Maps;
  38. import com.google.inject.Guice;
  39. import com.google.inject.Injector;
  40. import com.google.inject.Provides;
  41. /**
  42. * @author Adrian Cole
  43. */
  44. //NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
  45. @Test(groups = "unit", testName = "ParseCredentialsFromJsonResponseTest")
  46. public class ParseCredentialsFromJsonResponseTest {
  47. @Test(expectedExceptions = IllegalStateException.class)
  48. public void testFailWhenTooManyPasswords() throws UnknownHostException {
  49. InputStream is = getClass().getResourceAsStream("/test_credentials_list.json");
  50. HttpResponse response = new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is));
  51. ParseCredentialsFromJsonResponse parser = i.getInstance(ParseCredentialsFromJsonResponse.class);
  52. parser.apply(response);
  53. }
  54. @Test
  55. public void testValid() throws UnknownHostException {
  56. InputStream is = getClass().getResourceAsStream("/test_credential.json");
  57. HttpResponse response = new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is));
  58. ParseCredentialsFromJsonResponse parser = i.getInstance(ParseCredentialsFromJsonResponse.class);
  59. Credentials creds = parser.apply(response);
  60. assertEquals(creds.identity, "root");
  61. assertEquals(creds.credential, "zot40ced");
  62. }
  63. Injector i = Guice.createInjector(new GsonModule() {
  64. @Override
  65. protected void configure() {
  66. bind(DateAdapter.class).to(DateSecondsAdapter.class);
  67. super.configure();
  68. }
  69. @Provides
  70. @Singleton
  71. @SuppressWarnings("unused")
  72. public Map<Type, Object> provideCustomAdapterBindings() {
  73. Map<Type, Object> bindings = Maps.newHashMap();
  74. bindings.put(IpState.class, new CustomDeserializers.IpStateAdapter());
  75. bindings.put(ServerImageType.class, new CustomDeserializers.ServerImageTypeAdapter());
  76. bindings.put(ServerImageState.class, new CustomDeserializers.ServerImageStateAdapter());
  77. bindings.put(ServerState.class, new CustomDeserializers.ServerStateAdapter());
  78. return bindings;
  79. }
  80. });
  81. }