/** * Impose immutability constraints. * * @param <T> representation type * @param strict array of strictly immutable field names * @param incoming incoming representation * @param existing existing representation * @return error Response if appropriate */ public static <T extends BaseResource> Response imposeConstraints(String[] strict, T incoming, T existing) { return imposeConstraints(strict, incoming, existing, BROKEN_CONSTRAINT_REASON, BROKEN_CONSTRAINT_DETAIL); }
/** Validate that @name is the name of an enum constant from the * enum class @clz. * * @param clz the enum class * @param name the enum constant name; must not be null * @return the enum constant * @throws WebApplicationException wrapping an appropriate response * iff the @name is invalid */ public static <E extends Enum<E>> E validateEnum(Class<E> clz, String name) { return validateEnum(clz, name, false); }
private static boolean assertFields(Object model, String fields) { return assertFields(model, null, fields); }
@Test public void testMissingParameter() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters(vm, "status"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Vm", "status"); } }
/** * Validate presence of required parameters. * Note the model type is java.lang.Object as opposed to a generic * <T extends BaseResource> in order to accommodate parameters types * such as Action. * * @param model the incoming representation * @param required the required field names * @throws WebApplicationException wrapping an appropriate response * iff a required parameter is missing */ public static void validateParameters(Object model, String... required) { validateParameters(INCOMPLETE_PARAMS_REASON, INCOMPLETE_PARAMS_DETAIL, model, 2, required); }
@Test public void testInvalid() { try { validateEnum(Thread.State.class, "foobar"); fail("expected WebApplicationException on invalid value"); } catch (WebApplicationException wae) { verifyInvalidValueException(wae, "foobar", "State"); } }
@Test public void testIncludeSingle() { doTestIncludes(";detail=statistics", new String[] {"statistics", "permissions"}, new boolean[] {true, false}); }
@Test public void testMissingParameterSpecificReason() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters("Missing stuff", "{0} {1} required for {2}", vm, "status"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException("Missing stuff", wae, "Vm", "status"); } }
/** * Use this method wherever the list of possible values is extended beyond the enum values.; * This would commonly be seen in situations we where enums are deprecated they're value become system configurable; * instead of hard-coded. e.g {@link org.ovirt.engine.core.common.osinfo.OsRepository} * @param clz the emum class * @param externalValues the extended list of values to validate the input upon * @param name the actual value to be looked up. could be the enum member or part of extended list of values */ public static <E extends Enum<E>> String validateEnum(Class<E> clz, Set<String> externalValues, String name, boolean toUppercase) { return validateEnum(INVALID_ENUM_REASON, INVALID_ENUM_DETAIL, clz, externalValues, name, toUppercase); }
/** * Validate presence of required parameters. * Note the model type is java.lang.Object as opposed to a generic * <T extends BaseResource> in order to accommodate parameters types * such as Action. * * @param reason the fault reason * @param detail the fault detail * @param model the incoming representation * @param required the required field names * @throws WebApplicationException wrapping an appropriate response * iff a required parameter is missing */ public static void validateParameters(String reason, String detail, Object model, String... required) { validateParameters(reason, detail, model, 2, required); }
@Test public void testIncludeSome() { doTestIncludes(";detail=devices ;detail=statistics", new String[] {"statistics", "permissions"}, new boolean[] {true, false}); }
@Test public void testMissingSuperField() { Vm vm = new Vm(); vm.setDescription("incomplete"); try { CompletenessAssertor.validateParameters(vm, "host.id"); fail("expected WebApplicationException on incomplete model"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "Vm", "host.id"); } }
public static <E extends Enum<E>> E validateEnum(Class<E> clz, String name, boolean toUppercase) { return validateEnum(INVALID_ENUM_REASON, INVALID_ENUM_DETAIL, clz, name, toUppercase); }
private void offsetValidateParameters(Vm vm) { CompletenessAssertor.validateParameters(vm, 2, "status"); }
@Test public void testIncludeMoreCollapsed() { doTestIncludes(";detail=devices; detail=statistics+tags+permissions", new String[] {"statistics", "permissions"}, new boolean[] {true, true}); }
/** Validate that @name is the name of an enum constant from the * enum class @clz. * * @param reason the fault reason * @param detail the fault detail * @param clz the enum class * @param name the enum constant name; must not be null * @return the enum constant * @throws WebApplicationException wrapping an appropriate response * iff the @name is invalid */ public static <E extends Enum<E>> E validateEnum(String reason, String detail, Class<E> clz, String name) { return validateEnum(reason, detail, clz, name, false); }
public void validateParameters(Object model, int frameOffset, String... required) { String reason = localize(Messages.INCOMPLETE_PARAMS_REASON); String detail = localize(Messages.INCOMPLETE_PARAMS_DETAIL_TEMPLATE); CompletenessAssertor.validateParameters(reason, detail, model, frameOffset + 1, required); }
@Test public void testIncludeAll() { doTestIncludes(";detail=statistics; detail=permissions", new String[] {"statistics", "permissions"}, new boolean[] {true, true}); }
public <E extends Enum<E>> E validateEnum(Class<E> clz, String name) { String reason = localize(Messages.INVALID_ENUM_REASON); String detail = localize(Messages.INVALID_ENUM_DETAIL); return EnumValidator.validateEnum(reason, detail, clz, name); }
@Test public void testIncludeAllCollapsed() { doTestIncludes(";detail=statistics+permissions", new String[] {"statistics", "permissions"}, new boolean[] {true, true}); }