forked from WoopSicredi/jobs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WoopSicredi#6 - Criação do serviço de apuração
+ Criação dos testes unitários e de integração da apuração dos votos.
- Loading branch information
1 parent
71a57e4
commit 4f337ca
Showing
14 changed files
with
213 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/br/com/sicredi/votacaoapi/domains/pauta/dto/ResultadoVotacaoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package br.com.sicredi.votacaoapi.domains.pauta.dto; | ||
|
||
import br.com.sicredi.votacaoapi.domains.pauta.model.Pauta; | ||
import br.com.sicredi.votacaoapi.domains.pauta.model.validations.TipoDeResultadoDaVotacao; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ResultadoVotacaoResponse { | ||
|
||
private PautaDTO pauta; | ||
|
||
private TipoDeResultadoDaVotacao votingResult; | ||
|
||
public ResultadoVotacaoResponse(Pauta pauta, TipoDeResultadoDaVotacao votingResult) { | ||
this.pauta = PautaDTO.converterEmDTO(pauta); | ||
this.votingResult = votingResult; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...a/br/com/sicredi/votacaoapi/domains/pauta/model/validations/TipoDeResultadoDaVotacao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package br.com.sicredi.votacaoapi.domains.pauta.model.validations; | ||
|
||
public enum TipoDeResultadoDaVotacao { | ||
|
||
SIM, | ||
NAO, | ||
EMPATE, | ||
SEM_VOTOS; | ||
|
||
public static TipoDeResultadoDaVotacao deAcordoComAQuantidadeDeVotos(Integer quantidadeDeVotos) { | ||
if (quantidadeDeVotos == null) { | ||
return SEM_VOTOS; | ||
} else if (quantidadeDeVotos == 0) { | ||
return EMPATE; | ||
} else if (quantidadeDeVotos < 0) { | ||
return NAO; | ||
} | ||
return SIM; | ||
} | ||
|
||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...n/java/br/com/sicredi/votacaoapi/domains/pauta/service/ApuracaoDaVotacaoPautaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package br.com.sicredi.votacaoapi.domains.pauta.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import br.com.sicredi.votacaoapi.application.error.BusinessException; | ||
import br.com.sicredi.votacaoapi.application.error.MensagemValidacaoVotacaoEnum; | ||
import br.com.sicredi.votacaoapi.domains.pauta.dto.ResultadoVotacaoResponse; | ||
import br.com.sicredi.votacaoapi.domains.pauta.model.Pauta; | ||
import br.com.sicredi.votacaoapi.domains.pauta.model.validations.TipoDeResultadoDaVotacao; | ||
|
||
@Service | ||
public class ApuracaoDaVotacaoPautaService { | ||
|
||
private RecuperarPautaService recuperarPautaService; | ||
|
||
@Autowired | ||
public ApuracaoDaVotacaoPautaService(RecuperarPautaService recuperarPautaService) { | ||
this.recuperarPautaService = recuperarPautaService; | ||
} | ||
|
||
public ResultadoVotacaoResponse apurar(Long pautaId) { | ||
Pauta pauta = recuperarPautaService.retornarPautaPorId(pautaId); | ||
if (!pauta.temSessaoIniciada()) { | ||
throw new BusinessException(MensagemValidacaoVotacaoEnum.SESSAO_NAO_INICIOU); | ||
} else if (pauta.temSessaoAtiva()) { | ||
throw new BusinessException(MensagemValidacaoVotacaoEnum.SESSAO_ESTA_ATIVA); | ||
} | ||
|
||
TipoDeResultadoDaVotacao votingResult = TipoDeResultadoDaVotacao.deAcordoComAQuantidadeDeVotos( | ||
pauta.getVotos() | ||
.stream() | ||
.map(v -> v.getDecisao() ? 1 : -1) | ||
.reduce(0, (subtotal, element) -> subtotal + element) | ||
); | ||
|
||
return new ResultadoVotacaoResponse(pauta, votingResult); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
src/test/java/br/com/sicredi/votacaoapi/domains/pauta/service/GetPollCountServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package br.com.sicredi.votacaoapi.domains.pauta.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.BDDMockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import br.com.sicredi.votacaoapi.StringUtils; | ||
import br.com.sicredi.votacaoapi.application.error.BusinessException; | ||
import br.com.sicredi.votacaoapi.application.error.MensagemValidacaoVotacaoEnum; | ||
import br.com.sicredi.votacaoapi.application.error.RecursoNaoEncontradoException; | ||
import br.com.sicredi.votacaoapi.domains.pauta.dto.ResultadoVotacaoResponse; | ||
import br.com.sicredi.votacaoapi.domains.pauta.model.Pauta; | ||
import br.com.sicredi.votacaoapi.domains.pauta.model.validations.TipoDeResultadoDaVotacao; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
public class GetPollCountServiceTest { | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@MockBean | ||
private ApuracaoDaVotacaoPautaService apuracaoDaVotacaoPautaService; | ||
|
||
@Test | ||
public void apurarQuandoSessaoEstiverFinalizadaDeveriaRetornarCodigoDeStatus200Ok() { | ||
|
||
Pauta pauta = new Pauta(); | ||
pauta.setId(1L); | ||
pauta.setNome(StringUtils.spaces(100)); | ||
pauta.abrirSessao(null); | ||
|
||
ResultadoVotacaoResponse resultadoVotacaoResponse = new ResultadoVotacaoResponse(pauta, TipoDeResultadoDaVotacao.SIM); | ||
BDDMockito.when(apuracaoDaVotacaoPautaService.apurar(1L)).thenReturn(resultadoVotacaoResponse); | ||
|
||
ResponseEntity<String> response = restTemplate.getForEntity("/pauta/apuracao/1", String.class); | ||
assertThat(response.getStatusCodeValue()).isEqualTo(200); | ||
|
||
} | ||
|
||
@Test | ||
public void apurarQuandoPautaENulaDeveriaRetornarCodigoDeStatus400BadRequest() { | ||
ResponseEntity<String> response = restTemplate.getForEntity("/pauta/apuracao/A", String.class); | ||
assertThat(response.getStatusCodeValue()).isEqualTo(400); | ||
} | ||
|
||
@Test | ||
public void apurarQuandoCodigoDaPautaENegativaDeveriaRetornarCodigoDeStatus400BadRequest() { | ||
ResponseEntity<String> response = restTemplate.getForEntity("/pauta/apuracao/-1", String.class); | ||
assertThat(response.getStatusCodeValue()).isEqualTo(400); | ||
} | ||
|
||
@Test | ||
public void apurarQuandoPautaNaoExisteDeveriaRetornarCodigoDeStatus404ResourceNotFound() { | ||
BDDMockito.when(apuracaoDaVotacaoPautaService.apurar(1L)).thenThrow(new RecursoNaoEncontradoException(MensagemValidacaoVotacaoEnum.PAUTA_NAO_EXISTE)); | ||
ResponseEntity<String> response = restTemplate.getForEntity("/pauta/apuracao/1", String.class); | ||
assertThat(response.getStatusCodeValue()).isEqualTo(404); | ||
} | ||
|
||
@Test | ||
public void apurarQuandoSessaoNaoFoiIniciadaDeveriaRetornarCodigoDeStatus400BadRequest() { | ||
BDDMockito.when(apuracaoDaVotacaoPautaService.apurar(1L)).thenThrow(new BusinessException(MensagemValidacaoVotacaoEnum.SESSAO_NAO_INICIOU)); | ||
ResponseEntity<String> response = restTemplate.getForEntity("/pauta/apuracao/1", String.class); | ||
assertThat(response.getStatusCodeValue()).isEqualTo(400); | ||
} | ||
|
||
@Test | ||
public void apurarQuandoSessaoEstiverAtivaDeveriaRetornarCodigoDeStatus400BadRequest() { | ||
BDDMockito.when(apuracaoDaVotacaoPautaService.apurar(1L)).thenThrow(new BusinessException(MensagemValidacaoVotacaoEnum.SESSAO_ESTA_ATIVA)); | ||
ResponseEntity<String> response = restTemplate.getForEntity("/pauta/apuracao/1", String.class); | ||
assertThat(response.getStatusCodeValue()).isEqualTo(400); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters