/** * If machine is restarting, then will get WinRM IOExceptions */ protected Throwable findExceptionCausedByWindowsRestart(Exception e) { return Exceptions.getFirstThrowableOfType(e, WebServiceException.class) != null ? Exceptions.getFirstThrowableOfType(e, WebServiceException.class/*Wraps Soap exceptions*/) : Exceptions.getFirstThrowableOfType(e, Fault.class/*Wraps IO exceptions*/); }
@Override public boolean apply(Throwable input) { return Exceptions.getFirstThrowableOfType(input, AllowedException.class) != null; } };
@Test public void testGetFirstThrowableOfType() throws Exception { NullPointerException npe = new NullPointerException("simulated"); IllegalStateException ise = new IllegalStateException("simulated", npe); assertEquals(Exceptions.getFirstThrowableOfType(ise, IllegalStateException.class), ise); assertEquals(Exceptions.getFirstThrowableOfType(ise, NullPointerException.class), npe); assertNull(Exceptions.getFirstThrowableOfType(ise, IndexOutOfBoundsException.class)); }
@Test(groups="Integration") public void testGetInetAddressWithFixedNameButInvalidIpThrowsException() throws Exception { // as with ByonLocationResolverTest.testNiceError // some DNS servers give an IP for this "hostname" // so test is marked as integration now try { Networking.getInetAddressWithFixedName("1.2.3.400"); fail(); } catch (Exception e) { if (Exceptions.getFirstThrowableOfType(e, UnknownHostException.class) == null) throw e; } }
@Test public void testDefaultResizeFailsIfContainerGroupNotResizable() throws Exception { try { pool.resize(1); fail(); } catch (Exception e) { if (Exceptions.getFirstThrowableOfType(e, UnsupportedOperationException.class) == null) throw e; } }
@Test public void testRemoveRegionWithNonEntityId() throws Exception { app.start(ImmutableList.of(loc1)); try { fabric.removeRegion("thisIsNotAnEntityId"); } catch (Exception e) { IllegalArgumentException cause = Exceptions.getFirstThrowableOfType(e, IllegalArgumentException.class); if (cause == null && !e.toString().contains("No entity found")) throw e; } }
protected void assertNoMachinesAvailableForApp(TestApplication app) { try { app.start(ImmutableList.of(pool.getDynamicLocation())); fail("Expected exception when starting app with too many entities for pool"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, NoMachinesAvailableException.class); if (t == null) { throw new RuntimeException(e); } } }
@Test public void testSetParentWhenDiffersFromParentSetInConstructor() { Entity e = new AbstractEntity(app) {}; Entity e2 = new AbstractEntity() {}; try { e.setParent(e2); Asserts.shouldHaveFailedPreviously(); } catch (Exception ex) { Exception uoe = Exceptions.getFirstThrowableOfType(ex, UnsupportedOperationException.class); if (uoe == null || !uoe.toString().contains("Cannot change parent")) { throw ex; } } }
@Test public void testExceptionWhenEntityHasNullConfig() { try { app.createAndManageChild(EntitySpec.create(EntityWithNonNullConstraint.class)); fail("Expected exception when managing entity with missing config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t); } }
@Test public void testRemoveRegionWithNonChild() throws Exception { app.start(ImmutableList.of(loc1)); try { fabric.removeRegion(app.getId()); } catch (Exception e) { IllegalArgumentException cause = Exceptions.getFirstThrowableOfType(e, IllegalArgumentException.class); if (cause == null && !e.toString().contains("Wrong parent")) throw e; } }
@Test public void testConfigKeyStringWontStoreAndRetrieveMaps() throws Exception { Map<String, String> v1 = ImmutableMap.of("a", "1", "b", "bb"); //it only allows strings try { entity.config().set((ConfigKey)TestEntity.CONF_MAP_THING.subKey("akey"), v1); fail(); } catch (Exception e) { ClassCastException cce = Exceptions.getFirstThrowableOfType(e, ClassCastException.class); if (cce == null) throw e; if (!cce.getMessage().contains("Cannot coerce type")) throw e; } }
@Test(dataProvider = "brooklynObjects") public void testCannotUpdateConfigToInvalidValue(BrooklynObject object) { try { object.config().set(EntityRequiringConfigKeyInRange.RANGE, -1); fail("Expected exception when calling config().set with invalid value on " + object); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testExceptionWhenSubclassSetsInvalidDefaultValue() { try { app.createAndManageChild(EntitySpec.create(EntityProvidingDefaultValueForConfigKeyInRange.class)); fail("Expected exception when managing entity setting invalid default value"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testExceptionWhenValueSetByName() { try { app.createAndManageChild(EntitySpec.create(EntityRequiringConfigKeyInRange.class) .configure(ImmutableMap.of("test.conf.range", -1))); fail("Expected exception when managing entity with invalid config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testFailsFastOnInvalidCoercion() { try { newLocation(MutableMap.of("int1", "thisisnotanint")); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { if (Exceptions.getFirstThrowableOfType(e, IllegalArgumentException.class) == null) { throw e; } } }
@Test public void testExceptionIsThrownWhenUserNullsConfigWithNonNullDefault() { try { app.createAndManageChild(EntitySpec.create(EntityWithNonNullConstraintWithNonNullDefault.class) .configure(EntityWithNonNullConstraintWithNonNullDefault.NON_NULL_WITH_DEFAULT, (Object) null)); fail("Expected exception when config key set to null"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testExceptionWhenPolicyHasInvalidConfig() { try { mgmt.getEntityManager().createPolicy(PolicySpec.create(PolicyWithConfigConstraint.class) .configure(PolicyWithConfigConstraint.NON_NULL_CONFIG, (Object) null)); fail("Expected exception when creating policy with missing config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testExceptionWhenEnricherHasInvalidConfig() { try { mgmt.getEntityManager().createEnricher(EnricherSpec.create(EnricherWithConfigConstraint.class) .configure(EnricherWithConfigConstraint.PATTERN, "123.123.256.10")); fail("Expected exception when creating enricher with invalid config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test(enabled = false) public void testExceptionWhenPolicyHasNullForeignConfig() { Policy p = mgmt.getEntityManager().createPolicy(PolicySpec.create(TestPolicy.class) .configure(EntityWithNonNullConstraint.NON_NULL_CONFIG, (Object) null)); try { ConfigConstraints.assertValid(p); fail("Expected exception when validating policy with missing config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testEnricherSpecThrowsOnPropagatesAndPropagatesAllSet() throws Exception { try { app.enrichers().add(EnricherSpec.create(Propagator.class) .configure(MutableMap.builder() .put(Propagator.PRODUCER, entity) .put(Propagator.PROPAGATING, ImmutableList.of(TestEntity.NAME)) .put(Propagator.PROPAGATING_ALL, true) .build())); } catch (Exception e) { IllegalStateException ise = Exceptions.getFirstThrowableOfType(e, IllegalStateException.class); if (ise == null) throw e; } }