@Test
  public void testMondrianUpdateName() throws Exception {
    File mondrianSchemaXmlFile = new File(MONDRIAN_TEST_FILE_PATH);

    Document mondrianSchemaXmlDoc =
        DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mondrianSchemaXmlFile);

    // Renaming the measure
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_MONDRIAN_FORMULA);
    updateMeasure.setName(NEW_BUYPRICE_NAME);
    boolean isApplied = updateMeasure.apply(mondrianSchemaXmlDoc);
    assertTrue(isApplied);

    assertTrue(mondrianSchemaXmlDoc != null);
    assertTrue(
        mondrianSchemaXmlDoc.getElementsByTagName(AnnotationUtil.MEASURE_ELEMENT_NAME).getLength()
            > 0);
    assertTrue(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            NEW_BUYPRICE_NAME,
            AnnotationUtil.NAME_ATTRIB,
            NEW_BUYPRICE_NAME));
  }
  @Test
  public void testMetaDataUpdateName() throws Exception {
    ModelerWorkspace model = new ModelerWorkspace(new ModelerWorkspaceHelper(""));
    model.setDomain(new XmiParser().parseXmi(new FileInputStream(PRODUCT_XMI_FILE)));
    model.getWorkspaceHelper().populateDomain(model);

    // Initial measure exists
    LogicalModel logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    OlapCube cube =
        ((List<OlapCube>) logicalModel.getProperty(LogicalModel.PROPERTY_OLAP_CUBES)).get(0);
    List<OlapMeasure> olapMeasures = cube.getOlapMeasures();
    assertNotNull(AnnotationUtil.getOlapMeasure(INIT_BUYPRICE_NAME, olapMeasures));

    // Renaming the measure
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_FORMULA);
    updateMeasure.setName(NEW_BUYPRICE_NAME);
    boolean isApplied = updateMeasure.apply(model, metaStore);
    assertTrue(isApplied);

    // Make sure the initial measure name cannot be found and the new measure name exists
    logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    cube = ((List<OlapCube>) logicalModel.getProperty(LogicalModel.PROPERTY_OLAP_CUBES)).get(0);
    olapMeasures = cube.getOlapMeasures();
    assertNull(AnnotationUtil.getOlapMeasure(INIT_BUYPRICE_NAME, olapMeasures));
    assertNotNull(AnnotationUtil.getOlapMeasure(NEW_BUYPRICE_NAME, olapMeasures));
  }
  @Test
  public void testMetaDataUpdateFormat() throws Exception {
    ModelerWorkspace model = new ModelerWorkspace(new ModelerWorkspaceHelper(""));
    model.setDomain(new XmiParser().parseXmi(new FileInputStream(PRODUCT_XMI_FILE)));
    model.getWorkspaceHelper().populateDomain(model);

    // Initial measure exists
    LogicalModel logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    OlapCube cube =
        ((List<OlapCube>) logicalModel.getProperty(LogicalModel.PROPERTY_OLAP_CUBES)).get(0);
    List<OlapMeasure> olapMeasures = cube.getOlapMeasures();
    OlapMeasure measure = AnnotationUtil.getOlapMeasure(INIT_BUYPRICE_NAME, olapMeasures);
    assertNotNull(measure);
    assertNull(measure.getLogicalColumn().getProperty("mask"));

    // Changing the aggregation type
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_FORMULA);
    updateMeasure.setName(INIT_BUYPRICE_NAME);
    updateMeasure.setFormat(NEW_FORMAT);
    boolean isApplied = updateMeasure.apply(model, metaStore);
    assertTrue(isApplied);

    // Ensure the aggregation type got set
    logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    cube = ((List<OlapCube>) logicalModel.getProperty(LogicalModel.PROPERTY_OLAP_CUBES)).get(0);
    olapMeasures = cube.getOlapMeasures();
    measure = AnnotationUtil.getOlapMeasure(INIT_BUYPRICE_NAME, olapMeasures);
    assertNotNull(measure);
    assertEquals(NEW_FORMAT, measure.getLogicalColumn().getProperty("mask"));
  }
  @Test
  public void testMetaDataNoDuplicateNames() throws Exception {
    ModelerWorkspace model = new ModelerWorkspace(new ModelerWorkspaceHelper(""));
    model.setDomain(new XmiParser().parseXmi(new FileInputStream(PRODUCT_XMI_FILE)));
    model.getWorkspaceHelper().populateDomain(model);

    // Initial measure exists
    LogicalModel logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    OlapCube cube =
        ((List<OlapCube>) logicalModel.getProperty(LogicalModel.PROPERTY_OLAP_CUBES)).get(0);
    List<OlapMeasure> olapMeasures = cube.getOlapMeasures();
    assertNotNull(AnnotationUtil.getOlapMeasure(INIT_BUYPRICE_NAME, olapMeasures));

    // Renaming the measure
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_FORMULA);
    updateMeasure.setName(EXISTING_NAME);
    boolean isApplied = updateMeasure.apply(model, metaStore);
    assertFalse(isApplied);

    // Make sure nothing has changed
    logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    cube = ((List<OlapCube>) logicalModel.getProperty(LogicalModel.PROPERTY_OLAP_CUBES)).get(0);
    olapMeasures = cube.getOlapMeasures();

    OlapMeasure oldMeasure = AnnotationUtil.getOlapMeasure(INIT_BUYPRICE_NAME, olapMeasures);
    assertNotNull(oldMeasure);
    INIT_BUYPRICE_COLUMN.equals(oldMeasure.getLogicalColumn().getName());

    oldMeasure = AnnotationUtil.getOlapMeasure(EXISTING_NAME, olapMeasures);
    assertNotNull(oldMeasure);
    EXISTING_COLUMN.equals(oldMeasure.getLogicalColumn().getName());
  }
  @Test
  public void testMondrianUpdateFormat() throws Exception {
    File mondrianSchemaXmlFile = new File(MONDRIAN_TEST_FILE_PATH);

    Document mondrianSchemaXmlDoc =
        DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mondrianSchemaXmlFile);
    assertTrue(mondrianSchemaXmlDoc != null);
    // Check existing state
    assertTrue(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            INIT_BUYPRICE_COLUMN,
            AnnotationUtil.NAME_ATTRIB,
            INIT_BUYPRICE_COLUMN));
    assertTrue(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            INIT_BUYPRICE_COLUMN,
            AnnotationUtil.FORMATSTRING_ATTRIB,
            INIT_FORMAT));

    // Changing the aggregation type
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_MONDRIAN_FORMULA);
    updateMeasure.setName(INIT_BUYPRICE_COLUMN);
    updateMeasure.setFormat(NEW_FORMAT);
    boolean isApplied = updateMeasure.apply(mondrianSchemaXmlDoc);
    assertTrue(isApplied);

    // Check change
    assertTrue(
        mondrianSchemaXmlDoc.getElementsByTagName(AnnotationUtil.MEASURE_ELEMENT_NAME).getLength()
            > 0);
    assertTrue(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            INIT_BUYPRICE_COLUMN,
            AnnotationUtil.NAME_ATTRIB,
            INIT_BUYPRICE_COLUMN));
    assertTrue(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            INIT_BUYPRICE_COLUMN,
            AnnotationUtil.FORMATSTRING_ATTRIB,
            NEW_FORMAT));
  }
  @Test
  public void testValidate() {
    // Valid
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_FORMULA);
    updateMeasure.setName(NEW_BUYPRICE_NAME);
    try {
      updateMeasure.validate();
    } catch (Exception e) {
      fail("Should be valid be was not");
    }

    updateMeasure.setMeasure("");
    try {
      updateMeasure.validate();
      fail("Exception should of been thrown");
    } catch (Exception e) {
    }
  }
  @Test
  public void testMondrianNoDuplicateNames() throws Exception {
    File mondrianSchemaXmlFile = new File(MONDRIAN_TEST_FILE_PATH);

    Document mondrianSchemaXmlDoc =
        DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mondrianSchemaXmlFile);

    // Renaming the measure
    UpdateMeasure updateMeasure = new UpdateMeasure();
    updateMeasure.setMeasure(INIT_MEASURE_MONDRIAN_FORMULA);
    updateMeasure.setName(EXISTING_COLUMN);
    assertFalse(updateMeasure.apply(mondrianSchemaXmlDoc));

    // Ensure nothing has changed
    assertTrue(mondrianSchemaXmlDoc != null);
    assertTrue(
        mondrianSchemaXmlDoc.getElementsByTagName(AnnotationUtil.MEASURE_ELEMENT_NAME).getLength()
            > 0);

    // Ensure new Element was not created
    assertFalse(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            NEW_BUYPRICE_NAME,
            AnnotationUtil.NAME_ATTRIB,
            ""));

    // Ensure original element still exists
    assertTrue(
        AnnotationUtil.validateNodeAttribute(
            mondrianSchemaXmlDoc,
            AnnotationUtil.MEASURE_ELEMENT_NAME,
            EXISTING_COLUMN,
            AnnotationUtil.NAME_ATTRIB,
            EXISTING_COLUMN));
  }