EnabledPatchMapping extends PatchMapping { @Override public PatchOperation convertOperation(Road road, PatchOperation modelOperation) { checkArgument(isAddOrReplace(modelOperation), "You can only change the value of enabled"); checkArgument(modelOperation.getValue() instanceof Boolean, "enabled must be a boolean"); if (isTryingToEnableRoad(modelOperation)) { checkArgument(road.getStatus().isTopicCreated(), "Road is not ready and cannot be enabled"); } return PatchOperation.replace("/enabled", modelOperation.getValue()); }  @Override String getPath(); @Override PatchOperation convertOperation(Road road, PatchOperation modelOperation);  }
@Test(expected = IllegalArgumentException.class) public void enable_road_failes_when_road_is_not_ready() throws Exception { mapping.convertOperation(road, PatchOperation.replace("/enabled", Boolean.TRUE)); }
@Test public void enable_succeds_when_road_is_ready() throws Exception { road.getStatus().setTopicCreated(true); PatchOperation operation = mapping.convertOperation(road, PatchOperation.replace("/enabled", Boolean.TRUE)); assertThat(operation, is(PatchOperation.replace("/enabled", Boolean.TRUE))); }
@Test public void disable_succeeds() throws Exception { PatchOperation operation = mapping.convertOperation(road, PatchOperation.replace("/enabled", Boolean.FALSE)); assertThat(operation, is(PatchOperation.replace("/enabled", Boolean.FALSE))); }
@Test public void add_converts_to_replace() throws Exception { road.getStatus().setTopicCreated(true); PatchOperation operation = mapping.convertOperation(road, PatchOperation.add("/enabled", Boolean.TRUE)); assertThat(operation, is(PatchOperation.replace("/enabled", Boolean.TRUE))); }
@Test(expected = IllegalArgumentException.class) public void remove_patch_fails() throws Exception { mapping.convertOperation(road, PatchOperation.remove("/enabled")); }
@Test(expected = IllegalArgumentException.class) public void setting_to_a_non_boolean_fails() throws Exception { mapping.convertOperation(road, PatchOperation.add("/enabled", "FALSE")); }