@Override protected void validateStateOnStart(PlacementHostSelectionTaskState state) { if (state.resourceCount < 1) { throw new LocalizableValidationException("'resourceCount' must be greater than 0.", "request.resource-count.zero"); } }
private static String resolveMessage(LocalizableValidationException e, Locale locale) { if (locale == null) { Logger.getAnonymousLogger().fine("Locale not provided for localization, using default locale."); locale = DEFAULT_LOCALE; } ResourceBundle messages = ResourceBundle.getBundle(MESSAGES_BASE_FILENAME, locale); String message; try { message = messages.getString(e.getErrorMessageCode()); } catch (MissingResourceException ex) { message = e.getMessage(); } MessageFormat f = new MessageFormat(message, locale); message = f.format(e.getArguments()); return message; }
@Test public void testParseOfHostDirhWrongInput() { String invalidVolumeName = "host-dir:/container-dir:/some-other-dir"; try { VolumeUtil.parseVolumeHostDirectory(invalidVolumeName); } catch (LocalizableValidationException e) { assertEquals("Invalid volume directory.", e.getMessage()); } }
private void validateHostTypeAndConnection(ContainerHostSpec hostSpec, Operation op) { ContainerHostType hostType; try { hostType = ContainerHostUtil.getDeclaredContainerHostType(hostSpec.hostState); } catch (LocalizableValidationException ex) { logWarning("Error getting host type: %s", ex.getMessage()); op.fail(ex); return; } // Apply the appropriate validation for each host type switch (hostType) { case DOCKER: validateConnection(hostSpec, op); break; case VCH: validateVicHost(hostSpec, op); break; case KUBERNETES: validateConnection(hostSpec, op); break; default: String error = String.format( ContainerHostUtil.CONTAINER_HOST_TYPE_NOT_SUPPORTED_MESSAGE_FORMAT, hostType.toString()); op.fail(new LocalizableValidationException(error, ContainerHostUtil.CONTAINER_HOST_TYPE_NOT_SUPPORTED_MESSAGE_CODE, hostType)); break; } }
@Test public void testPutEmptyHostType() throws Throwable { List<String> tenantLinks = Arrays.asList( FIRST_TENANT_ID); ContainerHostSpec hostSpec = createContainerHostSpec(tenantLinks, SECOND_COMPUTE_DESC_ID); hostSpec.hostState.customProperties.put(ContainerHostService.CONTAINER_HOST_TYPE_PROP_NAME, "invalid"); String errorMessage = String.format( ContainerHostUtil.CONTAINER_HOST_TYPE_NOT_SUPPORTED_MESSAGE_FORMAT, "invalid"); try { createContainerHostSpec(hostSpec); fail("Should've thrown LocalizableValidationException - " + errorMessage); } catch (LocalizableValidationException e) { assertEquals(errorMessage, e.getMessage()); } }
@Override protected void validateStateOnStart(ResourceNamePrefixTaskState state) { if (state.resourceCount < 1) { throw new LocalizableValidationException("'resourceCount' must be greater than 0.", "request.resource-count.zero"); } }
@Test public void testWrongSerializationExceptions() throws IOException { try { serializeCompositeTemplate(null); fail("wrong content!"); } catch (LocalizableValidationException e) { assertTrue(e.getMessage().contains("is required")); } try { serializeDockerCompose(null); fail("wrong content!"); } catch (LocalizableValidationException e) { assertTrue(e.getMessage().contains("is required")); } }
@Override protected void validateStateOnStart(ReservationRemovalTaskState state) { if (state.resourceCount < 1) { throw new LocalizableValidationException("'resourceCount' must be greater than 0.", "request.resource-count.zero"); } }
@Test public void testValidateIpAddress() { for (TestEntry entry : testIpAddressesData) { try { NetworkUtils.validateIpAddress(entry.testValue); if (entry.shouldFail) { String message = String.format( "Test should have failed, '%s' is not a valid IP address", entry.testValue); Assert.fail(message); } } catch (LocalizableValidationException e) { String errorMessage = e.getMessage(); String expectedError = String.format(NetworkUtils.FORMAT_IP_VALIDATION_ERROR, entry.testValue); if (!errorMessage.equals(expectedError)) { throw e; } else { if (!entry.shouldFail) { String message = String.format( "Test should have passed, '%s' is a valid IP address", entry.testValue); Assert.fail(message); } } } } }
@Override protected void validateStateOnStart(ClusteringTaskState state) throws IllegalArgumentException { if (state.resourceCount < 1) { throw new LocalizableValidationException("'resourceCount' must be greater than 0.", "request.resource-count.zero"); } }
@Test public void testValidateIpCidrNotation() { for (TestEntry entry : testIpCidrNotationData) { try { NetworkUtils.validateIpCidrNotation(entry.testValue); if (entry.shouldFail) { String message = String.format( "Test should have failed, '%s' is not a valid CIDR notation", entry.testValue); Assert.fail(message); } } catch (LocalizableValidationException e) { String errorMessage = e.getMessage(); String expectedError = String.format( NetworkUtils.FORMAT_CIDR_NOTATION_VALIDATION_ERROR, entry.testValue); if (!errorMessage.equals(expectedError)) { throw e; } else { if (!entry.shouldFail) { String message = String.format( "Test should have passed, '%s' is a valid CIDR notation", entry.testValue); Assert.fail(message); } } } } }
@Override protected void validateStateOnStart(ContainerNetworkProvisionTaskState state) { state.resourceCount = (long) state.resourceLinks.size(); if (state.resourceCount < 1) { throw new LocalizableValidationException("'resourceCount' must be greater than 0.", "request.resource-count.zero"); } }
@Test public void testValidateNetworkName() { for (TestEntry entry : testNetworkNames) { try { NetworkUtils.validateNetworkName(entry.testValue); if (entry.shouldFail) { String message = String.format( "Test should have failed, '%s' is not a valid network name", entry.testValue); Assert.fail(message); } } catch (LocalizableValidationException e) { String errorMessage = e.getMessage(); String expectedError = NetworkUtils.ERROR_NETWORK_NAME_IS_REQUIRED; String expectedErrorBadNetworkName = NetworkUtils.BAD_NETWORK_NAME; if (!errorMessage.equals(expectedError) && !errorMessage .equals(expectedErrorBadNetworkName)) { throw e; } else { if (!entry.shouldFail) { String message = String.format( "Test should have passed, '%s' is a valid network name", entry.testValue); Assert.fail(message); } } } } }
protected Throwable checkAuthCredentialsSupportedType(AuthCredentialsServiceState c, boolean throwError) { RuntimeException e = null; if (UNSUPPORTED_CREDENTIALS_TYPES.contains(c.type)) { e = new LocalizableValidationException("Unsupported credentials type", "adapter.unsuported.auth.credentials.type"); if (throwError) { throw e; } } return e; }
@Test public void testCreateDCShouldFailIfAlreadyExists() throws Throwable { HostNetworkListDataCollectionState dc = new HostNetworkListDataCollectionState(); dc.containerHostLinks = new HashMap<>(); try { doPost(dc, HostNetworkListDataCollection.DEFAULT_HOST_NETWORK_LIST_DATA_COLLECTION_LINK); fail("Should have failed because only single DC can exists."); } catch (LocalizableValidationException e) { assertEquals("Only one instance of networks data collection can be started", e.getMessage()); } }
@Override protected void validateStateOnStart(ContainerVolumeProvisionTaskState state) throws IllegalArgumentException { state.resourceCount = (long) state.resourceLinks.size(); if (state.resourceCount < 1) { throw new LocalizableValidationException("'resourceCount' must be greater than 0.", "request.resource-count.zero"); } }
@Test public void testPropertiesValidationOnCreate() throws Throwable { try { createProject(null); fail("Creation of project should have failed due to invalid name"); } catch (AssertionError e) { e.printStackTrace(); throw e; } catch (LocalizableValidationException e) { verifyExceptionMessage(String.format(AssertUtil.PROPERTY_CANNOT_BE_NULL_MESSAGE_FORMAT, ProjectState.FIELD_NAME_NAME), e.getMessage()); } }
public static void assertNotNull(Object value, String propertyName) { if (value == null) { throw new LocalizableValidationException(String.format(PROPERTY_CANNOT_BE_NULL_MESSAGE_FORMAT, propertyName), "common.assertion.property.required", propertyName); } }
@Test(expected = LocalizableValidationException.class) public void testPostStateNotDefaultSelfLink() throws Throwable { HostVolumeListDataCollectionState hostVolumeListDataCollection = getDocument(HostVolumeListDataCollectionState.class, HostVolumeListDataCollection .DEFAULT_HOST_VOLUME_LIST_DATA_COLLECTION_LINK); assertEquals(0, hostVolumeListDataCollection.containerHostLinks.size()); HostVolumeListDataCollectionState hostVolumeListDataCollectionNew = new HostVolumeListDataCollectionState(); hostVolumeListDataCollectionNew.documentSelfLink = "test"; hostVolumeListDataCollectionNew.containerHostLinks = new HashMap<>(); hostVolumeListDataCollectionNew.containerHostLinks.put(COMPUTE_HOST_LINK, 1L); //converted to put which should be ignored try { doPost(hostVolumeListDataCollectionNew, HostVolumeListDataCollection.DEFAULT_HOST_VOLUME_LIST_DATA_COLLECTION_LINK); } catch (LocalizableValidationException e) { if (e.getMessage().contains("Only one instance of list containers data collection can" + " be started")) { throw e; } } fail("Should fail with: Only one instance of list containers data collection can be started"); }
public static void assertNotEmpty(Object[] value, String propertyName) { if (value == null || value.length == 0) { throw new LocalizableValidationException(String.format(PROPERTY_CANNOT_BE_EMPTY_MESSAGE_FORMAT, propertyName), "common.assertion.property.not.empty", propertyName); } }