Skip to content

Commit

Permalink
#284: some code polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-fra authored and benitsch committed Jun 3, 2024
1 parent 3709b73 commit 087ce3e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public ResponseEntity<ObservationDTO> updateObservation(Long studyId, Integer ob

@Override
@RequiresStudyRole({StudyRole.STUDY_ADMIN, StudyRole.STUDY_OPERATOR})
public ResponseEntity<EndpointTokenDTO> createToken(Long studyId, Integer observationId, String tokenLabel) {
if(tokenLabel.isBlank()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); }
public ResponseEntity<EndpointTokenDTO> createToken(Long studyId, Integer observationId, EndpointTokenDTO token) {
if(token.getTokenLabel().isBlank()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); }

Optional<EndpointToken> addedToken = integrationService.addToken(studyId, observationId, tokenLabel.replace("\"", ""));
Optional<EndpointToken> addedToken = integrationService.addToken(studyId, observationId, token.getTokenLabel());
if(addedToken.isEmpty()) {
throw new BadRequestException("Token with given label already exists for given observation");
}
Expand All @@ -100,17 +100,11 @@ public ResponseEntity<EndpointTokenDTO> createToken(Long studyId, Integer observ

@Override
@RequiresStudyRole({StudyRole.STUDY_ADMIN, StudyRole.STUDY_OPERATOR})
public ResponseEntity<EndpointTokenDTO> updateTokenLabel(Long studyId, Integer observationId, Integer tokenId, String tokenLabel) {
Optional<EndpointToken> token = integrationService.updateToken(studyId, observationId, tokenId, tokenLabel.replace("\"", ""));
public ResponseEntity<EndpointTokenDTO> updateTokenLabel(Long studyId, Integer observationId, Integer tokenId, EndpointTokenDTO endpointToken) {
Optional<EndpointToken> token = integrationService.updateToken(studyId, observationId, tokenId, endpointToken.getTokenLabel());

if(token.isEmpty()) {
throw new BadRequestException("Token with given id doesn't exist for given observation");
}

return ResponseEntity.ok().body(
EndpointTokenTransformer.toEndpointTokenDTO(
token.get()
)
return ResponseEntity.of(
token.map(EndpointTokenTransformer::toEndpointTokenDTO)
);
}

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

@Component
public class IntegrationRepository {
private static final String GET_TOKEN_BY_IDS = "SELECT * FROM observation_api_tokens WHERE study_id = ? AND observation_id = ? AND token_id = ?";
private static final String ADD_TOKEN =
"INSERT INTO observation_api_tokens(study_id, observation_id, token_id, token_label, token) " +
"VALUES (:study_id, :observation_id, (SELECT COALESCE(MAX(token_id),0)+1 FROM observation_api_tokens WHERE study_id = :study_id AND observation_id = :observation_id), :token_label, :token) " +
Expand All @@ -38,10 +37,12 @@ public class IntegrationRepository {
"DELETE FROM observation_api_tokens " +
"WHERE study_id = ? AND observation_id = ? AND token_id = ?";
private static final String DELETE_ALL = "DELETE FROM observation_api_tokens";
private static final String UPDATE_TOKEN =
"UPDATE observation_api_tokens " +
"SET token_label = :token_label " +
"WHERE study_id = :study_id AND observation_id = :observation_id AND token_id = :token_id";
private static final String UPDATE_TOKEN = """
UPDATE observation_api_tokens
SET token_label = :token_label
WHERE study_id = :study_id AND observation_id = :observation_id AND token_id = :token_id
RETURNING token_id, token_label, created
""";
private static final String DELETE_ALL_FOR_STUDY_ID =
"DELETE FROM observation_api_tokens " +
"WHERE study_id = ?";
Expand Down Expand Up @@ -93,23 +94,21 @@ public void deleteToken(Long studyId, Integer observationId, Integer tokenId) {

public Optional<EndpointToken> updateToken(Long studyId, Integer observationId, Integer tokenId, String tokenLabel) {
try {
namedTemplate.update(UPDATE_TOKEN,
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("observation_id", observationId)
.addValue("token_id", tokenId)
.addValue("token_label", tokenLabel)
);
return getById(studyId, observationId, tokenId);
return Optional.ofNullable(
namedTemplate.queryForObject(UPDATE_TOKEN,
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("observation_id", observationId)
.addValue("token_id", tokenId)
.addValue("token_label", tokenLabel),
getHiddenTokenRowMapper()
)
);
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}

public Optional<EndpointToken> getById(long studyId, int observationId, int tokenId) {
return Optional.ofNullable(template.queryForObject(GET_TOKEN_BY_IDS, getHiddenTokenRowMapper(), studyId, observationId, tokenId));
}

private static RowMapper<EndpointToken> getHiddenTokenRowMapper() {
return (rs, rowNum) -> new EndpointToken(
rs.getInt("token_id"),
Expand Down
10 changes: 4 additions & 6 deletions studymanager/src/main/resources/openapi/StudyManagerAPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/TokenLabel'
$ref: '#/components/schemas/EndpointToken'
responses:
'201':
description: Token successfully added
Expand Down Expand Up @@ -803,7 +803,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/TokenLabel'
$ref: '#/components/schemas/EndpointToken'
responses:
'200':
description: Token label successfully updated
Expand Down Expand Up @@ -1626,22 +1626,20 @@ components:
tokenId:
$ref: '#/components/schemas/Id'
tokenLabel:
$ref: '#/components/schemas/TokenLabel'
type: string
created:
type: string
format: date-time
readOnly: true
token:
type: string
readOnly: true
required:
- tokenId
- tokenLabel
- created
- token

TokenLabel:
type: string

ParticipationData:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package io.redlink.more.studymanager.controller.studymanager;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.redlink.more.studymanager.api.v1.model.EventDTO;
import io.redlink.more.studymanager.api.v1.model.EndpointTokenDTO;
import io.redlink.more.studymanager.api.v1.model.ObservationDTO;
import io.redlink.more.studymanager.api.v1.model.ObservationScheduleDTO;
import io.redlink.more.studymanager.model.*;
Expand All @@ -18,6 +18,7 @@
import io.redlink.more.studymanager.service.OAuth2AuthenticationService;
import io.redlink.more.studymanager.service.ObservationService;
import io.redlink.more.studymanager.utils.MapperUtils;
import java.time.OffsetDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -164,26 +165,26 @@ void testEmptySchedule() throws Exception {
@Test
@DisplayName("Add token should create and return token with id, label, timestamp and secret set, only if label is valid")
void testAddToken() throws Exception{
EndpointToken token = new EndpointToken(
EndpointTokenDTO token = new EndpointTokenDTO(
1,
"testLabel",
Instant.now(),
OffsetDateTime.now(),
"test");
when(integrationService.addToken(anyLong(), anyInt(), anyString()))
.thenAnswer(invocationOnMock -> Optional.of(new EndpointToken(
token.tokenId(),
token.getTokenId(),
invocationOnMock.getArgument(2),
token.created(),
token.token()
Instant.now(),
token.getToken()
)));
mvc.perform(post("/api/v1/studies/1/observations/1/tokens")
.content(token.tokenLabel())
.content(mapper.writeValueAsString(token))
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.tokenId").value(token.tokenId()))
.andExpect(jsonPath("$.tokenLabel").value(token.tokenLabel()))
.andExpect(jsonPath("$.token").value(token.token()))
.andExpect(jsonPath("$.tokenId").value(token.getTokenId()))
.andExpect(jsonPath("$.tokenLabel").value(token.getTokenLabel()))
.andExpect(jsonPath("$.token").value(token.getToken()))
.andExpect(jsonPath("$.created").exists());

mvc.perform(post("/api/v1/studies/1/observations/1/tokens")
Expand Down

0 comments on commit 087ce3e

Please sign in to comment.