PageRenderTime 931ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/labs/joyent-cloudapi/src/test/java/org/jclouds/joyent/cloudapi/v6_5/compute/loaders/CreateUniqueKeyTest.java

http://github.com/jclouds/jclouds
Java | 101 lines | 61 code | 19 blank | 21 comment | 0 complexity | 1f2a28fb70ab403b013d6da4f36b2874 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.joyent.cloudapi.v6_5.compute.loaders;
  20. import static org.easymock.EasyMock.createMock;
  21. import static org.easymock.EasyMock.expect;
  22. import static org.easymock.EasyMock.replay;
  23. import static org.easymock.EasyMock.verify;
  24. import static org.testng.Assert.assertEquals;
  25. import java.io.IOException;
  26. import java.security.NoSuchAlgorithmException;
  27. import java.security.spec.InvalidKeySpecException;
  28. import java.util.Map;
  29. import org.jclouds.compute.functions.GroupNamingConvention;
  30. import org.jclouds.compute.functions.GroupNamingConvention.Factory;
  31. import org.jclouds.crypto.SshKeyPairGenerator;
  32. import org.jclouds.joyent.cloudapi.v6_5.JoyentCloudApi;
  33. import org.jclouds.joyent.cloudapi.v6_5.compute.internal.KeyAndPrivateKey;
  34. import org.jclouds.joyent.cloudapi.v6_5.domain.Key;
  35. import org.jclouds.joyent.cloudapi.v6_5.domain.datacenterscoped.DatacenterAndName;
  36. import org.jclouds.joyent.cloudapi.v6_5.features.KeyApi;
  37. import org.testng.annotations.BeforeClass;
  38. import org.testng.annotations.Test;
  39. import com.google.common.base.Supplier;
  40. import com.google.common.base.Suppliers;
  41. import com.google.common.collect.ImmutableMap;
  42. import com.google.inject.AbstractModule;
  43. import com.google.inject.Guice;
  44. import com.google.inject.TypeLiteral;
  45. /**
  46. * @author Adrian Cole
  47. */
  48. @Test(groups = "unit", testName = "CreateUniqueKeyTest")
  49. public class CreateUniqueKeyTest {
  50. private ImmutableMap<String, String> keyPair = ImmutableMap.of("public", "ssh-rsa AAAAB3NzaC...", "private",
  51. "-----BEGIN RSA PRIVATE KEY-----\n");
  52. private Factory namingConvention;
  53. @BeforeClass
  54. public void setup() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException {
  55. namingConvention = Guice.createInjector(new AbstractModule() {
  56. @Override
  57. protected void configure() {
  58. bind(new TypeLiteral<Supplier<String>>() {
  59. }).toInstance(Suppliers.ofInstance("foo"));
  60. }
  61. }).getInstance(GroupNamingConvention.Factory.class);
  62. }
  63. @Test
  64. public void testApply() {
  65. JoyentCloudApi cloudApiApi = createMock(JoyentCloudApi.class);
  66. SshKeyPairGenerator sshKeyPairGenerator = new SshKeyPairGenerator() {
  67. @Override
  68. public Map<String, String> get() {
  69. return keyPair;
  70. }
  71. };
  72. KeyApi keyApi = createMock(KeyApi.class);
  73. Key key = Key.builder().name("group-foo").key(keyPair.get("public")).build();
  74. expect(cloudApiApi.getKeyApi()).andReturn(keyApi);
  75. expect(keyApi.create(key)).andReturn(key);
  76. replay(cloudApiApi, keyApi);
  77. CreateUniqueKey parser = new CreateUniqueKey(cloudApiApi, namingConvention, sshKeyPairGenerator);
  78. assertEquals(parser.load(DatacenterAndName.fromDatacenterAndName("datacenter", "group")),
  79. KeyAndPrivateKey.fromKeyAndPrivateKey(key, keyPair.get("private")));
  80. verify(cloudApiApi, keyApi);
  81. }
  82. }