Skip to content

Commit 6abec22

Browse files
Adding integration test to ReplaceDefaultHttpClientCodemod (#213)
- Adding integration test to ReplaceDefaultHttpClientCodemod
1 parent b285aaf commit 6abec22

File tree

8 files changed

+130
-0
lines changed

8 files changed

+130
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.codemodder.codemods.integration.tests;
2+
3+
import io.codemodder.codemods.integration.util.CodemodIntegrationTestMixin;
4+
import io.codemodder.codemods.integration.util.IntegrationTestMetadata;
5+
import io.codemodder.codemods.integration.util.IntegrationTestPropertiesMetadata;
6+
7+
/**
8+
* Test project: the project used for this test is using DefaultHttpClient to do a request to
9+
* https://restcountries.com/, the expectation is that DefaultHttpClient will be transformed into
10+
* HttpClientBuilder and the request is still working as expected. Test project location:
11+
* resources/test-applications/replace-apache-defaulthttpclient
12+
*/
13+
@IntegrationTestMetadata(
14+
codemodId = "replace-apache-defaulthttpclient",
15+
tests = {
16+
@IntegrationTestPropertiesMetadata(
17+
endpoint = "/test/country/mexico/capital",
18+
expectedResponse = "Mexico City")
19+
})
20+
public class ReplaceDefaultHttpClientCodemodIntegrationTest
21+
implements CodemodIntegrationTestMixin {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.springframework.boot.gradle.tasks.bundling.BootJar
2+
3+
plugins {
4+
java
5+
id("org.springframework.boot") version "3.1.5"
6+
id("io.spring.dependency-management") version "1.1.3"
7+
}
8+
9+
group = "ai.pixee.integration"
10+
11+
java {
12+
sourceCompatibility = JavaVersion.VERSION_17
13+
}
14+
15+
repositories {
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
implementation("org.springframework.boot:spring-boot-starter-web")
21+
implementation("org.apache.httpcomponents:httpclient:4.5.14")
22+
implementation("org.json:json:20231013")
23+
}
24+
25+
tasks.withType<Test> {
26+
useJUnitPlatform()
27+
}
28+
29+
tasks.named<BootJar>("bootJar") {
30+
archiveFileName.set("test-app.jar")
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = "replace-apache-defaulthttpclient"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ai.pixee.integration.replaceapachedefaulthttpclient;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ReplaceApacheDefaulthttpclientApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ReplaceApacheDefaulthttpclientApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ai.pixee.integration.replaceapachedefaulthttpclient.api;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.methods.HttpGet;
5+
import org.apache.http.impl.client.CloseableHttpClient;
6+
import org.apache.http.impl.client.DefaultHttpClient;
7+
import org.apache.http.util.EntityUtils;
8+
import org.json.JSONArray;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import java.io.IOException;
14+
15+
@RestController
16+
public class TestController {
17+
18+
private static final String COUNTRY_CAPITAL_URL = "https://restcountries.com/v3.1/name/%s?fields=capital";
19+
@GetMapping("/test/country/{countryName}/capital")
20+
public String getCapitalByCountryName(@PathVariable final String countryName) throws IOException {
21+
22+
CloseableHttpClient httpClient = new DefaultHttpClient();
23+
24+
HttpGet httpGet = new HttpGet(COUNTRY_CAPITAL_URL.formatted(countryName));
25+
26+
try {
27+
// Execute the request
28+
HttpResponse response = httpClient.execute(httpGet);
29+
30+
// Check the response status code
31+
int statusCode = response.getStatusLine().getStatusCode();
32+
33+
if (statusCode == 200) {
34+
// If the response status code is 200 (OK), read and print the response content
35+
String jsonResponse = EntityUtils.toString(response.getEntity());
36+
JSONArray jsonArray = new JSONArray(jsonResponse);
37+
38+
if (jsonArray.length() > 0) {
39+
JSONArray capitalArray = jsonArray.getJSONObject(0).optJSONArray("capital");
40+
if (capitalArray != null && capitalArray.length() > 0) {
41+
return capitalArray.getString(0);
42+
}
43+
}
44+
45+
return "Capital data not found for the given country.";
46+
47+
} else {
48+
return "Request failed with status code: " + statusCode;
49+
}
50+
} catch (Exception e) {
51+
return "Error: Exception occurred while making the request " + e.getMessage();
52+
} finally {
53+
httpClient.close();
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)