@SuppressWarnings( { "unchecked" }) @Test public void testApplyWithIllegalStateException() throws UnknownHostException { final EC2Api client = createMock(EC2Api.class); KeyPairApi keyClient = createMock(KeyPairApi.class); final Supplier<String> uniqueIdSupplier = createMock(Supplier.class); KeyPair pair = createMock(KeyPair.class); expect(client.getKeyPairApi()).andReturn((Optional) Optional.of(keyClient)).atLeastOnce(); expect(uniqueIdSupplier.get()).andReturn("1"); expect(keyClient.createKeyPairInRegion("region", "jclouds#group#1")).andThrow(new IllegalStateException()); expect(uniqueIdSupplier.get()).andReturn("2"); expect(keyClient.createKeyPairInRegion("region", "jclouds#group#2")).andReturn(pair); replay(client); replay(keyClient); replay(uniqueIdSupplier); CreateUniqueKeyPair parser = Guice.createInjector(new AbstractModule() { @Override protected void configure() { Names.bindProperties(binder(), new EC2ApiMetadata().getDefaultProperties()); bind(new TypeLiteral<Supplier<String>>() { }).toInstance(uniqueIdSupplier); bind(EC2Api.class).toInstance(client); } }).getInstance(CreateUniqueKeyPair.class); assertEquals(parser.createNewKeyPairInRegion("region", "group"), pair); verify(client); verify(keyClient); verify(uniqueIdSupplier); }
@VisibleForTesting KeyPair createNewKeyPairInRegion(String region, String group) { checkNotNull(region, "region"); checkNotNull(group, "group"); logger.debug(">> creating keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; String prefix = group; while (keyPair == null) { String keyName = namingConvention.create().uniqueNameForGroup(prefix); try { keyPair = ec2Api.getKeyPairApi().get().createKeyPairInRegion(region, keyName); } catch (IllegalStateException e) { logger.trace(" invalid keyname (%s in %s); retrying", keyName, region); } } logger.debug("<< created keyPair(%s)", keyPair); return keyPair; }
CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> { @VisibleForTesting KeyPair createNewKeyPairInRegion(String region, String group) { checkNotNull(region, "region"); checkNotNull(group, "group"); logger.debug(">> creating keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; String prefix = group; while (keyPair == null) { String keyName = namingConvention.create().uniqueNameForGroup(prefix); try { keyPair = ec2Api.getKeyPairApi().get().createKeyPairInRegion(region, keyName); } catch (IllegalStateException e) { logger.trace(" invalid keyname (%s in %s); retrying", keyName, region); } } logger.debug("<< created keyPair(%s)", keyPair); return keyPair; } }
CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> { @VisibleForTesting KeyPair createNewKeyPairInRegion(String region, String group) { checkNotNull(region, "region"); checkNotNull(group, "group"); logger.debug(">> creating keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; String prefix = group; while (keyPair == null) { String keyName = namingConvention.create().uniqueNameForGroup(prefix); try { keyPair = ec2Api.getKeyPairApi().get().createKeyPairInRegion(region, keyName); } catch (IllegalStateException e) { logger.trace(" invalid keyname (%s in %s); retrying", keyName, region); } } logger.debug("<< created keyPair(%s)", keyPair); return keyPair; } @Inject  CreateUniqueKeyPair(EC2Api ec2Api, GroupNamingConvention.Factory namingConvention); }
CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> { @VisibleForTesting KeyPair createNewKeyPairInRegion(String region, String group) { checkNotNull(region, "region"); checkNotNull(group, "group"); logger.debug(">> creating keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; String prefix = group; while (keyPair == null) { String keyName = namingConvention.create().uniqueNameForGroup(prefix); try { keyPair = ec2Api.getKeyPairApi().get().createKeyPairInRegion(region, keyName); } catch (IllegalStateException e) { logger.trace(" invalid keyname (%s in %s); retrying", keyName, region); } } logger.debug("<< created keyPair(%s)", keyPair); return keyPair; } @Inject  CreateUniqueKeyPair(EC2Api ec2Api, GroupNamingConvention.Factory namingConvention); @Override KeyPair apply(RegionAndName from); }
CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> { @VisibleForTesting KeyPair createNewKeyPairInRegion(String region, String group) { checkNotNull(region, "region"); checkNotNull(group, "group"); logger.debug(">> creating keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; String prefix = group; while (keyPair == null) { String keyName = namingConvention.create().uniqueNameForGroup(prefix); try { keyPair = ec2Api.getKeyPairApi().get().createKeyPairInRegion(region, keyName); } catch (IllegalStateException e) { logger.trace(" invalid keyname (%s in %s); retrying", keyName, region); } } logger.debug("<< created keyPair(%s)", keyPair); return keyPair; } @Inject  CreateUniqueKeyPair(EC2Api ec2Api, GroupNamingConvention.Factory namingConvention); @Override KeyPair apply(RegionAndName from);  }