Skip to content

Commit e0cb773

Browse files
authored
Use Lists instead of arrays in the xapi-model (#82)
1 parent f7ed67a commit e0cb773

File tree

22 files changed

+171
-171
lines changed

22 files changed

+171
-171
lines changed

samples/get-activity-profiles/src/main/java/dev/learning/xapi/samples/getactivityprofiles/GetActivityProfilesApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import dev.learning.xapi.client.XapiClient;
88
import dev.learning.xapi.samples.core.ExampleState;
99
import java.time.Instant;
10-
import java.util.Arrays;
10+
import java.util.List;
1111
import org.springframework.boot.CommandLineRunner;
1212
import org.springframework.boot.SpringApplication;
1313
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -52,13 +52,13 @@ public void run(String... args) throws Exception {
5252
postActivityProfile();
5353

5454
// Get Activity Profiles
55-
ResponseEntity<String[]> response =
55+
ResponseEntity<List<String>> response =
5656
client.getActivityProfiles(r -> r.activityId("https://example.com/activity/1"))
5757

5858
.block();
5959

6060
// Print the each returned activity profile id to the console
61-
Arrays.asList(response.getBody()).forEach(id -> System.out.println(id));
61+
response.getBody().stream().forEach(id -> System.out.println(id));
6262
}
6363

6464
private void postActivityProfile() {

samples/get-agent-profiles/src/main/java/dev/learning/xapi/samples/getagentprofiles/GetAgentProfilesApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package dev.learning.xapi.samples.getagentprofiles;
66

77
import dev.learning.xapi.client.XapiClient;
8-
import java.util.Arrays;
8+
import java.util.List;
99
import org.springframework.boot.CommandLineRunner;
1010
import org.springframework.boot.SpringApplication;
1111
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -47,13 +47,13 @@ public static void main(String[] args) {
4747
public void run(String... args) throws Exception {
4848

4949
// Get Profiles
50-
ResponseEntity<String[]> response = client
50+
ResponseEntity<List<String>> response = client
5151
.getAgentProfiles(r -> r.agent(a -> a.name("A N Other").mbox("mailto:another@example.com")))
5252

5353
.block();
5454

5555
// Print the each returned profile id to the console
56-
Arrays.asList(response.getBody()).forEach(id -> System.out.println(id));
56+
response.getBody().stream().forEach(id -> System.out.println(id));
5757

5858
}
5959

samples/get-states/src/main/java/dev/learning/xapi/samples/getstates/GetStatesApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import dev.learning.xapi.client.XapiClient;
88
import dev.learning.xapi.samples.core.ExampleState;
99
import java.time.Instant;
10-
import java.util.Arrays;
10+
import java.util.List;
1111
import org.springframework.boot.CommandLineRunner;
1212
import org.springframework.boot.SpringApplication;
1313
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -53,7 +53,7 @@ public void run(String... args) throws Exception {
5353
postState();
5454

5555
// Get States
56-
ResponseEntity<String[]> response = client
56+
ResponseEntity<List<String>> response = client
5757
.getStates(r -> r.activityId("https://example.com/activity/1")
5858

5959
.agent(a -> a.name("A N Other").mbox("mailto:another@example.com"))
@@ -63,7 +63,7 @@ public void run(String... args) throws Exception {
6363
.block();
6464

6565
// Print the each returned state id to the console
66-
Arrays.asList(response.getBody()).forEach(id -> System.out.println(id));
66+
response.getBody().stream().forEach(id -> System.out.println(id));
6767

6868
}
6969

xapi-client/src/main/java/dev/learning/xapi/client/PostStatementsRequest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package dev.learning.xapi.client;
66

77
import dev.learning.xapi.model.Statement;
8+
import java.util.Arrays;
9+
import java.util.List;
810
import java.util.Map;
911
import lombok.Builder;
1012
import lombok.Getter;
@@ -24,7 +26,7 @@
2426
@Getter
2527
public class PostStatementsRequest implements Request {
2628

27-
private final Statement[] statements;
29+
private final List<Statement> statements;
2830

2931
@Override
3032
public HttpMethod getMethod() {
@@ -54,10 +56,24 @@ public static class Builder {
5456
*
5557
* @see PostStatementsRequest#statements
5658
*/
57-
public Builder statements(Statement... statements) {
59+
public Builder statements(List<Statement> statements) {
5860
this.statements = statements;
5961
return this;
6062
}
63+
64+
/**
65+
* Sets the statements.
66+
*
67+
* @param statements The statements of the PostStatementsRequest.
68+
*
69+
* @return This builder
70+
*
71+
* @see PostStatementsRequest#statements
72+
*/
73+
public Builder statements(Statement... statements) {
74+
this.statements = Arrays.asList(statements);
75+
return this;
76+
}
6177

6278
}
6379

xapi-client/src/main/java/dev/learning/xapi/client/XapiClient.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import dev.learning.xapi.model.Statement;
1111
import dev.learning.xapi.model.StatementResult;
1212
import java.util.HashMap;
13+
import java.util.List;
1314
import java.util.Map;
1415
import java.util.UUID;
1516
import java.util.function.Consumer;
17+
import org.springframework.core.ParameterizedTypeReference;
1618
import org.springframework.http.ResponseEntity;
1719
import org.springframework.web.reactive.function.client.WebClient;
1820
import reactor.core.publisher.Mono;
@@ -32,6 +34,14 @@ public class XapiClient {
3234

3335
private final WebClient webClient;
3436

37+
private static final ParameterizedTypeReference<
38+
List<UUID>> LIST_UUID_TYPE = new ParameterizedTypeReference<>() {
39+
};
40+
41+
private static final ParameterizedTypeReference<
42+
List<String>> LIST_STRING_TYPE = new ParameterizedTypeReference<>() {
43+
};
44+
3545
/**
3646
* Default constructor for XapiClient.
3747
*
@@ -116,9 +126,9 @@ public Mono<ResponseEntity<UUID>> postStatement(PostStatementRequest request) {
116126

117127
.retrieve()
118128

119-
.toEntity(UUID[].class)
129+
.toEntity(LIST_UUID_TYPE)
120130

121-
.map(i -> ResponseEntity.ok().headers(i.getHeaders()).body(i.getBody()[0]));
131+
.map(i -> ResponseEntity.ok().headers(i.getHeaders()).body(i.getBody().get(0)));
122132

123133
}
124134

@@ -151,7 +161,7 @@ public Mono<ResponseEntity<UUID>> postStatement(Consumer<PostStatementRequest.Bu
151161
*
152162
* @return the ResponseEntity
153163
*/
154-
public Mono<ResponseEntity<UUID[]>> postStatements(PostStatementsRequest request) {
164+
public Mono<ResponseEntity<List<UUID>>> postStatements(PostStatementsRequest request) {
155165

156166
Map<String, Object> queryParams = new HashMap<>();
157167

@@ -165,7 +175,7 @@ public Mono<ResponseEntity<UUID[]>> postStatements(PostStatementsRequest request
165175

166176
.retrieve()
167177

168-
.toEntity(UUID[].class);
178+
.toEntity(LIST_UUID_TYPE);
169179

170180
}
171181

@@ -179,7 +189,7 @@ public Mono<ResponseEntity<UUID[]>> postStatements(PostStatementsRequest request
179189
*
180190
* @return the ResponseEntity
181191
*/
182-
public Mono<ResponseEntity<UUID[]>> postStatements(
192+
public Mono<ResponseEntity<List<UUID>>> postStatements(
183193
Consumer<PostStatementsRequest.Builder> request) {
184194

185195
final PostStatementsRequest.Builder builder = PostStatementsRequest.builder();
@@ -572,7 +582,7 @@ public Mono<ResponseEntity<Void>> deleteState(
572582
*
573583
* @return the ResponseEntity
574584
*/
575-
public Mono<ResponseEntity<String[]>> getStates(GetStatesRequest request) {
585+
public Mono<ResponseEntity<List<String>>> getStates(GetStatesRequest request) {
576586

577587
Map<String, Object> queryParams = new HashMap<>();
578588

@@ -584,7 +594,7 @@ public Mono<ResponseEntity<String[]>> getStates(GetStatesRequest request) {
584594

585595
.retrieve()
586596

587-
.toEntity(String[].class);
597+
.toEntity(LIST_STRING_TYPE);
588598

589599
}
590600

@@ -600,7 +610,7 @@ public Mono<ResponseEntity<String[]>> getStates(GetStatesRequest request) {
600610
*
601611
* @return the ResponseEntity
602612
*/
603-
public Mono<ResponseEntity<String[]>> getStates(
613+
public Mono<ResponseEntity<List<String>>> getStates(
604614
Consumer<GetStatesRequest.Builder<?, ?>> request) {
605615

606616
final GetStatesRequest.Builder<?, ?> builder = GetStatesRequest.builder();
@@ -964,7 +974,7 @@ public Mono<ResponseEntity<Void>> postAgentProfile(
964974
*
965975
* @return the ResponseEntity
966976
*/
967-
public Mono<ResponseEntity<String[]>> getAgentProfiles(GetAgentProfilesRequest request) {
977+
public Mono<ResponseEntity<List<String>>> getAgentProfiles(GetAgentProfilesRequest request) {
968978

969979
Map<String, Object> queryParams = new HashMap<>();
970980

@@ -976,7 +986,7 @@ public Mono<ResponseEntity<String[]>> getAgentProfiles(GetAgentProfilesRequest r
976986

977987
.retrieve()
978988

979-
.toEntity(String[].class);
989+
.toEntity(LIST_STRING_TYPE);
980990

981991
}
982992

@@ -989,7 +999,7 @@ public Mono<ResponseEntity<String[]>> getAgentProfiles(GetAgentProfilesRequest r
989999
*
9901000
* @return the ResponseEntity
9911001
*/
992-
public Mono<ResponseEntity<String[]>> getAgentProfiles(
1002+
public Mono<ResponseEntity<List<String>>> getAgentProfiles(
9931003
Consumer<GetAgentProfilesRequest.Builder> request) {
9941004

9951005
final GetAgentProfilesRequest.Builder builder = GetAgentProfilesRequest.builder();
@@ -1220,8 +1230,9 @@ public Mono<ResponseEntity<Void>> deleteActivityProfile(
12201230
* @param request The parameters of the get activity profiles request
12211231
*
12221232
* @return the ResponseEntity
1223-
*/
1224-
public Mono<ResponseEntity<String[]>> getActivityProfiles(GetActivityProfilesRequest request) {
1233+
*/
1234+
public Mono<ResponseEntity<List<String>>> getActivityProfiles(
1235+
GetActivityProfilesRequest request) {
12251236

12261237
Map<String, Object> queryParams = new HashMap<>();
12271238

@@ -1233,7 +1244,7 @@ public Mono<ResponseEntity<String[]>> getActivityProfiles(GetActivityProfilesReq
12331244

12341245
.retrieve()
12351246

1236-
.toEntity(String[].class);
1247+
.toEntity(LIST_STRING_TYPE);
12371248

12381249
}
12391250

@@ -1250,7 +1261,7 @@ public Mono<ResponseEntity<String[]>> getActivityProfiles(GetActivityProfilesReq
12501261
*
12511262
* @return the ResponseEntity
12521263
*/
1253-
public Mono<ResponseEntity<String[]>> getActivityProfiles(
1264+
public Mono<ResponseEntity<List<String>>> getActivityProfiles(
12541265
Consumer<GetActivityProfilesRequest.Builder> request) {
12551266

12561267
final GetActivityProfilesRequest.Builder builder = GetActivityProfilesRequest.builder();

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import dev.learning.xapi.model.Verb;
1616
import java.net.URI;
1717
import java.time.Instant;
18+
import java.util.Arrays;
19+
import java.util.List;
1820
import java.util.Locale;
1921
import java.util.UUID;
2022
import lombok.Getter;
@@ -164,7 +166,7 @@ void whenPostingStatementsThenMethodIsPost() throws InterruptedException {
164166

165167
final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build();
166168

167-
final Statement statements[] = { attemptedStatement, passedStatement };
169+
final List<Statement> statements = Arrays.asList(attemptedStatement, passedStatement);
168170

169171
// When posting Statements
170172
client.postStatements(r -> r.statements(statements)).block();
@@ -193,7 +195,7 @@ void whenPostingStatementsThenBodyIsExpected() throws InterruptedException {
193195

194196
final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build();
195197

196-
final Statement statements[] = { attemptedStatement, passedStatement };
198+
final List<Statement> statements = Arrays.asList(attemptedStatement, passedStatement);
197199

198200
// When Posting Statements
199201
client.postStatements(r -> r.statements(statements)).block();
@@ -223,7 +225,7 @@ void whenPostingStatementsThenContentTypeHeaderIsApplicationJson() throws Interr
223225

224226
final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build();
225227

226-
final Statement statements[] = { attemptedStatement, passedStatement };
228+
final List<Statement> statements = Arrays.asList(attemptedStatement, passedStatement);
227229

228230
// When Posting Statements
229231
client.postStatements(r -> r.statements(statements)).block();
@@ -255,14 +257,14 @@ void whenPostingStatementsThenResponseBodyIsInstanceOfUUIDArray() throws Interru
255257

256258
final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build();
257259

258-
final Statement statements[] = { attemptedStatement, passedStatement };
260+
final List<Statement> statements = Arrays.asList(attemptedStatement, passedStatement);
259261

260262
// When Posting Statements
261263
final ResponseEntity<
262-
UUID[]> response = client.postStatements(r -> r.statements(statements)).block();
264+
List<UUID>> response = client.postStatements(r -> r.statements(statements)).block();
263265

264266
// Then Response Body Is Instance Of UUID Array
265-
assertThat(response.getBody(), instanceOf(UUID[].class));
267+
assertThat(response.getBody(), instanceOf(List.class));
266268
}
267269

268270
// Posting a Statement
@@ -1178,7 +1180,7 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsInstanceOfString
11781180
.addHeader("Content-Type", "application/json; charset=utf-8"));
11791181

11801182
// When Getting Multiple States
1181-
final ResponseEntity<String[]> response = client
1183+
final ResponseEntity<List<String>> response = client
11821184
.getStates(r -> r.activityId("https://example.com/activity/1")
11831185

11841186
.agent(a -> a.name("A N Other").mbox("mailto:another@example.com"))
@@ -1188,7 +1190,7 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsInstanceOfString
11881190
.block();
11891191

11901192
// Then Body Is Instance Of String Array
1191-
assertThat(response.getBody(), instanceOf(String[].class));
1193+
assertThat(response.getBody(), instanceOf(List.class));
11921194
}
11931195

11941196
@Test
@@ -1201,7 +1203,7 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsExpected()
12011203
.addHeader("Content-Type", "application/json; charset=utf-8"));
12021204

12031205
// When Getting Multiple States
1204-
final ResponseEntity<String[]> response = client
1206+
final ResponseEntity<List<String>> response = client
12051207
.getStates(r -> r.activityId("https://example.com/activity/1")
12061208

12071209
.agent(a -> a.name("A N Other").mbox("mailto:another@example.com"))
@@ -1211,7 +1213,7 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsExpected()
12111213
.block();
12121214

12131215
// Then Body Is Expected
1214-
assertThat(response.getBody(), is(new String[] { "State1", "State2", "State3" }));
1216+
assertThat(response.getBody(), is(Arrays.asList( "State1", "State2", "State3" )));
12151217
}
12161218

12171219
// Deleting Multiple States

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonInclude.Include;
99
import java.net.URI;
1010
import java.util.LinkedHashMap;
11+
import java.util.List;
1112
import lombok.Builder;
1213
import lombok.Value;
1314

@@ -26,7 +27,7 @@
2627
@JsonInclude(Include.NON_EMPTY)
2728
public class About {
2829

29-
private String[] version;
30+
private List<String> version;
3031

3132
private LinkedHashMap<URI, Object> extensions;
3233

0 commit comments

Comments
 (0)