Skip to content

Commit aca31cf

Browse files
test coverage
1 parent 1b645ce commit aca31cf

File tree

4 files changed

+107
-82
lines changed

4 files changed

+107
-82
lines changed

.github/workflows/jacoco.yml

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,40 @@
1-
name: build
1+
name: Java CI with JaCoCo
22

33
on:
44
push:
5-
branches: [ feat/CS-36689-compare-merge-branch ]
6-
paths: [ '**.java', '.github/workflows/jacoco.yml', 'pom.xml' ]
7-
pull_request:
8-
branches: [ feat/CS-36689-compare-merge-branch ]
9-
workflow_dispatch:
5+
branches:
6+
- main
107

118
jobs:
12-
jacoco-java-maven:
13-
9+
build-and-test:
1410
runs-on: ubuntu-latest
15-
16-
steps:
17-
- name: Checkout
18-
uses: actions/checkout@v3
19-
20-
21-
- name: Set up JDK 11
22-
uses: actions/setup-java@v3
23-
with:
24-
distribution: 'adopt'
25-
java-version: '11'
26-
27-
- name: Build with Maven
28-
run: mvn -B package -Pcoverage
29-
30-
- name: Generate JaCoCo badge
31-
id: jacoco
32-
uses: cicirello/jacoco-badge-generator@v2.9.0
33-
with:
34-
badges-directory: badges
35-
generate-branches-badge: true
36-
generate-summary: true
3711

38-
- name: Log coverage percentages to workflow output
39-
run: |
40-
echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
41-
echo "branches = ${{ steps.jacoco.outputs.branches }}"
42-
43-
- name: Upload JaCoCo coverage report as a workflow artifact
44-
uses: actions/upload-artifact@v3
45-
with:
46-
name: jacoco-report
47-
path: target/site/jacoco/
48-
49-
50-
- name: Comment on PR with coverage percentages
51-
if: ${{ github.event_name == 'pull_request' }}
52-
run: |
53-
REPORT=$(<badges/coverage-summary.json)
54-
COVERAGE=$(jq -r '.coverage' <<< "$REPORT")%
55-
BRANCHES=$(jq -r '.branches' <<< "$REPORT")%
56-
NEWLINE=$'\n'
57-
BODY="## JaCoCo Test Coverage Summary Statistics${NEWLINE}* __Coverage:__ ${COVERAGE}${NEWLINE}* __Branches:__ ${BRANCHES}"
58-
gh pr comment ${{github.event.pull_request.number}} -b "${BODY}"
59-
continue-on-error: true
60-
env:
61-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62-
63-
- name: Get the Coverage info
64-
run: |
65-
echo "Total coverage ${{ steps.jacoco.outputs.coverage-overall }}"
66-
echo "Changed Files coverage ${{ steps.jacoco.outputs.coverage-changed-files }}"
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up JDK 11
17+
uses: actions/setup-java@v2
18+
with:
19+
java-version: "11"
20+
21+
- name: Build with Maven
22+
run: mvn clean package
23+
24+
- name: Run Tests with JaCoCo
25+
run: mvn jacoco:prepare-agent test jacoco:report
26+
27+
- name: Check Code Coverage
28+
run: |
29+
total_lines=$(xmllint --xpath 'string(//counter[@type="LINE"]/@covered)' target/site/jacoco/jacoco.xml)
30+
total_lines=${total_lines:-0}
31+
total_missed=$(xmllint --xpath 'string(//counter[@type="LINE"]/@missed)' target/site/jacoco/jacoco.xml)
32+
total_missed=${total_missed:-0}
33+
total_percentage=$(awk "BEGIN { pc=100*${total_lines}/(${total_lines}+${total_missed}) } { printf \"%f\", pc }")
34+
echo "Total Coverage Percentage: $total_percentage"
35+
if (( $(bc <<< "$total_percentage < 80.0") )); then
36+
echo "Code Coverage is below 80% - failing the CI build."
37+
exit 1
38+
else
39+
echo "Code Coverage is satisfactory - CI build passed."
40+
fi

