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 all 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
}
}
}
54 changes: 54 additions & 0 deletions src/test/java/com/shaft/api/PathParamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.shaft.api;

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

public class PathParamTest {
private final SHAFT.API api = new SHAFT.API("https://petstore.swagger.io/v2");
private static final String GET_USER_BY_USERNAME = "/user/{username}";
private static final String GET_RESOURCE_BY_USERNAME = "/{resource}/{username}";

/**
* Test fetching user details by username using a Map for path parameters.
* This test verifies that the API correctly substitutes a path parameter provided as a Map.
*/
@Test(description = "Validate fetching user details using a Map for path parameters")
public void testGetUserByUsernameWithMap() {
// Path parameter
Map<String, Object> parameters = Map.of("username", "string");

// Perform the GET request and validate
api.get(GET_USER_BY_USERNAME)
.setPathParameters(parameters)
.perform();

api.assertThatResponse().extractedJsonValue("username").isEqualTo("string");
}

/**
* Test fetching user details by username using a single value for the placeholder.
* This test ensures the API handles a single value substitution correctly.
*/
@Test(description = "Validate fetching user details using a single value for the path parameter")
public void testGetUserByUsernameWithValue() {
api.get(GET_USER_BY_USERNAME)
.setPathParameters("string") // Direct value substitution
.perform();

api.assertThatResponse().extractedJsonValue("username").isEqualTo("string");
}

/**
* Test fetching a resource using multiple path parameters.
* This test ensures the API supports multiple dynamic placeholders.
*/
@Test(description = "Validate fetching a resource using multiple dynamic path parameters")
public void testGetResourceWithMultiplePathParameters() {
api.get(GET_RESOURCE_BY_USERNAME)
.setPathParameters("user", "string") // Multiple values for placeholders
.perform();

api.assertThatResponse().extractedJsonValue("username").isEqualTo("string");
}
}
Loading