Skip to content

Commit c33b233

Browse files
committed
feat: update IT for API
1 parent 4f7c206 commit c33b233

File tree

4 files changed

+17
-32
lines changed

4 files changed

+17
-32
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Unit Test
3434
run: mvn test
3535
- name: Integration API Test
36-
run: mvn verify -P integration-test
36+
run: mvn verify -Pintegration-test
3737
- name: Upload test results
3838
uses: actions/upload-artifact@v3 # upload test results
3939
if: success() || failure() # run this step even if previous step failed

src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.geekshubs.calculator.rest;
22

33
import com.geekshubs.calculator.Calculator;
4+
import com.geekshubs.calculator.service.CalculatorService;
5+
46
import jakarta.ws.rs.GET;
57
import jakarta.ws.rs.Path;
68
import jakarta.ws.rs.Produces;
@@ -12,6 +14,8 @@
1214
@Path("/calculator")
1315
public class CalculatorAPI {
1416

17+
CalculatorService calculatorService = new CalculatorService();
18+
1519
@GET
1620
@Path("ping")
1721
@Produces(MediaType.TEXT_PLAIN)
@@ -23,27 +27,27 @@ public String ping() {
2327
@Path("add")
2428
@Produces(MediaType.APPLICATION_JSON)
2529
public Calculator Add(@QueryParam("x") int x, @QueryParam("y") int y) {
26-
return new Calculator(x, y, x + y);
30+
return calculatorService.Add(x, y);
2731
}
2832

2933
@GET
3034
@Path("sub")
3135
@Produces(MediaType.APPLICATION_JSON)
3236
public Calculator Sub(@QueryParam("x") int x, @QueryParam("y") int y) {
33-
return new Calculator(x, y, x - y);
37+
return calculatorService.Sub(x, y);
3438
}
3539

3640
@GET
3741
@Path("mul")
3842
@Produces(MediaType.APPLICATION_JSON)
3943
public Calculator Mul(@QueryParam("x") int x, @QueryParam("y") int y) {
40-
return new Calculator(x, y, x * y);
44+
return calculatorService.Mul(x, y);
4145
}
4246

4347
@GET
4448
@Path("div")
4549
@Produces(MediaType.APPLICATION_JSON)
4650
public Calculator Div(@QueryParam("x") int x, @QueryParam("y") int y) {
47-
return new Calculator(x, y, x / y);
51+
return calculatorService.Div(x, y);
4852
}
4953
}

src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,36 @@ public void testPing() throws Exception {
2525
@Test
2626
public void testAdd() throws Exception {
2727
CloseableHttpClient httpclient = HttpClients.createDefault();
28-
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/add?x=8&y=26");
28+
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/add?x=2&y=3");
2929
HttpResponse response = httpclient.execute(httpGet);
3030
assertEquals(200, response.getStatusLine().getStatusCode());
31-
assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":34"));
31+
assertThat(EntityUtils.toString(response.getEntity()), containsString("5"));
3232
}
3333

3434
@Test
3535
public void testSub() throws Exception {
3636
CloseableHttpClient httpclient = HttpClients.createDefault();
37-
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/sub?x=12&y=8");
37+
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/sub?x=2&y=3");
3838
HttpResponse response = httpclient.execute(httpGet);
3939
assertEquals(200, response.getStatusLine().getStatusCode());
40-
assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":4"));
40+
assertThat(EntityUtils.toString(response.getEntity()), containsString("-1"));
4141
}
4242

4343
@Test
4444
public void testMul() throws Exception {
4545
CloseableHttpClient httpclient = HttpClients.createDefault();
46-
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/mul?x=11&y=8");
46+
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/mul?x=2&y=3");
4747
HttpResponse response = httpclient.execute(httpGet);
4848
assertEquals(200, response.getStatusLine().getStatusCode());
49-
assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":88"));
49+
assertThat(EntityUtils.toString(response.getEntity()), containsString("6"));
5050
}
5151

5252
@Test
5353
public void testDiv() throws Exception {
5454
CloseableHttpClient httpclient = HttpClients.createDefault();
55-
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=12&y=12");
55+
HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=6&y=3");
5656
HttpResponse response = httpclient.execute(httpGet);
5757
assertEquals(200, response.getStatusLine().getStatusCode());
58-
assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":1"));
58+
assertThat(EntityUtils.toString(response.getEntity()), containsString("2"));
5959
}
6060
}

src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,4 @@ public void testPing() {
1313
assertThat(new CalculatorAPI().ping(), containsString("Welcome to Java Maven Calculator Web App!!!"));
1414
}
1515

16-
@Test
17-
public void testAdd() {
18-
assertEquals(34, new CalculatorAPI().Add(8, 26).getResult());
19-
}
20-
21-
@Test
22-
public void testSub() {
23-
assertEquals(4, new CalculatorAPI().Sub(12, 8).getResult());
24-
}
25-
26-
@Test
27-
public void testMul() {
28-
assertEquals(88, new CalculatorAPI().Mul(11, 8).getResult());
29-
}
30-
31-
@Test
32-
public void testDiv() {
33-
assertEquals(1, new CalculatorAPI().Div(12, 12).getResult());
34-
}
3516
}

0 commit comments

Comments
 (0)