src/main/java/com/contentstack/cms/stack/Webhook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public Call<ResponseBody> export() {
302302
public Call<ResponseBody> importWebhook(@NotNull String fileName, @NotNull String jsonPath) {
303303
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
304304
MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
305-
.addFormDataPart("webhook", fileName, RequestBody.create(mediaType, new File(jsonPath))).build();
305+
.addFormDataPart("webhook", fileName, RequestBody.create(mediaType, jsonPath)).build();
306306
this.headers.put(Util.CONTENT_TYPE, Util.MULTIPART);
307307
return this.service.imports(this.headers, body);
308308
}

src/test/java/com/contentstack/cms/stack/AssetAPITest.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.contentstack.cms.Contentstack;
44
import com.contentstack.cms.TestClient;
5-
import com.google.gson.JsonObject;
6-
import com.google.gson.JsonParser;
75
import okhttp3.Request;
86
import okhttp3.ResponseBody;
97
import org.json.simple.JSONObject;
@@ -51,7 +49,8 @@ void testFindAssets() throws IOException {
5149
Assertions.assertEquals(443, request.url().port(), "port should be 443");
5250
Assertions.assertTrue(request.url().pathSegments().contains("v3"), "the first segment of url should be v3");
5351
Assertions.assertTrue(request.url().pathSegments().contains("assets"), "url segment should contain assets");
54-
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(), "query params should not be empty");
52+
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(),
53+
"query params should not be empty");
5554
}
5655

5756
@Order(2)
@@ -78,7 +77,8 @@ void testFetch() throws IOException {
7877
Assertions.assertEquals(443, request.url().port(), "port should be 443");
7978
Assertions.assertTrue(request.url().pathSegments().contains("v3"), "the first segment of url should be v3");
8079
Assertions.assertTrue(request.url().pathSegments().contains("assets"), "url segment should contain assets");
81-
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(), "query params should not be empty");
80+
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(),
81+
"query params should not be empty");
8282

8383
}
8484

@@ -97,11 +97,15 @@ void testGetAssetByFolderUid() throws IOException {
9797
Assertions.assertTrue(resp.raw().request().isHttps(), "always works on https");
9898
Assertions.assertEquals("GET", resp.raw().request().method(), "works with GET call");
9999
Assertions.assertEquals("https", resp.raw().request().url().scheme(), "the scheme should be https");
100-
Assertions.assertEquals("api.contentstack.io", resp.raw().request().url().host(), "host should be anything but not null");
100+
Assertions.assertEquals("api.contentstack.io", resp.raw().request().url().host(),
101+
"host should be anything but not null");
101102
Assertions.assertEquals(443, resp.raw().request().url().port(), "port should be 443");
102-
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("v3"), "the first segment of url should be v3");
103-
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("assets"), "url segment should contain assets");
104-
Assertions.assertFalse(Objects.requireNonNull(resp.raw().request().url().query()).isEmpty(), "query params should not be empty");
103+
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("v3"),
104+
"the first segment of url should be v3");
105+
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("assets"),
106+
"url segment should contain assets");
107+
Assertions.assertFalse(Objects.requireNonNull(resp.raw().request().url().query()).isEmpty(),
108+
"query params should not be empty");
105109

106110
}
107111

@@ -122,7 +126,8 @@ void testAssetSubFolder() throws IOException {
122126
Assertions.assertEquals(443, request.url().port(), "port should be 443");
123127
Assertions.assertTrue(request.url().pathSegments().contains("v3"), "the first segment of url should be v3");
124128
Assertions.assertTrue(request.url().pathSegments().contains("assets"), "url segment should contain assets");
125-
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(), "query params should not be empty");
129+
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(),
130+
"query params should not be empty");
126131

127132
}
128133

@@ -150,7 +155,8 @@ void testAssetUpload() {
150155
Assertions.assertEquals(443, request.url().port(), "port should be 443");
151156
Assertions.assertTrue(request.url().pathSegments().contains("v3"), "the first segment of url should be v3");
152157
Assertions.assertTrue(request.url().pathSegments().contains("assets"), "url segment should contain assets");
153-
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(), "query params should not be empty");
158+
Assertions.assertFalse(Objects.requireNonNull(request.url().query()).isEmpty(),
159+
"query params should not be empty");
154160

155161
}
156162

@@ -176,11 +182,15 @@ void testAssetReplace() throws IOException {
176182
Assertions.assertTrue(resp.raw().request().isHttps(), "always works on https");
177183
Assertions.assertEquals("PUT", resp.raw().request().method(), "works with GET call");
178184
Assertions.assertEquals("https", resp.raw().request().url().scheme(), "the scheme should be https");
179-
Assertions.assertEquals("api.contentstack.io", resp.raw().request().url().host(), "host should be anything but not null");
185+
Assertions.assertEquals("api.contentstack.io", resp.raw().request().url().host(),
186+
"host should be anything but not null");
180187
Assertions.assertEquals(443, resp.raw().request().url().port(), "port should be 443");
181-
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("v3"), "the first segment of url should be v3");
182-
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("assets"), "url segment should contain assets");
183-
Assertions.assertFalse(Objects.requireNonNull(resp.raw().request().url().query()).isEmpty(), "query params should not be empty");
188+
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("v3"),
189+
"the first segment of url should be v3");
190+
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("assets"),
191+
"url segment should contain assets");
192+
Assertions.assertFalse(Objects.requireNonNull(resp.raw().request().url().query()).isEmpty(),
193+
"query params should not be empty");
184194

