PartitionPathPatchMapping extends PatchMapping { @Override public PatchOperation convertOperation(Road road, PatchOperation modelOperation) { checkArgument(isAddOrReplace(modelOperation), "You can only change the value of partitionPath"); checkArgument(modelOperation.getValue() instanceof String, "partitionPath must be a string"); checkArgument(StringUtils.isNotBlank((CharSequence) modelOperation.getValue()) || modelOperation.getValue() == null, "partitionPath cannot be blank, it must be null or have a value"); Path path = KeyPathParser.parse((String) modelOperation.getValue()); Map<Integer, SchemaVersion> schemas = road.getSchemas(); Integer latestSchemaVersion = schemas .values() .stream() .filter(not(SchemaVersion::isDeleted)) .map(SchemaVersion::getVersion) .max(Integer::compareTo) .orElse(null); if (latestSchemaVersion != null) { try { new KeyPathValidator(path, schemas.get(latestSchemaVersion).getSchema()).validate(); } catch (InvalidKeyPathException e) { throw new IllegalArgumentException(String.format( "partitionPath '%s' not compatible with latest schema (version %d).", path, latestSchemaVersion), e); } } return PatchOperation.replace("/partitionPath", modelOperation.getValue()); }  @Override String getPath(); @Override PatchOperation convertOperation(Road road, PatchOperation modelOperation);  }
@Test(expected = IllegalArgumentException.class) public void partitionPath_cannot_be_set_to_null() throws Exception { mapping.convertOperation(road, PatchOperation.replace("/partitionPath", null)); }
@Test public void partitionPath_valid_in_latest_schema() throws Exception { mapping.convertOperation(road, PatchOperation.replace("/partitionPath", "$.bar")); }
@Test(expected = IllegalArgumentException.class) public void partitionPath_not_valid_in_latest_schema() throws Exception { mapping.convertOperation(road, PatchOperation.replace("/partitionPath", "$.baz")); }
@Test public void add_converts_to_replace() throws Exception { PatchOperation operation = mapping.convertOperation(road, PatchOperation.add("/partitionPath", "$.foo")); assertThat(operation, is(PatchOperation.replace("/partitionPath", "$.foo"))); }
@Test(expected = IllegalArgumentException.class) public void remove_patch_fails() throws Exception { mapping.convertOperation(road, PatchOperation.remove("/partitionPath")); }
@Test(expected = IllegalArgumentException.class) public void setting_to_a_non_string_fails() throws Exception { mapping.convertOperation(road, PatchOperation.add("/partitionPath", false)); }