Skip to content

Commit

Permalink
Use parameterized tests and add missing String to Object methods in b…
Browse files Browse the repository at this point in the history
…uilders (#70)
  • Loading branch information
Selindek authored Mar 8, 2023
1 parent 702b897 commit 76ee277
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Activity Object GET</a>
*
* @author Thomas Turrell-Croft
* @author István Rátkai (Selindek)
*/
@Getter
@Builder
Expand Down Expand Up @@ -47,6 +48,39 @@ public HttpMethod getMethod() {
*/
public static class Builder {

/**
* Sets the activityId.
*
* @param activityId The activityId of the GetActivityRequest.
*
* @return This builder
*
* @see GetActivityRequest#activityId
*/
public Builder activityId(String activityId) {

this.activityId = URI.create(activityId);

return this;

}

/**
* Sets the activityId.
*
* @param activityId The activityId of the GetActivityRequest.
*
* @return This builder
*
* @see GetActivityRequest#activityId
*/
public Builder activityId(URI activityId) {

this.activityId = activityId;

return this;

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Statements</a>
*
* @author Thomas Turrell-Croft
* @author István Rátkai (Selindek)
*/
@Builder
@Getter
Expand All @@ -46,6 +47,34 @@ public UriBuilder url(UriBuilder uriBuilder, Map<String, Object> queryParams) {
*/
public static class Builder {

/**
* Sets the more token.
*
* @param more The more token of the GetMoreStatementsRequest.
* @return This builder
*/
public Builder more(String more) {

this.more = URI.create(more);

return this;

}

/**
* Sets the more token.
*
* @param more The more token of the GetMoreStatementsRequest.
* @return This builder
*/
public Builder more(URI more) {

this.more = more;

return this;

}

// This static class extends the lombok builder.

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import org.springframework.web.util.UriBuilder;
Expand All @@ -18,6 +19,7 @@
* Statements</a>
*
* @author Thomas Turrell-Croft
* @author István Rátkai (Selindek)
*/
@SuperBuilder
@Getter
Expand All @@ -37,4 +39,38 @@ public UriBuilder url(UriBuilder uriBuilder, Map<String, Object> queryParams) {

}

/**
* Builder for GetVoidedStatementRequest.
*/
public abstract static class Builder<C extends GetVoidedStatementRequest, B extends Builder<C, B>>
extends GetStatementRequest.Builder<C, B> {

/**
* Sets the voidedId.
*
* @param voidedId The voidedId of the GetVoidedStatementRequest.
* @return This builder
* @see GetVoidedStatementRequest#voidedId
*/
public Builder<C, B> voidedId(UUID voidedId) {
id(voidedId);
return self();
}

/**
* Sets the voidedId.
*
* @param voidedId The voidedId of the GetVoidedStatementRequest.
* @return This builder
* @see GetVoidedStatementRequest#voidedId
*/
public Builder<C, B> voidedId(String voidedId) {
id(voidedId);
return self();
}

// This static class extends the lombok builder.

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
* GetMoreStatementsRequest Tests.
*
* @author Thomas Turrell-Croft
* @author István Rátkai (Selindek)
*/
@DisplayName("GetMoreStatementsRequest Tests")
class GetMoreStatementsRequestTests {

@Test
void whenBuildingGetMoreStatementsRequestWithAllParametersThenNoExceptionIsThrown() {
void whenBuildingGetMoreStatementsRequestWithUriParameterThenNoExceptionIsThrown() {

// When Building GetMoreStatementsRequest With All Parameters
// When Building GetMoreStatementsRequest With Uri Parameter
GetMoreStatementsRequest.Builder builder = GetMoreStatementsRequest.builder()

.more(
Expand All @@ -30,6 +31,19 @@ void whenBuildingGetMoreStatementsRequestWithAllParametersThenNoExceptionIsThrow
assertDoesNotThrow(() -> builder.build());

}

@Test
void whenBuildingGetMoreStatementsRequestWithStringParameterThenNoExceptionIsThrown() {

// When Building GetMoreStatementsRequest With String Parameter
GetMoreStatementsRequest.Builder builder = GetMoreStatementsRequest.builder()

.more("https://example.com/xapi/statements/869cc589-76fa-4283-8e96-eea86f9124e1");

// Then No Exception Is Thrown
assertDoesNotThrow(() -> builder.build());

}

@Test
void whenBuildingGetMoreStatementsRequestWithoutMoreThenNullPointerExceptionIsThrown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void whenGettingVoidedStatementThenMethodIsGet() throws InterruptedException {
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));

// When Getting Voided Statement
client.getVoidedStatement(r -> r.id(UUID.fromString("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")))
client.getVoidedStatement(r -> r.voidedId(UUID.fromString("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")))
.block();

RecordedRequest recordedRequest = mockWebServer.takeRequest();
Expand All @@ -357,7 +357,7 @@ void whenGettingVoidedStatementThenPathIsExpected() throws InterruptedException
mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));

// When Getting Voided Statement
client.getVoidedStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")).block();
client.getVoidedStatement(r -> r.voidedId("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")).block();

RecordedRequest recordedRequest = mockWebServer.takeRequest();

Expand Down Expand Up @@ -1664,11 +1664,11 @@ void whenGettingProfilesWithSinceParameterThenPathIsExpected() throws Interrupte
// Get Activity

@Test
void whenGettingActivityThenMethodIsGet() throws InterruptedException {
void whenGettingActivityByUriThenMethodIsGet() throws InterruptedException {

mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));

// When Getting Activity
// When Getting Activity By Uri
client
.getActivity(r -> r.activityId(URI.create("https://example.com/activity/simplestatement")))
.block();
Expand All @@ -1679,6 +1679,22 @@ void whenGettingActivityThenMethodIsGet() throws InterruptedException {
assertThat(recordedRequest.getMethod(), is("GET"));
}

@Test
void whenGettingActivityByStringThenMethodIsGet() throws InterruptedException {

mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));

// When Getting Activity By String
client
.getActivity(r -> r.activityId("https://example.com/activity/simplestatement"))
.block();

RecordedRequest recordedRequest = mockWebServer.takeRequest();

// Then Method Is Get
assertThat(recordedRequest.getMethod(), is("GET"));
}

@Test
void whenGettingActivityThenPathIsExpected() throws InterruptedException {

Expand Down
22 changes: 17 additions & 5 deletions xapi-model/src/main/java/dev/learning/xapi/model/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import lombok.Builder;
import lombok.Value;
import org.springframework.util.Assert;

/**
* This class represents the xAPI Score object.
Expand All @@ -24,12 +23,9 @@
@JsonInclude(Include.NON_EMPTY)
public class Score {

// TODO check that @DecimalMax and @DecimalMin apply to float
/**
* The score related to the experience as modified by scaling and/or normalization.
*/
@DecimalMax(value = "1.0")
@DecimalMin(value = "-1.0")
private Float scaled;

/**
Expand All @@ -55,6 +51,22 @@ public class Score {
*/
public static class Builder {

protected Float scaled;

/**
* Sets the scaled score.
*
* @param scaled The scaled score.
*
* @return This builder
*/
public Builder scaled(Float scaled) {
Assert.isTrue(scaled == null || (scaled >= -1.0 && scaled <= 1.0),
"Scaled score vaule must be between -1.0 and 1.0");
this.scaled = scaled;
return this;
}

// This static class extends the lombok builder.

}
Expand Down
37 changes: 8 additions & 29 deletions xapi-model/src/test/java/dev/learning/xapi/model/AgentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.net.URI;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.util.ResourceUtils;

/**
Expand All @@ -28,10 +30,13 @@ class AgentTests {

private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();

@Test
void whenDeserializingAgentThenResultIsInstanceOfAgent() throws IOException {
@ParameterizedTest
@ValueSource(strings = {"classpath:agent/agent.json",
"classpath:agent/agent_without_object_type.json",
"classpath:agent/agent_with_object_type.json"})
void whenDeserializingAgentThenResultIsInstanceOfAgent(String fileName) throws IOException {

final File file = ResourceUtils.getFile("classpath:agent/agent.json");
final File file = ResourceUtils.getFile(fileName);

// When Deserializing Agent
final Agent result = objectMapper.readValue(file, Agent.class);
Expand All @@ -41,32 +46,6 @@ void whenDeserializingAgentThenResultIsInstanceOfAgent() throws IOException {

}

@Test
void whenDeserializingAgentWithoutObjectTypeThenResultIsInstanceOfAgent() throws IOException {

final File file = ResourceUtils.getFile("classpath:agent/agent_without_object_type.json");

// When Deserializing Agent Without Object Type
final Agent result = objectMapper.readValue(file, Agent.class);

// Then Result Is Instance Of Agent
assertThat(result, instanceOf(Agent.class));

}

@Test
void whenDeserializingAgentWithObjectTypeThenResultIsInstanceOfAgent() throws IOException {

final File file = ResourceUtils.getFile("classpath:agent/agent_with_object_type.json");

// When Deserializing Agent Without Object Type
final Agent result = objectMapper.readValue(file, Agent.class);

// Then Result Is Instance Of Agent
assertThat(result, instanceOf(Agent.class));

}

@Test
void whenDeserializingAgentWithNameThenNameIsExpected() throws IOException {

Expand Down
Loading

0 comments on commit 76ee277

Please sign in to comment.