Untitled

 avatar
unknown
plain_text
9 months ago
4.8 kB
14
Indexable
@When("the user retrieves their user information")
public void theUserRetrievesTheirUserInformation() {
    logger.info("Retrieving user information");
    
    String bearerToken = testContext.getContext("bearerToken", String.class);
    
    // Get username from context (set by userWithDefaultUsernameExists in Background)
    String username = testContext.getContext("defaultUsername", String.class);
    
    if (username == null) {
        // Fallback to default
        username = "GOLDADMIN";
        logger.warn("Username not found in context, using default: {}", username);
    }
    
    String query = String.format("""
        query {
            getUserInfo(userId: "%s") {
                userId
                userName
                emailAddress
            }
        }
        """, username);
    
    Response response = graphQLHelper.sendGraphQLQuery(query, null, bearerToken);
    
    logger.info("User info query response: {}", response.getBody().asPrettyString());
    
    // Store response for verification
    testContext.setContext("userInfoResponse", response);
    
    // Extract and store user info fields
    String userId = response.jsonPath().getString("data.getUserInfo.userId");
    String userName = response.jsonPath().getString("data.getUserInfo.userName");
    String emailAddress = response.jsonPath().getString("data.getUserInfo.emailAddress");
    
    testContext.setContext("retrievedUserId", userId);
    testContext.setContext("retrievedUserName", userName);
    testContext.setContext("retrievedEmailAddress", emailAddress);
    
    logger.info("Retrieved user info - userId: {}, userName: {}, email: {}", userId, userName, emailAddress);
}

@Then("the user information should be returned successfully")
public void theUserInformationShouldBeReturnedSuccessfully() {
    logger.info("Verifying user information was returned successfully");
    
    Response response = testContext.getContext("userInfoResponse", Response.class);
    
    if (response == null) {
        throw new IllegalStateException("No user info response found in TestContext");
    }
    
    // Verify status code
    response.then().statusCode(200);
    
    // Verify no GraphQL errors
    Object errors = response.jsonPath().get("errors");
    if (errors != null) {
        throw new AssertionError("GraphQL returned errors: " + errors);
    }
    
    // Verify data node exists
    Object data = response.jsonPath().get("data.getUserInfo");
    if (data == null) {
        throw new AssertionError("No user info data returned in response");
    }
    
    logger.info("User information returned successfully");
}

@Then("the userId should be {string}")
public void theUserIdShouldBe(String expectedUserId) {
    logger.info("Verifying userId is '{}'", expectedUserId);
    
    String actualUserId = testContext.getContext("retrievedUserId", String.class);
    
    if (actualUserId == null) {
        throw new AssertionError("User ID was not retrieved from response");
    }
    
    if (!expectedUserId.equals(actualUserId)) {
        throw new AssertionError(String.format(
            "User ID mismatch: expected '%s' but got '%s'",
            expectedUserId, actualUserId));
    }
    
    logger.info("User ID verified: {}", actualUserId);
}

@Then("the userName should be {string}")
public void theUserNameShouldBe(String expectedUserName) {
    logger.info("Verifying userName is '{}'", expectedUserName);
    
    String actualUserName = testContext.getContext("retrievedUserName", String.class);
    
    if (actualUserName == null) {
        throw new AssertionError("User name was not retrieved from response");
    }
    
    if (!expectedUserName.equals(actualUserName)) {
        throw new AssertionError(String.format(
            "User name mismatch: expected '%s' but got '%s'",
            expectedUserName, actualUserName));
    }
    
    logger.info("User name verified: {}", actualUserName);
}

@Then("the emailAddress should contain {string}")
public void theEmailAddressShouldContain(String expectedEmailOrDomain) {
    logger.info("Verifying emailAddress contains '{}'", expectedEmailOrDomain);
    
    String actualEmail = testContext.getContext("retrievedEmailAddress", String.class);
    
    if (actualEmail == null) {
        throw new AssertionError("Email address was not retrieved from response");
    }
    
    // Check if it's an exact match or contains the domain
    if (!actualEmail.equals(expectedEmailOrDomain) && !actualEmail.contains(expectedEmailOrDomain)) {
        throw new AssertionError(String.format(
            "Email address mismatch: expected '%s' to contain or equal '%s'",
            actualEmail, expectedEmailOrDomain));
    }
    
    logger.info("Email address verified: {}", actualEmail);
}
Editor is loading...
Leave a Comment