Skip to content

Commit

Permalink
Validate timestamp parameters in sample xapi-server (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Selindek authored Jun 28, 2023
1 parent 846e6e2 commit c883ef4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* /* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.samples.xapiserver;

import dev.learning.xapi.jackson.model.strict.XapiTimestamp;
import java.time.Instant;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
* <p>
* InstantConverter class.
* </p>
*
* @author István Rátkai (Selindek)
* @author Thomas Turrell-Croft
*/
@Component
public class InstantConverter implements Converter<String, Instant> {

/**
* Converts string to {@link java.time.Instant}. If the timezone is not specified in string, UTC
* will be used.
*
* @param source the String representation of the datetime in ISO 8601 format (e.q.
* '2011-12-03T10:15:30+01:00')
*
* @return {@link java.time.Instant} of source input
*/
@Override
public Instant convert(String source) {

return XapiTimestamp.parse(source);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.learning.xapi.model.StatementResult;
import dev.learning.xapi.model.validation.constraints.Statements;
import jakarta.validation.Valid;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -80,7 +81,24 @@ public ResponseEntity<Statement> getStatement(@RequestParam(required = true) UUI
* "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#213-get-statements">GET
* Statements</a>
*/
@GetMapping(params = {"!statementId, !voidedStatementId"})
@GetMapping(params = "since")
public ResponseEntity<Void> getStatementsSince(@RequestParam Instant since) {

log.debug("GET statements since");

return ResponseEntity.status(HttpStatus.NOT_IMPLEMENTED).build();
}

/**
* Get Statements.
*
* @return the ResponseEntity
*
* @see <a href=
* "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#213-get-statements">GET
* Statements</a>
*/
@GetMapping(params = {"!statementId, !voidedStatementId", "!since"})
public ResponseEntity<StatementResult> getStatements() {

log.debug("GET statements");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package dev.learning.xapi.samples.xapiserver;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -69,4 +70,26 @@ void whenPostingMultipleStatementsThenStatusIsOk() throws Exception {
.andExpect(status().isOk());
}

@Test
void whenGettingMultipleStatementsWithSinceParameterThenStatusIsNotImplemented()
throws Exception {

// When Getting Multiple Statements With Since Parameter
mvc.perform(get("/xapi/statements?since=2017-03-01T12:30:00.000+00"))

// Then Status Is Not Implemented
.andExpect(status().isNotImplemented());
}

@Test
void whenGettingMultipleStatementsWithNegativeTimezoneOffsetThenStatusIsBadRequest()
throws Exception {

// When Getting Multiple Statements With Negative Timezone Offset
mvc.perform(get("/xapi/statements?since=2017-03-01T12:30:00.000-00"))

// Then Status Is Bad Request
.andExpect(status().isBadRequest());
}

}

0 comments on commit c883ef4

Please sign in to comment.