Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package br.gov.al.saude.srv.core.domain; |
| 2 | |||
| 3 | import static br.gov.al.saude.test.ConstantesTestHelper.CargoConstantes.DESCRICAO_CARGO_ASSISTENTE_ADMINISTRATIVO; |
||
| 4 | import static br.gov.al.saude.test.ConstantesTestHelper.CargoConstantes.QUANTIDADE_VAGAS_50; |
||
| 5 | import static br.gov.al.saude.test.ConstantesTestHelper.CargoConstantes.SEQUENCIAL_CARGO_13; |
||
| 6 | import static br.gov.al.saude.test.ConstantesTestHelper.GrauInstrucaoConstantes.SEQUENCIAL_GRAU_INSTRUCAO_6; |
||
| 7 | |||
| 8 | import java.util.ArrayList; |
||
| 9 | import java.util.List; |
||
| 10 | |||
| 11 | import org.jmock.Expectations; |
||
| 12 | import org.junit.Before; |
||
| 13 | import org.junit.Test; |
||
| 14 | |||
| 15 | import br.gov.al.saude.framework.core.consulta.restricao.Restricoes; |
||
| 16 | import br.gov.al.saude.framework.core.generic.GenericRepository; |
||
| 17 | import br.gov.al.saude.framework.core.generic.GenericService; |
||
| 18 | import br.gov.al.saude.framework.core.interfaces.Alterar; |
||
| 19 | import br.gov.al.saude.framework.core.interfaces.Cadastrar; |
||
| 20 | import br.gov.al.saude.framework.core.util.VerificadorUtil; |
||
| 21 | import br.gov.al.saude.framework.core.validador.Validador; |
||
| 22 | import br.gov.al.saude.srv.core.domain.cargo.CargoService; |
||
| 23 | import br.gov.al.saude.srv.core.domain.cargo.impl.CargoServiceImpl; |
||
| 24 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.cargo.CargoRepository; |
||
| 25 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.servidor.ServidorRepository; |
||
| 26 | import br.gov.al.saude.srv.model.Cargo; |
||
| 27 | import br.gov.al.saude.srv.model.dto.CargoDTO; |
||
| 28 | import br.gov.al.saude.srv.model.dto.ParametroConsultaCargoDTO; |
||
| 29 | import br.gov.al.saude.test.builder.CargoBuilder; |
||
| 30 | import br.gov.al.saude.test.unidade.generic.service.ServiceImplTest; |
||
| 31 | import br.gov.al.saude.test.util.CommandSemMensagem; |
||
| 32 | import br.gov.al.saude.test.util.VerificadorLancamentoException; |
||
| 33 | |||
| 34 | public class CargoServiceImplTest extends ServiceImplTest<Cargo> { |
||
| 35 | |||
| 36 | private CargoService cargoService; |
||
| 37 | private CargoRepository cargoRepositoryMock; |
||
| 38 | private ServidorRepository servidorRepositoryMock; |
||
| 39 | private Cargo cargo; |
||
| 40 | |||
| 41 | @Before |
||
| 42 | public void inicializarContexto() { |
||
| 43 | cargoRepositoryMock = getContexto().mock(CargoRepository.class); |
||
| 44 | validadorMock = getContexto().mock(Validador.class); |
||
| 45 | servidorRepositoryMock = getContexto().mock(ServidorRepository.class); |
||
| 46 | cargoService = new CargoServiceImpl(validadorMock, cargoRepositoryMock, servidorRepositoryMock); |
||
| 47 | cargo = new CargoBuilder().comDescricao(DESCRICAO_CARGO_ASSISTENTE_ADMINISTRATIVO) |
||
| 48 | .comGrauInstrucao(SEQUENCIAL_GRAU_INSTRUCAO_6) |
||
| 49 | .comQuantidadeDeVagas(QUANTIDADE_VAGAS_50) |
||
| 50 | .comAtivo("SIM") |
||
| 51 | .build(); |
||
| 52 | setEntidade(cargo); |
||
| 53 | } |
||
| 54 | |||
| 55 | @Override |
||
| 56 | protected GenericService<Cargo> getService() { |
||
| 57 | return cargoService; |
||
| 58 | } |
||
| 59 | |||
| 60 | @Override |
||
| 61 | protected GenericRepository<Cargo> getRepositoryMock() { |
||
| 62 | return cargoRepositoryMock; |
||
| 63 | } |
||
| 64 | |||
| 65 | @Override |
||
| 66 | protected void expectativasDeNegocioFuncionalidadeCadastrar() { |
||
| 67 | getContexto().checking(new Expectations(){{ |
||
| 68 | oneOf(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 69 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 70 | will(returnValue(new ArrayList<Cargo>())); |
||
| 71 | }}); |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | protected void expectativasDeNegocioFuncionalidadeAlterar() { |
||
| 76 | getContexto().checking(new Expectations(){{ |
||
| 77 | oneOf(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 78 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 79 | will(returnValue(new ArrayList<Cargo>())); |
||
| 80 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargo(getEntidade()))); |
||
| 81 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(getEntidade()))); |
||
| 82 | }}); |
||
| 83 | } |
||
| 84 | |||
| 85 | @Test |
||
| 86 | public void aoConsultarDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 87 | getContexto().checking(new Expectations(){{ |
||
| 88 | oneOf(cargoRepositoryMock).consultar(with(any(ParametroConsultaCargoDTO.class))); |
||
| 89 | will(returnValue(new ArrayList<Cargo>())); |
||
| 90 | }}); |
||
| 91 | |||
| 92 | cargoService.consultar(new ParametroConsultaCargoDTO()); |
||
| 93 | } |
||
| 94 | |||
| 95 | @Test |
||
| 96 | public void aoObterQuantideRegistrosConsultarCargosAtivosDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 97 | getContexto().checking(new Expectations(){{ |
||
| 98 | oneOf(cargoRepositoryMock).obterQuantideRegistrosConsultarCargosAtivos(with(any(ParametroConsultaCargoDTO.class))); |
||
| 99 | }}); |
||
| 100 | |||
| 101 | cargoService.obterQuantideRegistrosConsultarCargosAtivos(new ParametroConsultaCargoDTO()); |
||
| 102 | } |
||
| 103 | |||
| 104 | @Test |
||
| 105 | public void aoConsultarCargosAtivosDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 106 | getContexto().checking(new Expectations(){{ |
||
| 107 | oneOf(cargoRepositoryMock).consultarCargosAtivos(with(any(ParametroConsultaCargoDTO.class))); |
||
| 108 | will(returnValue(new ArrayList<CargoDTO>())); |
||
| 109 | }}); |
||
| 110 | |||
| 111 | cargoService.consultarCargosAtivos(new ParametroConsultaCargoDTO()); |
||
| 112 | } |
||
| 113 | |||
| 114 | @Test |
||
| 115 | public void aoObterQuantidadeRegistrosDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 116 | getContexto().checking(new Expectations(){{ |
||
| 117 | oneOf(cargoRepositoryMock).obterQuantidadeDeRegistros(with(any(ParametroConsultaCargoDTO.class))); |
||
| 118 | }}); |
||
| 119 | |||
| 120 | cargoService.obterQuantidadeDeRegistros(new ParametroConsultaCargoDTO()); |
||
| 121 | } |
||
| 122 | |||
| 123 | @Test |
||
| 124 | public void aoCadastrarDeveriaDelegarParaOhRepositorioConsultarPassandoRestricoes() throws Exception { |
||
| 125 | getContexto().checking(new Expectations(){{ |
||
| 126 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Cadastrar.class})); |
||
| 127 | oneOf(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 128 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 129 | will(returnValue(new ArrayList<Cargo>())); |
||
| 130 | ignoring(getRepositoryMock()).cadastrar(with(getEntidade())); |
||
| 131 | }}); |
||
| 132 | |||
| 133 | getService().cadastrar(getEntidade()); |
||
| 134 | } |
||
| 135 | |||
| 136 | @Test |
||
| 137 | public void aoCadastrarCargoJaCadastradoDeveriaLancarExcecao() throws Exception { |
||
| 138 | final List<Cargo> listaCargos = new ArrayList<Cargo>(); |
||
| 139 | listaCargos.add(new Cargo()); |
||
| 140 | |||
| 141 | getContexto().checking(new Expectations(){{ |
||
| 142 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Cadastrar.class})); |
||
| 143 | oneOf(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 144 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 145 | will(returnValue(listaCargos)); |
||
| 146 | never(getRepositoryMock()).cadastrar(with(getEntidade())); |
||
| 147 | }}); |
||
| 148 | |||
| 149 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 150 | @Override |
||
| 151 | public void execute() throws Exception { |
||
| 152 | cargoService.cadastrar(getEntidade()); |
||
| 153 | } |
||
| 154 | |||
| 155 | }, "Cargo já cadastrado."); |
||
| 156 | } |
||
| 157 | |||
| 158 | @Test |
||
| 159 | public void aoAlterarDeveriaDelegarParaOhRepositorioConsultarPassandoRestricoes() throws Exception { |
||
| 160 | getContexto().checking(new Expectations(){{ |
||
| 161 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Alterar.class})); |
||
| 162 | oneOf(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 163 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 164 | will(returnValue(new ArrayList<Cargo>())); |
||
| 165 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargo(getEntidade()))); |
||
| 166 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(getEntidade()))); |
||
| 167 | ignoring(getRepositoryMock()).alterar(with(getEntidade())); |
||
| 168 | }}); |
||
| 169 | |||
| 170 | getService().alterar(getEntidade()); |
||
| 171 | } |
||
| 172 | |||
| 173 | @Test |
||
| 174 | public void aoAlterarCargoParaJaCadastradoDeveriaLancarExcecao() throws Exception { |
||
| 175 | final List<Cargo> listaCargos = new ArrayList<Cargo>(); |
||
| 176 | listaCargos.add(new Cargo()); |
||
| 177 | getEntidade().setCodigo(new Integer(SEQUENCIAL_CARGO_13)); |
||
| 178 | |||
| 179 | getContexto().checking(new Expectations(){{ |
||
| 180 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Alterar.class})); |
||
| 181 | oneOf(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 182 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 183 | will(returnValue(listaCargos)); |
||
| 184 | never(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargo(getEntidade()))); |
||
| 185 | never(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(getEntidade()))); |
||
| 186 | never(getRepositoryMock()).alterar(with(getEntidade())); |
||
| 187 | }}); |
||
| 188 | |||
| 189 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 190 | @Override |
||
| 191 | public void execute() throws Exception { |
||
| 192 | cargoService.alterar(getEntidade()); |
||
| 193 | } |
||
| 194 | |||
| 195 | }, "Cargo já cadastrado."); |
||
| 196 | } |
||
| 197 | |||
| 198 | @Test |
||
| 199 | public void aoAlterarCargoInativoDeveriaDelegarParaOhServidorRepositorioConsultarDadoFuncionalPassandoRestricoes() throws Exception { |
||
| 200 | getEntidade().setAtivo(false); |
||
| 201 | |||
| 202 | getContexto().checking(new Expectations(){{ |
||
| 203 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Alterar.class})); |
||
| 204 | ignoring(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 205 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 206 | will(returnValue(new ArrayList<Cargo>())); |
||
| 207 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargo(getEntidade()))); |
||
| 208 | will(returnValue(false)); |
||
| 209 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(getEntidade()))); |
||
| 210 | will(returnValue(false)); |
||
| 211 | ignoring(getRepositoryMock()).alterar(with(getEntidade())); |
||
| 212 | }}); |
||
| 213 | |||
| 214 | getService().alterar(getEntidade()); |
||
| 215 | } |
||
| 216 | |||
| 217 | @Test |
||
| 218 | public void aoAlterarCargoInativoComDadoFuncionalComCargoVinculadoDeveriaLancarExcecao() throws Exception { |
||
| 219 | getEntidade().setAtivo(false); |
||
| 220 | |||
| 221 | getContexto().checking(new Expectations(){{ |
||
| 222 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Alterar.class})); |
||
| 223 | ignoring(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 224 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 225 | will(returnValue(new ArrayList<Cargo>())); |
||
| 226 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargo(getEntidade()))); |
||
| 227 | will(returnValue(true)); |
||
| 228 | never(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(getEntidade()))); |
||
| 229 | never(getRepositoryMock()).alterar(with(getEntidade())); |
||
| 230 | }}); |
||
| 231 | |||
| 232 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 233 | @Override |
||
| 234 | public void execute() throws Exception { |
||
| 235 | getService().alterar(getEntidade()); |
||
| 236 | } |
||
| 237 | |||
| 238 | }, "Não é possível inativar cargo que possua dado funcional de servidor vinculado."); |
||
| 239 | } |
||
| 240 | |||
| 241 | @Test |
||
| 242 | public void aoAlterarCargoInativoComDadoFuncionalComCargoCarreiraVinculadoDeveriaLancarExcecao() throws Exception { |
||
| 243 | getEntidade().setAtivo(false); |
||
| 244 | |||
| 245 | getContexto().checking(new Expectations(){{ |
||
| 246 | ignoring(getValidadorMock()).validar(with(getEntidade()), with(new Class<?>[]{Alterar.class})); |
||
| 247 | ignoring(cargoRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 248 | with(any(Integer.class)), with(any(Integer.class)), with(gerarRestricoesParaVerificacaoDeCargoJaCadastrado(getEntidade()))); |
||
| 249 | will(returnValue(new ArrayList<Cargo>())); |
||
| 250 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargo(getEntidade()))); |
||
| 251 | will(returnValue(false)); |
||
| 252 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(getEntidade()))); |
||
| 253 | will(returnValue(true)); |
||
| 254 | never(getRepositoryMock()).alterar(with(getEntidade())); |
||
| 255 | }}); |
||
| 256 | |||
| 257 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 258 | @Override |
||
| 259 | public void execute() throws Exception { |
||
| 260 | getService().alterar(getEntidade()); |
||
| 261 | } |
||
| 262 | |||
| 263 | }, "Não é possível inativar cargo que possua dado funcional de servidor vinculado."); |
||
| 264 | } |
||
| 265 | |||
| 266 | private List<Restricoes> gerarRestricoesParaVerificacaoDeCargoJaCadastrado(Cargo entidade) { |
||
| 267 | List<Restricoes> restricoes = new ArrayList<Restricoes>(); |
||
| 268 | adicionarRestricaoDiferenteDeCasoEntidadePossuaCodigo(entidade, restricoes); |
||
| 269 | adicionarRestricaoIgualParaDescricao(entidade, restricoes); |
||
| 270 | return restricoes; |
||
| 271 | } |
||
| 272 | |||
| 273 | private void adicionarRestricaoIgualParaDescricao(Cargo entidade, List<Restricoes> restricoes) { |
||
| 274 | restricoes.add(Restricoes.igualIgnorandoAcentuacao("descricao", entidade.getDescricao())); |
||
| 275 | } |
||
| 276 | |||
| 277 | private void adicionarRestricaoDiferenteDeCasoEntidadePossuaCodigo(Cargo entidade, List<Restricoes> restricoes) { |
||
| 278 | if (VerificadorUtil.naoEstaNulo(entidade.getCodigo())) { |
||
| 279 | restricoes.add(Restricoes.diferenteDe("codigo", entidade.getCodigo())); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | private List<Restricoes> gerarListraRestricoesConsultarDadoFuncionalPorCargo(Cargo entidade) { |
||
| 284 | List<Restricoes> restricoes = new ArrayList<Restricoes>(); |
||
| 285 | restricoes.add(Restricoes.igual("cargo", entidade)); |
||
| 286 | return restricoes; |
||
| 287 | } |
||
| 288 | |||
| 289 | private List<Restricoes> gerarListraRestricoesConsultarDadoFuncionalPorCargoCarreira(Cargo entidade) { |
||
| 290 | List<Restricoes> restricoes = new ArrayList<Restricoes>(); |
||
| 291 | restricoes.add(Restricoes.igual("cargoCarreira", entidade)); |
||
| 292 | return restricoes; |
||
| 293 | } |
||
| 294 | |||
| 295 | } |