Repository: apache/hadoop
File: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/diskbalancer/planner/TestNodePlan.java (Java)

Code before:

  @Test
  public void testNodePlan() throws IOException {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    MoveStep moveStep = new MoveStep();
    moveStep.setBandwidth(12345);
    moveStep.setBytesToMove(98765);
    moveStep.setIdealStorage(1.234);
    moveStep.setMaxDiskErrors(4567);
    moveStep.setVolumeSetID("id1234");
    nodePlan.addStep(moveStep);
    String json = nodePlan.toJson();
    assertThat(NodePlan.parseJson(json)).isNotNull();
  }

  @Test
  public void testNodePlanWithDisallowedStep() throws Exception {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    Step sampleStep = new SampleStep();
    sampleStep.setBandwidth(12345);
    sampleStep.setMaxDiskErrors(4567);
    nodePlan.addStep(sampleStep);
    String json = nodePlan.toJson();
    IOException ex = LambdaTestUtils.intercept(IOException.class, () -> NodePlan.parseJson(json));
    assertThat(ex.getMessage())
        .isEqualTo("Invalid @class value in NodePlan JSON: sample.SampleStep");
  }

  @Test
  public void testNodePlanWithSecondStepDisallowed() throws Exception {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    MoveStep moveStep = new MoveStep();
    moveStep.setBandwidth(12345);
    moveStep.setBytesToMove(98765);
    moveStep.setIdealStorage(1.234);
    moveStep.setMaxDiskErrors(4567);
    moveStep.setVolumeSetID("id1234");
    nodePlan.addStep(moveStep);
    Step sampleStep = new SampleStep();
    sampleStep.setBandwidth(12345);
    sampleStep.setMaxDiskErrors(4567);
    nodePlan.addStep(sampleStep);
    String json = nodePlan.toJson();
    IOException ex = LambdaTestUtils.intercept(IOException.class, () -> NodePlan.parseJson(json));
    assertThat(ex.getMessage())
        .isEqualTo("Invalid @class value in NodePlan JSON: sample.SampleStep");
  }

  @Test
  public void testNodePlanWithNestedDisallowedStep() throws Exception {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);

Review comment (bug):
Factor the toJson and intercept to a new assert, use everywhere, moving the string to check into intercept(). intercept() knows to include the rejected exception if there's no match, which is invaluable debugging things. (oh, it's a toString().contains() check, FWIW)

```
assertNodePlanInvalid(nodePlan) {
  LambdaTestUtils.intercept(IOException.class,
  "Invalid @class value in NodePlan JSON: sample.SampleStep",
  () -> NodePlan.parseJson(nodePlan.toJson()));
}
```

Apply here and in the steps below, just to reduce duplication in test code.



Code after:

  @Test
  public void testNodePlan() throws IOException {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    MoveStep moveStep = new MoveStep();
    moveStep.setBandwidth(12345);
    moveStep.setBytesToMove(98765);
    moveStep.setIdealStorage(1.234);
    moveStep.setMaxDiskErrors(4567);
    moveStep.setVolumeSetID("id1234");
    nodePlan.addStep(moveStep);
    String json = nodePlan.toJson();
    assertThat(NodePlan.parseJson(json)).isNotNull();
  }

  @Test
  public void testNodePlanWithDisallowedStep() throws Exception {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    Step sampleStep = new SampleStep();
    sampleStep.setBandwidth(12345);
    sampleStep.setMaxDiskErrors(4567);
    nodePlan.addStep(sampleStep);
    assertNodePlanInvalid(nodePlan);
  }

  @Test
  public void testNodePlanWithSecondStepDisallowed() throws Exception {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    MoveStep moveStep = new MoveStep();
    moveStep.setBandwidth(12345);
    moveStep.setBytesToMove(98765);
    moveStep.setIdealStorage(1.234);
    moveStep.setMaxDiskErrors(4567);
    moveStep.setVolumeSetID("id1234");
    nodePlan.addStep(moveStep);
    Step sampleStep = new SampleStep();
    sampleStep.setBandwidth(12345);
    sampleStep.setMaxDiskErrors(4567);
    nodePlan.addStep(sampleStep);
    assertNodePlanInvalid(nodePlan);
  }

  @Test
  public void testNodePlanWithNestedDisallowedStep() throws Exception {
    NodePlan nodePlan = new NodePlan("datanode1234", 1234);
    NodePlan nodePlan2 = new NodePlan("datanode9876", 9876);
    SampleStep sampleStep = new SampleStep();
    sampleStep.setBandwidth(12345);
    sampleStep.setMaxDiskErrors(4567);
    nodePlan2.addStep(sampleStep);
    NestedStep nestedStep = new NestedStep(nodePlan2);