@Test
  public void testMergeSchemaTreeWithTemplate() throws Exception {
    Template template1 =
        new Template(
            "t1",
            Arrays.asList("s1", "s2", "s3"),
            Arrays.asList(TSDataType.DOUBLE, TSDataType.INT32, TSDataType.BOOLEAN),
            Arrays.asList(TSEncoding.RLE, TSEncoding.RLE, TSEncoding.RLE),
            Arrays.asList(CompressionType.SNAPPY, CompressionType.SNAPPY, CompressionType.SNAPPY));
    template1.setId(1);
    ClusterSchemaTree schemaTree1 = new ClusterSchemaTree();
    ClusterSchemaTree schemaTree2 = new ClusterSchemaTree();
    schemaTree1.appendTemplateDevice(new PartialPath("root.sg1.v1.d1"), false, 1, template1);
    schemaTree1.appendTemplateDevice(new PartialPath("root.sg1.v1.d2"), false, 1, template1);
    schemaTree2.appendTemplateDevice(new PartialPath("root.sg1.v1"), false, 1, template1);
    schemaTree1.mergeSchemaTree(schemaTree2);
    List<DeviceSchemaInfo> deviceSchemaInfoList = schemaTree1.getAllDevices();
    Assert.assertEquals(3, deviceSchemaInfoList.size());
    for (DeviceSchemaInfo deviceSchemaInfo : deviceSchemaInfoList) {
      Assert.assertEquals(1, deviceSchemaInfo.getTemplateId());
      int measurementIndex = 1;
      for (MeasurementPath measurementPath : deviceSchemaInfo.getMeasurementSchemaPathList()) {
        Assert.assertEquals("s" + measurementIndex++, measurementPath.getMeasurement());
      }
    }
    Assert.assertFalse(schemaTree1.hasNormalTimeSeries());
    Assert.assertEquals(1, schemaTree1.getUsingTemplates().size());
    checkDeviceUsingTemplate(
        schemaTree1, 1, Sets.newSet("root.sg1.v1.d1", "root.sg1.v1.d2", "root.sg1.v1"));
    Template template2 =
        new Template(
            "t2",
            Arrays.asList("s11", "s22", "s33"),
            Arrays.asList(TSDataType.DOUBLE, TSDataType.INT32, TSDataType.BOOLEAN),
            Arrays.asList(TSEncoding.RLE, TSEncoding.RLE, TSEncoding.RLE),
            Arrays.asList(CompressionType.SNAPPY, CompressionType.SNAPPY, CompressionType.SNAPPY));
    template2.setId(2);
    ClusterSchemaTree schemaTree3 = new ClusterSchemaTree();
    schemaTree3.appendTemplateDevice(new PartialPath("root.sg2.d1"), false, 2, template2);
    schemaTree1.mergeSchemaTree(schemaTree3);
    Assert.assertFalse(schemaTree1.hasNormalTimeSeries());
    Assert.assertEquals(2, schemaTree1.getUsingTemplates().size());

    checkDeviceUsingTemplate(
        schemaTree1, 1, Sets.newSet("root.sg1.v1.d1", "root.sg1.v1.d2", "root.sg1.v1"));
    checkDeviceUsingTemplate(schemaTree1, 2, Sets.newSet("root.sg2.d1"));
    for (DeviceSchemaInfo deviceSchemaInfo : deviceSchemaInfoList) {
      if (deviceSchemaInfo.getDevicePath().startsWith("root.sg1")) {
        Assert.assertEquals(1, deviceSchemaInfo.getTemplateId());
        int measurementIndex = 1;
        for (MeasurementPath measurementPath : deviceSchemaInfo.getMeasurementSchemaPathList()) {
          Assert.assertEquals("s" + measurementIndex, measurementPath.getMeasurement());
        }
      } else {
        Assert.assertEquals(2, deviceSchemaInfo.getTemplateId());
        int measurementIndex = 1;
        for (MeasurementPath measurementPath : deviceSchemaInfo.getMeasurementSchemaPathList()) {
          Assert.assertEquals(
              String.format("s%d%d", measurementIndex, measurementIndex),
              measurementPath.getMeasurement());
          measurementIndex++;
        }
      }
    }
    ClusterSchemaTree schemaTree4 = new ClusterSchemaTree();
    schemaTree4.appendSingleMeasurementPath(
        new MeasurementPath("root.sg3.d1.s1", TSDataType.INT32));
    schemaTree1.mergeSchemaTree(schemaTree4);
    Assert.assertTrue(schemaTree1.hasNormalTimeSeries());
    Assert.assertEquals(2, schemaTree1.getUsingTemplates().size());
    for (DeviceSchemaInfo deviceSchemaInfo : deviceSchemaInfoList) {
      if (deviceSchemaInfo.getDevicePath().startsWith("root.sg1")) {
        Assert.assertEquals(1, deviceSchemaInfo.getTemplateId());
        int measurementIndex = 1;
        for (MeasurementPath measurementPath : deviceSchemaInfo.getMeasurementSchemaPathList()) {
          Assert.assertEquals("s" + measurementIndex, measurementPath.getMeasurement());
        }
      } else if (deviceSchemaInfo.getDevicePath().startsWith("root.sg2")) {
        Assert.assertEquals(2, deviceSchemaInfo.getTemplateId());
        int measurementIndex = 1;
        for (MeasurementPath measurementPath : deviceSchemaInfo.getMeasurementSchemaPathList()) {
          Assert.assertEquals(
              String.format("s%d%d", measurementIndex, measurementIndex),
              measurementPath.getMeasurement());
          measurementIndex++;
        }
      } else {
        Assert.assertEquals(NON_TEMPLATE, deviceSchemaInfo.getTemplateId());
        Assert.assertEquals(1, deviceSchemaInfo.getMeasurementSchemaPathList().size());
        Assert.assertEquals(
            "s1", deviceSchemaInfo.getMeasurementSchemaPathList().get(0).getMeasurement());
      }
    }
  }
Generate the following tree: root.sg.d1.s1, root.sg.d1.s2(status) root.sg.d2.s1,
root.sg.d2.s2(status) root.sg.d2.a.s1, root.sg.d2.a.s2(status)
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/common/schematree/ClusterSchemaTreeTest.java