Skip to content

Commit

Permalink
borrado challenge 7c8f6d12-8c2f-4b7f-9e94-6d5f5c3e02e3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuria-Fernandez committed Jul 23, 2024
1 parent 6d55d0e commit 1ef422f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public Mono<GenericResultDto<LanguageDto>> getAllLanguages() {
@ApiResponse(responseCode = "404", description = "The Challenge with given Id was not found.", content = {@Content(schema = @Schema())})
}
)
public Mono<GenericResultDto<SolutionDto>> getSolutions(@PathVariable("idChallenge") String
public Mono<GenericResultDto<ChallengeDto>> getSolutions(@PathVariable("idChallenge") String
idChallenge, @PathVariable("idLanguage") String idLanguage) {
return challengeService.getSolutions(idChallenge, idLanguage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public class ChallengeDto {
@JsonProperty(value = "id_challenge", index = 0)
private UUID challengeId;

@JsonProperty(value = "challenge_title", index = 1)
private Map<Locale, String> title;
@JsonProperty(value = "uuid_language", index = 1)
private UUID uuidLanguage;

@JsonProperty(index = 2)
private List<SolutionDto> solutions;

@JsonProperty(value = "challenge_title", index = 3)
private Map<Locale, String> title;

@JsonProperty(index = 4)
private String level;

/**
Expand All @@ -33,25 +39,25 @@ public class ChallengeDto {
* los datos de LocalDateTime a String
* al formato requerido en el .json
*/
@JsonProperty(value = "creation_date", index = 3)
@JsonProperty(value = "creation_date", index = 5)
private String creationDate;

@JsonProperty(value = "detail", index = 4)
@JsonProperty(value = "detail", index = 6)
private DetailDocument detail;

@JsonProperty(index = 5)
@JsonProperty(index = 7)
private Integer popularity;

@JsonProperty(index = 6)
@JsonProperty(index = 8)
private Float percentage;

@JsonProperty(index = 7)
@JsonProperty(index = 9)
private Set<LanguageDto> languages;

@JsonProperty(index = 8)
private List<UUID> solutions;

@JsonProperty(index = 9)
@JsonProperty(index = 11)
private List<TestingValueDto> testingValues;




}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.itachallenge.challenge.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.itachallenge.challenge.annotations.ValidGenericPattern;
import com.itachallenge.challenge.annotations.ValidUUID;
Expand All @@ -23,7 +24,7 @@ public class SolutionDto {
private static final String UUID_PATTERN = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
private static final String STRING_PATTERN = "^.{1,500}$"; //max 500 characters @Id

@JsonProperty(value = "id_solution", index = 0)
@JsonProperty(value = "uuid_solution", index = 0)
private UUID uuid;

//@ValidGenericPattern(pattern = STRING_PATTERN, message = "Solution text cannot be empty")
Expand All @@ -33,10 +34,12 @@ public class SolutionDto {

@ValidUUID(message = "Invalid UUID")
@JsonProperty(value = "uuid_language", index = 2)
@JsonIgnore
private UUID idLanguage;

@ValidUUID(message = "Invalid UUID")
@JsonProperty(value = "uuid_challenge", index = 3)
@JsonIgnore
private UUID idChallenge;

//constructor for testing with uuid, solutionText and idLanguage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ protected String convert(LocalDateTime creationDateFromDocument) {
if (dtoClass.isAssignableFrom(SolutionDto.class)) {
mapper.createTypeMap(SolutionDocument.class, SolutionDto.class)
.addMapping(SolutionDocument::getUuid, SolutionDto::setUuid)
.addMapping(SolutionDocument::getSolutionText, SolutionDto::setSolutionText)
.addMapping(SolutionDocument::getIdLanguage, SolutionDto::setIdLanguage)
.addMapping(SolutionDocument::getIdChallenge, SolutionDto::setIdChallenge);
.addMapping(SolutionDocument::getSolutionText, SolutionDto::setSolutionText);
}

if(dtoClass.isAssignableFrom(LanguageDto.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public Mono<GenericResultDto<ChallengeDto>> getAllChallenges(int offset, int lim
});
}

public Mono<GenericResultDto<SolutionDto>> getSolutions(String idChallenge, String idLanguage) {

public Mono<GenericResultDto<ChallengeDto>> getSolutions(String idChallenge, String idLanguage) {
Mono<UUID> challengeIdMono = validateUUID(idChallenge);
Mono<UUID> languageIdMono = validateUUID(idLanguage);

Expand All @@ -183,17 +184,23 @@ public Mono<GenericResultDto<SolutionDto>> getSolutions(String idChallenge, Stri

return challengeRepository.findByUuid(challengeId)
.switchIfEmpty(Mono.error(new ChallengeNotFoundException(String.format(CHALLENGE_NOT_FOUND_ERROR, challengeId))))
.flatMapMany(challenge -> {
// Una vez encontrado el desafío, realiza la consulta de agregación para obtener las soluciones
.flatMap(challenge -> {
return challengeRepository.aggregateChallengesWithSolutions(challengeId, languageId)
.flatMap(solution -> Mono.just(solutionConverter.convertDocumentToDto(solution, SolutionDto.class)));
})
.collectList()
.map(solutions -> {
// Una vez que se han convertido todas las soluciones a DTO, crea el resultado genérico
GenericResultDto<SolutionDto> resultDto = new GenericResultDto<>();
resultDto.setInfo(0, solutions.size(), solutions.size(), solutions.toArray(new SolutionDto[0]));
return resultDto;
.flatMap(solution -> Mono.just(solutionConverter.convertDocumentToDto(solution, SolutionDto.class)))
.collectList()
.map(solutions -> {
// Creamos el ChallengeDto y lo llenamos con los datos correspondientes
ChallengeDto challengeDto = new ChallengeDto();
challengeDto.setChallengeId(challengeId);
challengeDto.setUuidLanguage(languageId);
challengeDto.setSolutions(solutions);


// Creamos el GenericResultDto con la lista de ChallengeDto
GenericResultDto<ChallengeDto> resultDto = new GenericResultDto<>();
resultDto.setInfo(0, -1, solutions.size(), List.of(challengeDto).toArray(new ChallengeDto[0]));
return resultDto;
});
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.itachallenge.challenge.service;

import com.itachallenge.challenge.dto.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Optional;
Expand All @@ -13,7 +12,7 @@ public interface IChallengeService {
Mono<ChallengeDto> getChallengeById(String id);
Mono<String> removeResourcesByUuid(String id);
Mono<GenericResultDto<LanguageDto>> getAllLanguages();
Mono<GenericResultDto<SolutionDto>> getSolutions(String idChallenge, String idLanguage);
Mono<GenericResultDto<ChallengeDto>> getSolutions(String idChallenge, String idLanguage);
Mono<SolutionDto> addSolution(SolutionDto solutionDto);
Mono<GenericResultDto<ChallengeDto>> getAllChallenges(int offset, int limit);
Mono<GenericResultDto<ChallengeDto>> getChallengesByLanguageOrDifficulty(Optional<String> idLanguage, Optional<String> level, int offset, int limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void getAllLanguages_LanguageExist_LanguageReturned() {
verify(languageConverter).convertDocumentFluxToDtoFlux(any(), any());
}

@Test
/* @Test
void testGetSolutions() {
// Arrange
String challengeStringId = "e5f71456-62db-4323-a8d2-1d473d28a931";
Expand Down Expand Up @@ -307,7 +307,62 @@ void testGetSolutions() {
verify(challengeRepository).aggregateChallengesWithSolutions(challengeId, languageId);
verify(solutionConverter).convertDocumentToDto(solution1, SolutionDto.class);
verify(solutionConverter).convertDocumentToDto(solution2, SolutionDto.class);
}
}*/
@Test
void testGetSolutions() {
// Arrange
String challengeStringId = "e5f71456-62db-4323-a8d2-1d473d28a931";
String languageStringId = "b5f78901-28a1-49c7-98bd-1ee0a555c678";
UUID challengeId = UUID.fromString(challengeStringId);
UUID languageId = UUID.fromString(languageStringId);
UUID solutionId1 = UUID.fromString("c8a5440d-6466-463a-bccc-7fefbe9396e4");
UUID solutionId2 = UUID.fromString("0864463e-eb7c-4bb3-b8bc-766d71ab38b5");

// Create ChallengeDocument
ChallengeDocument challenge = new ChallengeDocument();
challenge.setUuid(challengeId);
challenge.setSolutions(Arrays.asList(solutionId1, solutionId2));

// Create SolutionDocuments
SolutionDocument solution1 = new SolutionDocument(solutionId1, "Solution 1", languageId, challengeId);
SolutionDocument solution2 = new SolutionDocument(solutionId2, "Solution 2", languageId, challengeId);

// Create SolutionDtos
SolutionDto solutionDto1 = new SolutionDto(solutionId1, "Solution 1", languageId);
SolutionDto solutionDto2 = new SolutionDto(solutionId2, "Solution 2", languageId);

// Create ChallengeDto
ChallengeDto expectedChallengeDto = ChallengeDto.builder()
.challengeId(challengeId)
.solutions(Arrays.asList(solutionDto1, solutionDto2))
.build();

// Setup mocks
when(challengeRepository.findByUuid(challengeId)).thenReturn(Mono.just(challenge));
when(challengeRepository.aggregateChallengesWithSolutions(challengeId, languageId))
.thenReturn(Flux.just(solution1, solution2));
when(solutionConverter.convertDocumentToDto(solution1, SolutionDto.class)).thenReturn(solutionDto1);
when(solutionConverter.convertDocumentToDto(solution2, SolutionDto.class)).thenReturn(solutionDto2);

// Act
Mono<GenericResultDto<ChallengeDto>> resultMono = challengeService.getSolutions(challengeStringId, languageStringId);

// Assert
StepVerifier.create(resultMono)
.expectNextMatches(resultDto -> {
assertThat(resultDto.getOffset()).isZero();
assertThat(resultDto.getLimit()).isEqualTo(1); // Assuming limit is 1 for simplification
assertThat(resultDto.getCount()).isEqualTo(1); // Assuming count is 1 for simplification
assertThat(resultDto.getResults()).containsExactly(expectedChallengeDto);
return true;
})
.verifyComplete();

verify(challengeRepository).findByUuid(challengeId);
verify(challengeRepository).aggregateChallengesWithSolutions(challengeId, languageId);
verify(solutionConverter).convertDocumentToDto(solution1, SolutionDto.class);
verify(solutionConverter).convertDocumentToDto(solution2, SolutionDto.class);
}


@Test
Expand Down

0 comments on commit 1ef422f

Please sign in to comment.