185195
}
186196

@@ -202,11 +212,15 @@ void testAssetGeneratePermanentUrl() throws IOException {
202212
Assertions.assertTrue(resp.raw().request().isHttps(), "always works on https");
203213
Assertions.assertEquals("PUT", resp.raw().request().method(), "works with GET call");
204214
Assertions.assertEquals("https", resp.raw().request().url().scheme(), "the scheme should be https");
205-
Assertions.assertEquals("api.contentstack.io", resp.raw().request().url().host(), "host should be anything but not null");
215+
Assertions.assertEquals("api.contentstack.io", resp.raw().request().url().host(),
216+
"host should be anything but not null");
206217
Assertions.assertEquals(443, resp.raw().request().url().port(), "port should be 443");
207-
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("v3"), "the first segment of url should be v3");
208-
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("assets"), "url segment should contain assets");
209-
Assertions.assertFalse(Objects.requireNonNull(resp.raw().request().url().query()).isEmpty(), "query params should not be empty");
218+
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("v3"),
219+
"the first segment of url should be v3");
220+
Assertions.assertTrue(resp.raw().request().url().pathSegments().contains("assets"),
221+
"url segment should contain assets");
222+
Assertions.assertFalse(Objects.requireNonNull(resp.raw().request().url().query()).isEmpty(),
223+
"query params should not be empty");
210224
}
211225

212226
@Test
@@ -231,5 +245,4 @@ void testAssetDownloadPermanentUrl() throws IOException {
231245
Assertions.assertTrue(request.url().pathSegments().contains("assets"), "url segment should contain assets");
232246
}
233247

234-
235248
}

src/test/java/com/contentstack/cms/stack/WebhookUnitTest.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010
import org.json.simple.parser.ParseException;
1111
import org.junit.jupiter.api.*;
1212

13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
import java.io.BufferedReader;
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.io.InputStreamReader;
1319
import java.util.HashMap;
1420

1521
@Tag("unit")
1622
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1723
class WebhookUnitTest {
1824

25+
private static String ENTYRY_FILE = null;
1926
private static final String AUTHTOKEN = TestClient.AUTHTOKEN;
2027
private static final String API_KEY = TestClient.API_KEY;
2128
private static final String _webhook_uid = TestClient.USER_ID;
@@ -151,6 +158,7 @@ void createWebhook() {
151158
}
152159

153160
@Test
161+
@Order(8)
154162
void updateWebhook() {
155163
assert _webhook_uid != null;
156164
Request request = webhook.update(body).request();
@@ -166,6 +174,7 @@ void updateWebhook() {
166174
}
167175

168176
@Test
177+
@Order(9)
169178
void deleteWebhook() {
170179
assert _webhook_uid != null;
171180
Request request = webhook.delete().request();
@@ -181,6 +190,7 @@ void deleteWebhook() {
181190
}
182191

183192
@Test
193+
@Order(10)
184194
void exportWebhook() {
185195
assert _webhook_uid != null;
186196
Request request = webhook.export().request();
@@ -196,11 +206,39 @@ void exportWebhook() {
196206
}
197207

198208
@Test
209+
@Order(11)
210+
public void testReadEntryJson() {
211+
// Load the entry.json file using the ClassLoader
212+
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("entry.json");
213+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
214+
215+
while ((ENTYRY_FILE = reader.readLine()) != null) {
216+
System.out.println(ENTYRY_FILE);
217+
}
218+
219+
} catch (IOException e) {
220+
e.printStackTrace();
221+
}
222+
assertNotNull(getClass().getClassLoader().getResourceAsStream("entry.json"));
223+
}
224+
225+
@Test
226+
@Order(12)
199227
@Disabled
200228
void importWebhook() {
201-
assert _webhook_uid != null;
202-
Request request = webhook.importWebhook("webhookFile", "/Application/Library/***REMOVED***/filename.json")
203-
.request();
229+
String line = "";
230+
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("entry.json");
231+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
232+
233+
while ((line = reader.readLine()) != null) {
234+
// Process each line here, if needed
235+
System.out.println(line);
236+
}
237+
238+
} catch (IOException e) {
239+
e.printStackTrace();
240+
}
241+
Request request = webhook.importWebhook("webhook", line).request();
204242
Assertions.assertEquals(2, request.headers().names().size());
205243
Assertions.assertEquals("GET", request.method());
206244
Assertions.assertTrue(request.url().isHttps());

0 commit comments

Comments
 (0)