Skip to content

Commit faa9df4

Browse files
Address outstanding SonarCloud issues on main branch (#311)
* Fix SonarCloud issues: lambdas, exceptions, empty blocks, deprecated JWT API, duplicate tests Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com> * Remove unused FileNotFoundException import from AttachmentTests Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com>
1 parent 84135d5 commit faa9df4

File tree

9 files changed

+37
-101
lines changed

9 files changed

+37
-101
lines changed

xapi-client/src/test/java/dev/learning/xapi/client/DeleteActivityProfileRequestTests.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,6 @@ void whenBuildingDeleteActivityProfileRequestWithAllParametersThenNoExceptionIsT
3939

4040
}
4141

42-
@Test
43-
void whenBuildingDeleteActivityProfileRequestWithoutRegistrationThenNoExceptionIsThrown() {
44-
45-
// When Building DeleteActivityProfileRequest Without Registration
46-
Builder<?, ?> builder = DeleteActivityProfileRequest.builder()
47-
48-
.activityId(URI.create("https://example.com/activity/1"))
49-
50-
.profileId("bookmark");
51-
52-
// Then No Exception Is Thrown
53-
assertDoesNotThrow(builder::build);
54-
55-
}
56-
5742
@Test
5843
void whenBuildingDeleteActivityProfileRequestWithoutActivityIdThenExceptionIsThrown() {
5944

@@ -105,29 +90,4 @@ void givenDeleteActivityProfileRequestWithAllParametersWhenGettingURLThenResultI
10590

10691
}
10792

108-
@Test
109-
void givenDeleteActivityProfileRequestWithoutRegistrationWhenGettingURLThenResultIsExpected() {
110-
111-
// Given DeleteActivityProfileRequest Without Registration
112-
DeleteActivityProfileRequest request = DeleteActivityProfileRequest.builder()
113-
114-
.activityId(URI.create("https://example.com/activity/1"))
115-
116-
.profileId("bookmark")
117-
118-
.build();
119-
120-
Map<String, Object> queryParams = new HashMap<>();
121-
122-
// When Getting URL
123-
URI result =
124-
request.url(UriComponentsBuilder.fromUriString("https://example.com/xapi/"), queryParams)
125-
.build(queryParams);
126-
127-
// Then Result Is Expected
128-
assertThat(result, is(URI.create(
129-
"https://example.com/xapi/activities/profile?activityId=https%3A%2F%2Fexample.com%2Factivity%2F1&profileId=bookmark")));
130-
131-
}
132-
13393
}

xapi-client/src/test/java/dev/learning/xapi/client/XapiClientTests.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ void givenApiResponseIsEmptyWhenPostingStatementThenMissingResponseBodyException
398398
.definition(d -> d.addName(Locale.ENGLISH, "Simple Statement")))));
399399

400400
// Then MissingResponseBodyException Is Thrown
401-
assertThrows(MissingResponseBodyException.class, () -> response.block());
401+
assertThrows(MissingResponseBodyException.class, response::block);
402402

403403
}
404404

@@ -418,7 +418,7 @@ void givenApiResponseIsBadRequestWhenPostingStatementThenBadRequestIsThrown() {
418418
.definition(d -> d.addName(Locale.ENGLISH, "Simple Statement")))));
419419

420420
// Then BadRequest Is Thrown
421-
assertThrows(BadRequest.class, () -> response.block());
421+
assertThrows(BadRequest.class, response::block);
422422

423423
}
424424

@@ -438,7 +438,7 @@ void givenApiResponseIsInternalServerErrorWhenPostingStatementThenInternalServer
438438
.definition(d -> d.addName(Locale.ENGLISH, "Simple Statement")))));
439439

440440
// Then InternalServerError Is Thrown
441-
assertThrows(InternalServerError.class, () -> response.block());
441+
assertThrows(InternalServerError.class, response::block);
442442

443443
}
444444

@@ -512,14 +512,14 @@ void whenGettingVoidedStatementWithAttachmentsThenPathIsExpected() throws Interr
512512
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
513513

514514
// When Getting Voided Statement With Attachments
515-
client.getStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6").attachments(true))
515+
client.getVoidedStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6").attachments(true))
516516
.block();
517517

518518
final var recordedRequest = mockWebServer.takeRequest();
519519

520520
// Then Path Is Expected
521521
assertThat(recordedRequest.getPath(),
522-
is("/statements?statementId=4df42866-40e7-45b6-bf7c-8d5fccbdccd6&attachments=true"));
522+
is("/statements?voidedStatementId=4df42866-40e7-45b6-bf7c-8d5fccbdccd6&attachments=true"));
523523
}
524524

525525
@Test
@@ -530,15 +530,15 @@ void whenGettingVoidedStatementWithCanonicalFormatThenPathIsExpected()
530530

