/*!
 * HITACHI VANTARA PROPRIETARY AND CONFIDENTIAL
 *
 * Copyright 2002 - 2017 Hitachi Vantara. All rights reserved.
 *
 * NOTICE: All information including source code contained herein is, and
 * remains the sole property of Hitachi Vantara and its licensors. The intellectual
 * and technical concepts contained herein are proprietary and confidential
 * to, and are trade secrets of Hitachi Vantara and may be covered by U.S. and foreign
 * patents, or patents in process, and are protected by trade secret and
 * copyright laws. The receipt or possession of this source code and/or related
 * information does not convey or imply any rights to reproduce, disclose or
 * distribute its contents, or to manufacture, use, or sell anything that it
 * may describe, in whole or in part. Any reproduction, modification, distribution,
 * or public display of this information without the express written authorization
 * from Hitachi Vantara is strictly prohibited and in violation of applicable laws and
 * international treaties. Access to the source code contained herein is strictly
 * prohibited to anyone except those individuals and entities who have executed
 * confidentiality and non-disclosure agreements or other agreements with Hitachi Vantara,
 * explicitly covering such access.
 */
package org.pentaho.agilebi.modeler.models.annotations;

import org.junit.BeforeClass;
import org.junit.Test;
import org.pentaho.agilebi.modeler.ModelerPerspective;
import org.pentaho.agilebi.modeler.ModelerWorkspace;
import org.pentaho.agilebi.modeler.models.annotations.util.ModelITHelper;
import org.pentaho.di.core.KettleClientEnvironment;
import org.pentaho.di.core.Props;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.StepPluginType;
import org.pentaho.metadata.model.LogicalModel;
import org.pentaho.metadata.model.concept.types.AggregationType;
import org.pentaho.metadata.model.olap.OlapCube;
import org.pentaho.metastore.stores.memory.MemoryMetaStore;

import java.util.List;

import static org.junit.Assert.*;

public class CreateMeasureIT {
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    KettleClientEnvironment.init();
    PluginRegistry.addPluginType( StepPluginType.getInstance() );
    PluginRegistry.init();
    if ( !Props.isInitialized() ) {
      Props.init( 0 );
    }
  }
  @Test
  public void createMeasureWorksOnDimensionlessModel() throws Exception {
    ModelerWorkspace workspace = createWorkspace();

    CreateMeasure valueMeasure2 = new CreateMeasure();
    valueMeasure2.setField( "value" );
    valueMeasure2.setName( "value" );
    valueMeasure2.setAggregateType( AggregationType.SUM );
    valueMeasure2.apply( workspace, new MemoryMetaStore() );

    CreateMeasure valueMeasure = new CreateMeasure();
    valueMeasure.setField( "value" );
    valueMeasure.setName( "The Value" );
    valueMeasure.setAggregateType( AggregationType.SUM );
    valueMeasure.apply( workspace, new MemoryMetaStore() );

    CreateMeasure idMeasure = new CreateMeasure();
    idMeasure.setField( "id" );
    idMeasure.setName( "Id Count" );
    idMeasure.setAggregateType( AggregationType.COUNT );
    idMeasure.apply( workspace, new MemoryMetaStore() );
    OlapCube olapCube = getCubes( workspace ).get( 0 );
    assertEquals( 3, olapCube.getOlapMeasures().size() );
    assertEquals( 3, workspace.getModel().getMeasures().size() );
    assertEquals( 0, olapCube.getOlapDimensionUsages().size() );
    assertEquals( "The Value", olapCube.getOlapMeasures().get( 1 ).getName() );
    assertEquals( "Id Count", olapCube.getOlapMeasures().get( 2 ).getName() );
  }

  @SuppressWarnings( "unchecked" )
  private List<OlapCube> getCubes( ModelerWorkspace wspace ) {
    return (List<OlapCube>) wspace.getLogicalModel( ModelerPerspective.ANALYSIS ).getProperty(
      LogicalModel.PROPERTY_OLAP_CUBES );
  }

  private ModelerWorkspace createWorkspace() throws Exception {
    String sql = "DROP TABLE testtable; CREATE TABLE testTable\n"
      + "(\n"
      + "  value bigint\n"
      + ", id varchar(25)\n"
      + ");\n";
    return ModelITHelper.modelTable( "CreateMeasureIT-H2-DB", "testTable", sql );
  }
}
@@ -74,10 +74,10 @@
 - 20
+2
 1
-7
  Hit
@@ -2354,16 +2354,17 @@
   %7D%0A  %7D%0A
+%0A
   @Test%0A
@@ -3837,16 +3837,18 @@
 (%0A      
+  
 LogicalM
@@ -3871,24 +3871,24 @@
 AP_CUBES );%0A
-
   %7D%0A%0A  priva
@@ -3960,16 +3960,24 @@
 ng sql =
+%0A       
  %22DROP T
@@ -3977,24 +3977,34 @@
 %22DROP TABLE 
+IF EXISTS 
 testtable; C
@@ -4031,22 +4031,16 @@
 e%5Cn%22
-%0A     
  + %22(%5Cn%22
 %0A   
@@ -4035,22 +4035,16 @@
  + %22(%5Cn%22
-%0A     
  + %22  va
@@ -4056,22 +4056,16 @@
 igint%5Cn%22
-%0A     
  + %22, id
@@ -4080,16 +4080,22 @@
 (25)%5Cn%22%0A
+      
       + 
@@ -4188,9 +4188,10 @@
  );%0A  %7D%0A
-
 %7D
+%0A

Fix IT for 9.2 release