Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add dynamic path param support #1808

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/main/java/com/shaft/api/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,60 @@
return this;
}

/**
* Dynamically sets path parameters by replacing placeholders in the `serviceName` using key-value pairs.
*
* @param paramMap a Map where the keys are the placeholder names (without curly braces) and the values are the replacement values.
* @return the current instance of RequestBuilder for method chaining.
* @throws IllegalArgumentException if any placeholder in the Map is not found in the `serviceName`.
*/
public RequestBuilder setPathParameters(Map<String, Object> paramMap) {
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
String key = entry.getKey();
String value = String.valueOf(entry.getValue());
if (serviceName.contains("{" + key + "}")) {
serviceName = serviceName.replace("{" + key + "}", value);
} else {
throw new IllegalArgumentException(
"Path parameter {" + key + "} not found in the serviceName: " + serviceName

Check warning on line 143 in src/main/java/com/shaft/api/RequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/shaft/api/RequestBuilder.java#L143

Added line #L143 was not covered by tests
);
}
}
return this;
}

/**
* Dynamically sets path parameters by replacing placeholders in the `serviceName` using ordered string values.
*
* @param values the string values to replace placeholders in the `serviceName`, provided in the exact order of their appearance.
* @return the current instance of RequestBuilder for method chaining.
* @throws IllegalArgumentException if the number of placeholders does not match the number of provided values,
* or if no placeholders are found in the `serviceName`.
*/
public RequestBuilder setPathParameters(String... values) {
// Check if serviceName contains any placeholders
if (!serviceName.contains("{") || !serviceName.contains("}")) {
throw new IllegalArgumentException(
"No placeholders found in the serviceName: " + serviceName + ". Cannot set path parameters."

Check warning on line 162 in src/main/java/com/shaft/api/RequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/shaft/api/RequestBuilder.java#L162

Added line #L162 was not covered by tests
);
}

String[] placeholders = serviceName.split("\\{");
int placeholderCount = placeholders.length - 1; // Count of placeholders
if (placeholderCount != values.length) {
throw new IllegalArgumentException(
"Number of provided values (" + values.length + ") does not match the number of placeholders (" +

Check warning on line 170 in src/main/java/com/shaft/api/RequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/shaft/api/RequestBuilder.java#L170

Added line #L170 was not covered by tests
placeholderCount + ") in the serviceName: " + serviceName
);
}

for (int i = 1; i < placeholders.length; i++) {
String placeholder = placeholders[i].split("}")[0]; // Extract placeholder name
serviceName = serviceName.replace("{" + placeholder + "}", values[i - 1]);
}
return this;
}

/**
* Sets the parameters (if any) for the API request that you're currently building. A request usually has only one of the following: urlArguments, parameters+type, or body
*
Expand Down Expand Up @@ -361,4 +415,4 @@
public enum AuthenticationType {
BASIC, FORM, NONE
}
}
}
49 changes: 49 additions & 0 deletions src/test/java/com/shaft/api/PathParamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.shaft.api;

import com.shaft.driver.SHAFT;
import org.testng.annotations.*;
import java.util.*;


public class PathParamTest {
// Initialize SHAFT.API with the base URI
SHAFT.API api = new SHAFT.API("https://petstore.swagger.io/v2");
public static final String GET_USER_BY_USERNAME = "/user/{username}"; // Get user by username

@Test
public void GetUserByUsernameMap() {

Map<String, Object> parameters = Map.of(
"username", "string");
// Perform the GET request
api.get(GET_USER_BY_USERNAME) // Endpoint with a placeholder
.setPathParameters(parameters) // Substitute the placeholder dynamically
.setTargetStatusCode(200) // Expected status code
.perform(); // Execute the request

SHAFT.Report.log(">>>>>> Response Body:" + api.getResponseBody());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add assertion to validate the response.

}

@Test
public void GetUserByUsernameValue() {
// Perform the GET request
api.get("/user/{username}") // Endpoint with a placeholder
.setPathParameters( "string") // Pass the value for the placeholder
.setContentType("application/json")
.setTargetStatusCode(200)
.perform();

SHAFT.Report.log(">>>>>> Response Body:" + api.getResponseBody());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add assertion to validate the response.

}

@Test
public void N_GetUserByUsernameTest3() {
api.get("/{resource}/{username}")
.setContentType("application/json")
.setPathParameters("user", "string")
.setTargetStatusCode(200)
.perform();

SHAFT.Report.log("Response Body: " + api.getResponseBody());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add assertion to validate the response.

}
}
Loading