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

Code before:

public class TestNodePlan {

    @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();
        assertNotNull(NodePlan.parseJson(json));
    }

    @Test
    public void testNodePlanWithDisallowedStep() throws IOException {
        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 = assertThrows(IOException.class, () -> NodePlan.parseJson(json));
        assertEquals("Invalid @class value in NodePlan JSON: sample.SampleStep", ex.getMessage());
    }

    @Test
    public void testNodePlanWithSecondStepDisallowed() 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);
        Step sampleStep = new SampleStep();
        sampleStep.setBandwidth(12345);
        sampleStep.setMaxDiskErrors(4567);
        nodePlan.addStep(sampleStep);
        String json = nodePlan.toJson();
        IOException ex = assertThrows(IOException.class, () -> NodePlan.parseJson(json));
        assertEquals("Invalid @class value in NodePlan JSON: sample.SampleStep", ex.getMessage());
    }

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

Review comment (bug):
I prefer using our intercept, 

```
intercept(IOException.class, "Invalid @class value in NodePlan JSON: sample.SampleStep", () ->  NodePlan.parseJson(json));
```

one key diff is that if the exception isn't raised, the assertion failure includes the string value of the lambda expression output, here what nodeplan is created. Which lines you up for actually debugging failures.


Code after:

public class TestNodePlan {

  @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);