531531
// When Getting Voided Statement With Canonical Format
532532
client
533-
.getStatement(
533+
.getVoidedStatement(
534534
r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6").format(StatementFormat.CANONICAL))
535535
.block();
536536

537537
final var recordedRequest = mockWebServer.takeRequest();
538538

539539
// Then Path Is Expected
540540
assertThat(recordedRequest.getPath(),
541-
is("/statements?statementId=4df42866-40e7-45b6-bf7c-8d5fccbdccd6&format=canonical"));
541+
is("/statements?voidedStatementId=4df42866-40e7-45b6-bf7c-8d5fccbdccd6&format=canonical"));
542542
}
543543

544544
// Get Statements
@@ -791,8 +791,7 @@ void whenGettingASingleStateWithoutRegistrationThenPathIsExpected() throws Inter
791791
}
792792

793793
@Test
794-
void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsInstanceOfString()
795-
throws InterruptedException {
794+
void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsInstanceOfString() {
796795

797796
// Given State Content Type Is Text Plain
798797
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK").setBody("Hello World!")
@@ -814,8 +813,7 @@ void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsInstanceOfString(
814813
}
815814

816815
@Test
817-
void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsExpected()
818-
throws InterruptedException {
816+
void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsExpected() {
819817

820818
// Given State Content Type Is Text Plain
821819
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK").setBody("Hello World!")
@@ -1304,8 +1302,7 @@ void whenGettingMultipleStatesWithoutRegistrationThenPathIsExpected()
13041302
}
13051303

13061304
@Test
1307-
void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsInstanceOfStringArray()
1308-
throws InterruptedException {
1305+
void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsInstanceOfStringArray() {
13091306

13101307
// Given Multiple States Exist
13111308
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")
@@ -1326,8 +1323,7 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsInstanceOfString
13261323
}
13271324

13281325
@Test
1329-
void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsExpected()
1330-
throws InterruptedException {
1326+
void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsExpected() {
13311327

13321328
// Given Multiple States Exist
13331329
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")
@@ -2002,8 +1998,7 @@ void whenGettingASingleActivityProfileThenPathIsExpected() throws InterruptedExc
20021998
}
20031999

20042000
@Test
2005-
void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBodyIsInstanceOfString()
2006-
throws InterruptedException {
2001+
void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBodyIsInstanceOfString() {
20072002

20082003
// Given ActivityProfile Content Type Is Text Plain
20092004
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK").setBody("Hello World!")
@@ -2022,8 +2017,7 @@ void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBod
20222017
}
20232018

20242019
@Test
2025-
void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBodyIsExpected()
2026-
throws InterruptedException {
2020+
void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBodyIsExpected() {
20272021

20282022
// Given ActivityProfile Content Type Is Text Plain
20292023
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK").setBody("Hello World!")
@@ -2363,7 +2357,7 @@ void givenApiResponseIsEmptyWhenGettingStatementIteratorThenMissingResponseBodyE
23632357
final var response = client.getStatementIterator();
23642358

23652359
// Then MissingResponseBodyException Is Thrown
2366-
assertThrows(MissingResponseBodyException.class, () -> response.block());
2360+
assertThrows(MissingResponseBodyException.class, response::block);
23672361

23682362
}
23692363

@@ -2490,8 +2484,7 @@ void whenGettingStatementIteratorAndProcessingItAsStreamThenRequestsAreExpected(
24902484
}
24912485

24922486
@Test
2493-
void givenEmptyStatementResultWhenGettingStatementIteratorThenHasNextIsFalse()
2494-
throws InterruptedException {
2487+
void givenEmptyStatementResultWhenGettingStatementIteratorThenHasNextIsFalse() {
24952488

24962489
// Given Empty StatementResult
24972490
final var body = """
@@ -2514,8 +2507,7 @@ void givenEmptyStatementResultWhenGettingStatementIteratorThenHasNextIsFalse()
25142507
}
25152508

25162509
@Test
2517-
void givenEmptyResponseWhenGettingStatementIteratorThenHasNextIsFalse()
2518-
throws InterruptedException {
2510+
void givenEmptyResponseWhenGettingStatementIteratorThenHasNextIsFalse() {
25192511

25202512
// Given Empty Response
25212513
// This response is technically invalid by the xAPI specification, but we cannot assume
@@ -2535,8 +2527,7 @@ void givenEmptyResponseWhenGettingStatementIteratorThenHasNextIsFalse()
25352527
}
25362528

25372529
@Test
2538-
void givenEmptyResponseWhenGettingStatementIteratorThenNextThrowsAnException()
2539-
throws InterruptedException {
2530+
void givenEmptyResponseWhenGettingStatementIteratorThenNextThrowsAnException() {
25402531

25412532
// Given Empty Response
25422533
final var body = """
@@ -2554,7 +2545,7 @@ void givenEmptyResponseWhenGettingStatementIteratorThenNextThrowsAnException()
25542545
final var iterator = client.getStatementIterator().block();
25552546

25562547
// Then Next Throws An Exception
2557-
assertThrows(NoSuchElementException.class, () -> iterator.next());
2548+
assertThrows(NoSuchElementException.class, iterator::next);
25582549

25592550
}
25602551

xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationAuthorizationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void accept(Builder builder) {
4747
try {
4848
mockWebServer.start();
4949
} catch (final IOException e) {
50+
// Ignore - test will fail if server doesn't start
5051
}
5152
builder.baseUrl(mockWebServer.url("").toString());
5253
}

xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationUsernamePasswordTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void accept(Builder builder) {
4747
try {
4848
mockWebServer.start();
4949
} catch (final IOException e) {
50+
// Ignore - test will fail if server doesn't start
5051
}
5152
builder.baseUrl(mockWebServer.url("").toString());
5253

xapi-model/src/main/java/dev/learning/xapi/model/Statement.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import dev.learning.xapi.model.validation.constraints.ValidStatementVerb;
1616
import dev.learning.xapi.model.validation.constraints.Variant;
1717
import io.jsonwebtoken.Jwts;
18-
import io.jsonwebtoken.SignatureAlgorithm;
1918
import io.jsonwebtoken.lang.UnknownClassException;
2019
import jakarta.validation.Valid;
2120
import jakarta.validation.constraints.NotNull;
@@ -162,8 +161,8 @@ public Statement signAndBuild(PrivateKey privateKey) {
162161
claims.put("context", this.context);
163162

164163
try {
165-
final var token = Jwts.builder().setClaims(claims)
166-
.signWith(privateKey, SignatureAlgorithm.RS512).compact();
164+
final var token = Jwts.builder().claims(claims)
165+
.signWith(privateKey, Jwts.SIG.RS512).compact();
167166

168167
addAttachment(a -> a.usageType(URI.create("http://adlnet.gov/expapi/attachments/signature"))
169168

xapi-model/src/test/java/dev/learning/xapi/model/AttachmentTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import jakarta.validation.ConstraintViolation;
1616
import jakarta.validation.Validation;
1717
import jakarta.validation.Validator;
18-
import java.io.FileNotFoundException;
1918
import java.io.IOException;
2019
import java.net.URI;
2120
import java.nio.charset.StandardCharsets;
@@ -247,8 +246,7 @@ void givenAttachmentWithStringDataWhenGettingSHA2ThenResultIsExpected() {
247246
}
248247

249248
@Test
250-
void givenAttachmentWithBinaryDataWhenGettingSHA2ThenResultIsExpected()
251-
throws FileNotFoundException, IOException {
249+
void givenAttachmentWithBinaryDataWhenGettingSHA2ThenResultIsExpected() throws IOException {
252250

253251
final var data =
254252
Files.readAllBytes(ResourceUtils.getFile("classpath:attachment/example.jpg").toPath());

xapi-model/src/test/java/dev/learning/xapi/model/ContextActivitiesTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ void whenBuildingContextActivitiesWithTwoCategoriesThenCategoryIshasSizeTwo() {
202202
}
203203

204204
@Test
205-
void whenBuildingContextActivitiesWithTwoParentsWithSameIdThenParentIshasSizeTwo()
206-
throws IOException {
205+
void whenBuildingContextActivitiesWithTwoParentsWithSameIdThenParentIshasSizeTwo() {
207206

208207
// When Building ContextActivities With Two Parents With Same Id
209208
final var contextActivities = ContextActivities.builder()

xapi-model/src/test/java/dev/learning/xapi/model/GroupTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ void givenGroupWithNameAndNoMembersWhenCallingIsAnonymousThenResultIsFalse() {
151151
}
152152

153153
@Test
154-
void givenGroupWithNameAndNullMembersWhenCallingIsAnonymousThenResultIsFalse()
155-
throws IOException {
154+
void givenGroupWithNameAndNullMembersWhenCallingIsAnonymousThenResultIsFalse() {
156155

157156
// Given Group With Name And Null Members
158157
final Group group = Group.builder()
@@ -194,8 +193,7 @@ void givenGroupWithMboxAndMembersWhenCallingIsAnonymousThenResultIsFalse() {
194193
}
195194

196195
@Test
197-
void givenGroupWithMboxSha1sumAndMembersWhenCallingIsAnonymousThenResultIsFalse()
198-
throws IOException {
196+
void givenGroupWithMboxSha1sumAndMembersWhenCallingIsAnonymousThenResultIsFalse() {
199197

200198
// Given Group With MboxSha1sum And Members
201199
final Group group = Group.builder()

0 commit comments

Comments
 (0)