From 7752528c2c7b9bccb527c5ba137e70b87ca29097 Mon Sep 17 00:00:00 2001 From: 282000ypk Date: Wed, 14 Jan 2026 21:19:54 +0530 Subject: [PATCH 1/2] Fixed CancelSubscription failure issue --- pom.xml | 5 - .../sdk/pg/common/http/HttpCommand.java | 226 +++++++++--------- src/test/CancelSubscriptionTest.java | 15 ++ src/test/HttpCommandTest.java | 24 ++ 4 files changed, 154 insertions(+), 116 deletions(-) diff --git a/pom.xml b/pom.xml index dcf027c..c2ba665 100644 --- a/pom.xml +++ b/pom.xml @@ -195,11 +195,6 @@ true 4 - - 1.22.0 - - true - diff --git a/src/main/java/com/phonepe/sdk/pg/common/http/HttpCommand.java b/src/main/java/com/phonepe/sdk/pg/common/http/HttpCommand.java index d72f6c6..b7dd396 100644 --- a/src/main/java/com/phonepe/sdk/pg/common/http/HttpCommand.java +++ b/src/main/java/com/phonepe/sdk/pg/common/http/HttpCommand.java @@ -48,121 +48,125 @@ @Slf4j public class HttpCommand { - private final OkHttpClient client; - @NotNull private final String hostURL; - @NotNull private String url; - private List headers; - private final ObjectMapper objectMapper; - private final TypeReference responseTypeReference; - private final R requestData; - private final String encodingType; - @NotBlank private final HttpMethodType methodName; - private Map queryParams; + private final OkHttpClient client; + @NotNull private final String hostURL; + @NotNull private String url; + private List headers; + private final ObjectMapper objectMapper; + private final TypeReference responseTypeReference; + private final R requestData; + private final String encodingType; + @NotBlank + private final HttpMethodType methodName; + private Map queryParams; - /** - * Returns the request body containing the serialized data. - * - * @return the request body as a RequestBody object - */ - @SneakyThrows - private RequestBody prepareRequestBody() { - if (Objects.equals(encodingType, APPLICATION_JSON)) { - return RequestBody.create( - objectMapper.writeValueAsBytes(requestData), MediaType.parse(encodingType)); - } + /** + * Returns the request body containing the serialized data. + * + * @return the request body as a RequestBody object + */ + @SneakyThrows + private RequestBody prepareRequestBody() { + if (Objects.equals(encodingType, APPLICATION_JSON)) { + return RequestBody.create( + objectMapper.writeValueAsBytes(requestData), MediaType.parse(encodingType)); + } - if (Objects.equals(encodingType, APPLICATION_FORM_URLENCODED)) { - return (RequestBody) requestData; - } - return (RequestBody) requestData; - } + if (Objects.equals(encodingType, APPLICATION_FORM_URLENCODED)) { + return (RequestBody) requestData; + } + return (RequestBody) requestData; + } - /** - * Constructs an HTTP URL by appending the provided URL to the base host URL. - * - * @param url the URL to append to the base host URL - * @return the constructed HttpUrl object - * @throws IllegalArgumentException if the base host URL or the provided URL is invalid - */ - private HttpUrl prepareHttpURL(String url) { - HttpUrl.Builder urlBuilder = - Objects.requireNonNull(HttpUrl.parse(String.format("%s%s", this.hostURL, url))) - .newBuilder(); - if (!Objects.isNull(queryParams)) { - for (Map.Entry param : queryParams.entrySet()) { - urlBuilder.addQueryParameter(param.getKey(), param.getValue()); - } - } - return HttpUrl.get(urlBuilder.build().toString()); - } + /** + * Constructs an HTTP URL by appending the provided URL to the base host URL. + * + * @param url the URL to append to the base host URL + * @return the constructed HttpUrl object + * @throws IllegalArgumentException if the base host URL or the provided URL is invalid + */ + private HttpUrl prepareHttpURL(String url) { + HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(String.format("%s%s", this.hostURL, url))) + .newBuilder(); + if (!Objects.isNull(queryParams)) { + for (Map.Entry param : queryParams.entrySet()) { + urlBuilder.addQueryParameter(param.getKey(), param.getValue()); + } + } + return HttpUrl.get(urlBuilder.build() + .toString()); + } - /** - * Executes the HTTP request and returns the response. - * - * @return the response object - */ - @SneakyThrows - public T execute() { - log.info("Calling {} : {}{}", methodName, hostURL, url); - final HttpUrl httpUrl = prepareHttpURL(this.url); - final Request httpRequest = prepareRequest(httpUrl); - final Response response = client.newCall(httpRequest).execute(); - return handleResponse(response); - } + /** + * Executes the HTTP request and returns the response. + * + * @return the response object + */ + @SneakyThrows + public T execute() { + log.info("Calling {} : {}{}", methodName, hostURL, url); + final HttpUrl httpUrl = prepareHttpURL(this.url); + final Request httpRequest = prepareRequest(httpUrl); + final Response response = client.newCall(httpRequest) + .execute(); + return handleResponse(response); + } - @SneakyThrows - public T handleResponse(Response response) { - int responseCode = response.code(); - final byte[] responseBody = - Objects.nonNull(response.body()) ? response.body().bytes() : null; + @SneakyThrows + public T handleResponse(Response response) { + int responseCode = response.code(); + final byte[] responseBody = Objects.nonNull(response.body()) ? response.body() + .bytes() : null; + if (responseCode == 204) { + return null; + } + if (responseCode >= 200 && responseCode <= 299) { + return objectMapper.readValue(responseBody, responseTypeReference); + } + try { + PhonePeResponse phonePeResponse = objectMapper.readValue(responseBody, PhonePeResponse.class); + if (ExceptionMapper.codeToException.containsKey(responseCode)) { + ExceptionMapper.prepareCodeToException( + responseCode, response.message(), phonePeResponse); + } else if (responseCode >= 400 && responseCode <= 499) { + throw new ClientError(responseCode, response.message(), phonePeResponse); + } else if (responseCode >= 500 && responseCode <= 599) { + throw new ServerError(responseCode, response.message(), phonePeResponse); + } + throw new PhonePeException(responseCode, response.message(), phonePeResponse); + } catch (JsonProcessingException jsonProcessingException) { + throw new PhonePeException(responseCode, response.message()); + } + } - if (responseCode >= 200 && responseCode <= 299) { - return objectMapper.readValue(responseBody, responseTypeReference); - } - try { - PhonePeResponse phonePeResponse = - objectMapper.readValue(responseBody, PhonePeResponse.class); - if (ExceptionMapper.codeToException.containsKey(responseCode)) { - ExceptionMapper.prepareCodeToException( - responseCode, response.message(), phonePeResponse); - } else if (responseCode >= 400 && responseCode <= 499) { - throw new ClientError(responseCode, response.message(), phonePeResponse); - } else if (responseCode >= 500 && responseCode <= 599) { - throw new ServerError(responseCode, response.message(), phonePeResponse); - } - throw new PhonePeException(responseCode, response.message(), phonePeResponse); - } catch (JsonProcessingException jsonProcessingException) { - throw new PhonePeException(responseCode, response.message()); - } - } - - /** - * Constructs an HTTP request based on the provided HTTP URL, method name, and headers. - * - * @param httpUrl the HTTP URL for the request - * @return the constructed Request object - * @throws PhonePeException if the method name is not supported - */ - public Request prepareRequest(final HttpUrl httpUrl) { - if (methodName == HttpMethodType.POST) { - final Request.Builder requestBuilder = - new Request.Builder().url(httpUrl).post(prepareRequestBody()); - if (!Objects.isNull(headers)) { - for (HttpHeaderPair httpHeader : headers) { - requestBuilder.header(httpHeader.getKey(), httpHeader.getValue()); - } - } - return requestBuilder.build(); - } else if (methodName == HttpMethodType.GET) { - final Request.Builder requestBuilder = new Request.Builder().url(httpUrl).get(); - if (!Objects.isNull(headers)) { - for (HttpHeaderPair httpHeader : headers) { - requestBuilder.header(httpHeader.getKey(), httpHeader.getValue()); - } - } - return requestBuilder.build(); - } else { - throw new PhonePeException(405, "Method Not Supported"); - } - } + /** + * Constructs an HTTP request based on the provided HTTP URL, method name, and headers. + * + * @param httpUrl the HTTP URL for the request + * @return the constructed Request object + * @throws PhonePeException if the method name is not supported + */ + public Request prepareRequest(final HttpUrl httpUrl) { + if (methodName == HttpMethodType.POST) { + final Request.Builder requestBuilder = new Request.Builder().url(httpUrl) + .post(prepareRequestBody()); + if (!Objects.isNull(headers)) { + for (HttpHeaderPair httpHeader : headers) { + requestBuilder.header(httpHeader.getKey(), httpHeader.getValue()); + } + } + return requestBuilder.build(); + } else if (methodName == HttpMethodType.GET) { + final Request.Builder requestBuilder = new Request.Builder().url(httpUrl) + .get(); + if (!Objects.isNull(headers)) { + for (HttpHeaderPair httpHeader : headers) { + requestBuilder.header(httpHeader.getKey(), httpHeader.getValue()); + } + } + return requestBuilder.build(); + } else { + throw new PhonePeException(405, "Method Not Supported"); + } + } } diff --git a/src/test/CancelSubscriptionTest.java b/src/test/CancelSubscriptionTest.java index 2900b38..662d2a3 100644 --- a/src/test/CancelSubscriptionTest.java +++ b/src/test/CancelSubscriptionTest.java @@ -56,4 +56,19 @@ void testCancelSubscriptionIfResponseReceived() { wireMockServer.verify(exactly(1), postRequestedFor(urlPathMatching(url))); } + + @Test + void testCancelSubscriptionIfNoContentResponseReceived() { + wireMockServer.resetRequests(); + String merchantSubscriptionId = "c61d7378-a081-44ab-9559-ad8563a24b49"; + String url = String.format(SubscriptionConstants.CANCEL_API, merchantSubscriptionId); + + Map headers = getHeadersForSubscription(); + + addStubForPostRequest(url, headers, null, HttpStatus.SC_NO_CONTENT, Maps.newHashMap(), ""); + + subscriptionClient.cancelSubscription(merchantSubscriptionId); + + wireMockServer.verify(exactly(1), postRequestedFor(urlPathMatching(url))); + } } diff --git a/src/test/HttpCommandTest.java b/src/test/HttpCommandTest.java index 390006a..7d570b3 100644 --- a/src/test/HttpCommandTest.java +++ b/src/test/HttpCommandTest.java @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.ImmutableMap; import com.phonepe.sdk.pg.common.exception.PhonePeException; @@ -22,6 +24,8 @@ import com.phonepe.sdk.pg.common.http.HttpMethodType; import com.phonepe.sdk.pg.common.http.PhonePeResponse; import java.util.Map; +import okhttp3.MediaType; +import okhttp3.RequestBody; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import wiremock.org.apache.http.HttpStatus; @@ -74,4 +78,24 @@ void testIfPhonePeResponseNull() { Assertions.assertEquals(404, phonePeException.getHttpStatusCode()); Assertions.assertEquals("Not Found", phonePeException.getMessage()); } + + @Test + public void testHttpCommand204() throws JsonProcessingException { + String sampleJson = "{\"state\":\"PENDING\"}"; + RequestBody requestBody = RequestBody.create( + sampleJson.getBytes(), MediaType.parse(APPLICATION_JSON)); + HttpCommand httpCommand = + HttpCommand.builder() + .hostURL("http://localhost:30419") + .client(okHttpClient) + .requestData(requestBody) + .objectMapper(mapper) + .responseTypeReference(new TypeReference<>() {}) + .url("/testing") + .methodName(HttpMethodType.POST) + .build(); + addStubForPostRequest("/testing", sampleJson, HttpStatus.SC_NO_CONTENT, ""); + Void result = httpCommand.execute(); + Assertions.assertNull(result); + } } From 94399bd7ab576f3ca7900b51034b1aa0e39b1500 Mon Sep 17 00:00:00 2001 From: 282000ypk Date: Mon, 19 Jan 2026 15:08:19 +0530 Subject: [PATCH 2/2] Fixed sonar issues --- src/test/HttpCommandTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/HttpCommandTest.java b/src/test/HttpCommandTest.java index 7d570b3..afc1eeb 100644 --- a/src/test/HttpCommandTest.java +++ b/src/test/HttpCommandTest.java @@ -16,7 +16,6 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static org.junit.jupiter.api.Assertions.assertThrows; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.ImmutableMap; import com.phonepe.sdk.pg.common.exception.PhonePeException; @@ -80,7 +79,7 @@ void testIfPhonePeResponseNull() { } @Test - public void testHttpCommand204() throws JsonProcessingException { + void testHttpCommand204() { String sampleJson = "{\"state\":\"PENDING\"}"; RequestBody requestBody = RequestBody.create( sampleJson.getBytes(), MediaType.parse(APPLICATION_JSON));