Untitled

mail@pastecode.io avatar
unknown
java
a year ago
5.8 kB
1
Indexable
Never
    public String refreshDataTypesForViewColumns(Context ctx, String schemaXml) {
        SchemaDef schemaDef;
        String newSchemaXml = null;
        try {
            schemaDef = XML.read(schemaXml, SchemaDef.class);
            for (BusinessViewDef businessViewDef : schemaDef.getBusinessViews()) {
                for (SchemaDef.ViewColumnDef viewColumnDef : businessViewDef.getColumns()) {
                    String dataType;
                    if (viewColumnDef.getSource() == null && viewColumnDef.getFormula() != null) {
                        dataType = getTenantFunc.apply(ctx).getEngine().getFormulaDataTypeName(ctx.getUser(), viewColumnDef.getFormula());
                    } else {
                        dataType = getTenantFunc.apply(ctx).getEngine().getColumnDataTypeName(viewColumnDef.getSource());
                    }
                    updateDataTypeOfViewColumn(viewColumnDef, dataType);
                }
            }
            newSchemaXml = XML.write(schemaDef);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newSchemaXml;
    }

    private void updateDataTypeOfViewColumn(SchemaDef.ViewColumnDef viewColumnDef, String dataType) {
        if (dataType == null) return;
        dataType = dataType.toLowerCase();
        if (dataType.contains("(")) {
           int startIndex = dataType.indexOf("(");
           int comma = dataType.indexOf(",");
           int endIndex = dataType.indexOf(")");
           String type = dataType.substring(0, startIndex);
           int precision = Integer.parseInt(dataType.substring(startIndex + 1, comma));
           int scale = Integer.parseInt(dataType.substring(comma + 1, endIndex));
           viewColumnDef.setType(com.incorta.metamodel.Type.byName(type));
           viewColumnDef.setPrecision(precision);
           viewColumnDef.setScale(scale);
        } else {
            viewColumnDef.setType(com.incorta.metamodel.Type.byName(dataType));
        }
    }
    
    
    
        @Test
    public void testRefreshDataTypesForViewColumns() {
        // Given
        HashMap<String, String[]> columnNameToDataType = new HashMap<>();
        columnNameToDataType.put("longColumn", new String[]{"long"});
        columnNameToDataType.put("doubleColumn", new String[]{"double"});
        columnNameToDataType.put("integerColumn", new String[]{"integer"});
        columnNameToDataType.put("decimalColumn", new String[]{"decimal", "18", "4"});
        columnNameToDataType.put("formulaColumn", new String[]{"boolean"});

        String schemaXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <schema version=\"1.0\" name=\"test_set_data_types\" loader=\"test_set_data_types\"> <tables /> " 
            + "<businessViews> <businessView name=\"New_View\" viewOrder=\"0\"> <column name=\"longColumn\" label=\"longColumn\" source=\"longColumn\" function=\"measure\" " 
            + "type=\"integer\" /> <column name=\"doubleColumn\" label=\"doubleColumn\" source=\"doubleColumn\" function=\"measure\" type=\"long\" /> <column name=\"integerColumn\" " 
            + "label=\"integerColumn\" source=\"integerColumn\" function=\"measure\" type=\"double\" /> <column name=\"decimalColumn\" label=\"decimalColumn\" source=\"decimalColumn\" " 
            + "function=\"measure\" type=\"integer\" /> <column name=\"formulaColumn\" label=\"formulaColumn\" formula=\"formulaColumn\" function=\"measure\" type=\"integer\" />" 
            + " </businessView> </businessViews> </schema>";

        Engine engine = mock(Engine.class);
        Context context = mock(Context.class);

        // When
        doReturn("user").when(context).getUser();
        for (String columnName : columnNameToDataType.keySet()) {
            String[] dataTypeWithPrecisionAndScale = columnNameToDataType.get(columnName);
            String dataType = dataTypeWithPrecisionAndScale[0];
            if (dataType.equals("decimal")) {
                dataType += "(" + dataTypeWithPrecisionAndScale[1] + "," + dataTypeWithPrecisionAndScale[2] + ")";
            }
            if (columnName.startsWith("formula")) {
                doReturn(dataType).when(engine).getFormulaDataTypeName(anyString(), eq(columnName));
            } else {
                doReturn(dataType).when(engine).getColumnDataTypeName(columnName);
            }
        }
        doReturn(engine).when(tenant).getEngine();

        //Then
        String updatedSchemaXml = handler.refreshDataTypesForViewColumns(context, schemaXml);

        try {
            SchemaDef actualSchemaDef = XML.read(updatedSchemaXml, SchemaDef.class);
            SchemaDef.BusinessViewDef[] businessViewDefs = actualSchemaDef.getBusinessViews();
            assertEquals(businessViewDefs.length, 1);
            for (SchemaDef.ViewColumnDef viewColumnDef : businessViewDefs[0].getColumns()) {
                String actualDataType = viewColumnDef.getType().getName();
                if (actualDataType.equalsIgnoreCase("decimal")) {
                    actualDataType += "(" + viewColumnDef.getPrecision() + "," + viewColumnDef.getScale() + ")";
                }

                String[] expectedDataTypeWithPrecisionAndScale = (viewColumnDef.getFormula() != null ) ? columnNameToDataType.get(viewColumnDef.getFormula()) : columnNameToDataType.get(viewColumnDef.getSource());
                String expectedDataType = expectedDataTypeWithPrecisionAndScale[0];
                if (expectedDataTypeWithPrecisionAndScale.length > 1) {
                    expectedDataType += "(" + expectedDataTypeWithPrecisionAndScale[1] + "," + expectedDataTypeWithPrecisionAndScale[2] + ")";
                }

                assertEquals(expectedDataType, actualDataType);
            }
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    }