Untitled

 avatar
unknown
plain_text
9 months ago
7.6 kB
16
Indexable
@Given("the stored configuration has no pinned columns")
public void theStoredConfigurationHasNoPinnedColumns() {
    logger.info("Verifying stored configuration has no pinned columns");
    
    String bearerToken = testContext.getContext("bearerToken", String.class);
    String tailNumber = testContext.getContext("configId", String.class);
    String widgetId = testContext.getContext("configWidgetId", String.class);
    
    // Build cache key for verification
    String cacheKey = String.format("config.%s.%s.pinnedColumns.none", tailNumber, widgetId);
    
    // Check if already verified
    if (verificationCache.isVerified(cacheKey)) {
        logger.info("Skipped: Already verified no pinned columns (cached)");
        if (verificationCache.hasCachedData(cacheKey)) {
            String cachedValue = verificationCache.getCachedData(cacheKey, String.class);
            testContext.setContext("configWidgetValue", cachedValue);
        }
        return;
    }
    
    try {
        // Get current configuration
        String widgetValue = testContext.getContext("configWidgetValue", String.class);
        
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode widgetJson = objectMapper.readTree(widgetValue);
        ObjectNode rootNode = (ObjectNode) widgetJson;
        ObjectNode columnsNode = (ObjectNode) rootNode.get("columns");
        
        // Remove pinnedColumns if exists, or ensure it's empty array
        if (columnsNode.has("pinnedColumns")) {
            ArrayNode pinnedColumns = (ArrayNode) columnsNode.get("pinnedColumns");
            if (pinnedColumns.size() > 0) {
                // Clear the array
                columnsNode.set("pinnedColumns", objectMapper.createArrayNode());
                logger.info("Cleared existing pinned columns");
                
                // Save the updated configuration
                String updatedWidgetValue = objectMapper.writeValueAsString(rootNode);
                testContext.setContext("configWidgetValue", updatedWidgetValue);
                
                // Persist to database
                String escapedWidgetValue = updatedWidgetValue
                    .replace("\\", "\\\\")
                    .replace("\"", "\\\"")
                    .replace("\n", "\\n")
                    .replace("\r", "\\r");
                
                String mutation = String.format("""
                    mutation {
                        addOrUpdateWidgetsState(widgetState: {
                            id: "%s"
                            widgetId: "%s"
                            widgetValue: "%s"
                        }) {
                            id
                            widgetId
                            widgetValue
                        }
                    }
                    """, tailNumber, widgetId, escapedWidgetValue);
                
                Response response = graphQLHelper.sendGraphQLQuery(mutation, null, bearerToken);
                response.then().statusCode(200);
                
                String savedValue = response.jsonPath().getString("data.addOrUpdateWidgetsState.widgetValue");
                testContext.setContext("configWidgetValue", savedValue);
                
                // Cache the result
                verificationCache.cacheData(cacheKey, savedValue);
            } else {
                logger.info("Configuration already has no pinned columns");
            }
        } else {
            // Add empty pinnedColumns array
            columnsNode.set("pinnedColumns", objectMapper.createArrayNode());
            logger.info("Added empty pinnedColumns array");
            
            String updatedWidgetValue = objectMapper.writeValueAsString(rootNode);
            testContext.setContext("configWidgetValue", updatedWidgetValue);
        }
        
        verificationCache.markVerified(cacheKey);
        logger.info("Verified configuration has no pinned columns");
        
    } catch (Exception e) {
        logger.error("Failed to ensure no pinned columns", e);
        throw new RuntimeException("Failed to ensure no pinned columns: " + e.getMessage(), e);
    }
}

@Then("the saved configuration has the following pinned columns")
public void theSavedConfigurationHasTheFollowingPinnedColumns(io.cucumber.datatable.DataTable dataTable) {
    logger.info("Verifying saved configuration has expected pinned columns");
    
    String bearerToken = testContext.getContext("bearerToken", String.class);
    String tailNumber = testContext.getContext("configId", String.class);
    String widgetId = testContext.getContext("configWidgetId", String.class);
    
    // Parse expected columns from data table
    List<String> expectedPinnedColumns = new ArrayList<>();
    List<Map<String, String>> rows = dataTable.asMaps();
    
    for (Map<String, String> row : rows) {
        String columnName = row.get("columnName");
        if (columnName != null && !columnName.isEmpty()) {
            expectedPinnedColumns.add(columnName);
        }
    }
    
    logger.info("Expected pinned columns: {}", expectedPinnedColumns);
    
    // Query the saved configuration
    String query = String.format("""
        query {
            getWidgetsState(id: "%s", widgetId: "%s") {
                id
                widgetId
                widgetValue
            }
        }
        """, tailNumber, widgetId);
    
    Response response = graphQLHelper.sendGraphQLQuery(query, null, bearerToken);
    response.then().statusCode(200);
    
    String widgetValue = response.jsonPath().getString("data.getWidgetsState.widgetValue");
    if (widgetValue == null) {
        throw new AssertionError("No saved configuration found for tail " + tailNumber);
    }
    
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode widgetJson = objectMapper.readTree(widgetValue);
        JsonNode columnsNode = widgetJson.get("columns");
        
        if (columnsNode == null) {
            throw new AssertionError("No 'columns' found in saved configuration");
        }
        
        // Get actual pinned columns
        List<String> actualPinnedColumns = new ArrayList<>();
        if (columnsNode.has("pinnedColumns")) {
            ArrayNode pinnedColumns = (ArrayNode) columnsNode.get("pinnedColumns");
            for (JsonNode node : pinnedColumns) {
                actualPinnedColumns.add(node.asText());
            }
        }
        
        logger.info("Actual pinned columns: {}", actualPinnedColumns);
        
        // Verify the lists match
        if (expectedPinnedColumns.size() != actualPinnedColumns.size()) {
            throw new AssertionError(String.format(
                "Pinned columns count mismatch: expected %d but got %d. Expected: %s, Actual: %s",
                expectedPinnedColumns.size(), actualPinnedColumns.size(),
                expectedPinnedColumns, actualPinnedColumns));
        }
        
        for (String expectedColumn : expectedPinnedColumns) {
            if (!actualPinnedColumns.contains(expectedColumn)) {
                throw new AssertionError(String.format(
                    "Column '%s' not found in pinned columns. Expected: %s, Actual: %s",
                    expectedColumn, expectedPinnedColumns, actualPinnedColumns));
            }
        }
        
        logger.info("Successfully verified all expected pinned columns are present");
        
    } catch (Exception e) {
        logger.error("Failed to verify pinned columns", e);
        throw new RuntimeException("Failed to verify pinned columns: " + e.getMessage(), e);
    }
}
Editor is loading...
Leave a Comment