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.OrgaoConstantes.DESCRICAO_ORGAO_SECRETARIA_ESTADO_SAUDE; |
||
| 4 | import static br.gov.al.saude.test.ConstantesTestHelper.OrgaoConstantes.SEQUENCIAL_ORGAO_SECRETARIA_ESTADO_SAUDE_20; |
||
| 5 | import static br.gov.al.saude.test.ConstantesTestHelper.UnidadeConstantes.DESCRICAO_UNIDADE_COORDENADORIA_SETORIAL_DA_GESTAO_DA_INFORMATICA; |
||
| 6 | import static br.gov.al.saude.test.ConstantesTestHelper.UnidadeConstantes.SEQUENCIAL_UNIDADE_30; |
||
| 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.generic.GenericRepository; |
||
| 16 | import br.gov.al.saude.framework.core.generic.GenericService; |
||
| 17 | import br.gov.al.saude.framework.core.interfaces.Alterar; |
||
| 18 | import br.gov.al.saude.framework.core.interfaces.Cadastrar; |
||
| 19 | import br.gov.al.saude.framework.core.validador.Validador; |
||
| 20 | import br.gov.al.saude.srv.core.domain.unidade.UnidadeService; |
||
| 21 | import br.gov.al.saude.srv.core.domain.unidade.impl.UnidadeServiceImpl; |
||
| 22 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.orgao.OrgaoRepository; |
||
| 23 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.servidor.ServidorRepository; |
||
| 24 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.unidade.UnidadeRepository; |
||
| 25 | import br.gov.al.saude.srv.model.Orgao; |
||
| 26 | import br.gov.al.saude.srv.model.ServidorDadoFuncional; |
||
| 27 | import br.gov.al.saude.srv.model.Unidade; |
||
| 28 | import br.gov.al.saude.test.builder.OrgaoBuilder; |
||
| 29 | import br.gov.al.saude.test.builder.UnidadeBuilder; |
||
| 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 UnidadeServiceImplTest extends ServiceImplTest<Unidade> { |
||
| 35 | |||
| 36 | private UnidadeService unidadeService; |
||
| 37 | private UnidadeRepository unidadeRepositoryMock; |
||
| 38 | private OrgaoRepository orgaoRepositoryMock; |
||
| 39 | private ServidorRepository servidorRepositoryMock; |
||
| 40 | private Unidade unidade; |
||
| 41 | private Orgao orgao; |
||
| 42 | |||
| 43 | @Before |
||
| 44 | public void inicializarContexto() { |
||
| 45 | unidadeRepositoryMock = getContexto().mock(UnidadeRepository.class); |
||
| 46 | orgaoRepositoryMock = getContexto().mock(OrgaoRepository.class); |
||
| 47 | validadorMock = getContexto().mock(Validador.class); |
||
| 48 | servidorRepositoryMock = getContexto().mock(ServidorRepository.class); |
||
| 49 | unidadeService = new UnidadeServiceImpl(validadorMock, unidadeRepositoryMock, orgaoRepositoryMock, servidorRepositoryMock); |
||
| 50 | |||
| 51 | orgao = new OrgaoBuilder().comCodigoOrgao(SEQUENCIAL_ORGAO_SECRETARIA_ESTADO_SAUDE_20) |
||
| 52 | .comDescricao(DESCRICAO_ORGAO_SECRETARIA_ESTADO_SAUDE) |
||
| 53 | .comSituacaoAtivo("SIM") |
||
| 54 | .build(); |
||
| 55 | |||
| 56 | unidade = new UnidadeBuilder().comOrgao(SEQUENCIAL_ORGAO_SECRETARIA_ESTADO_SAUDE_20) |
||
| 57 | .comDescricao(DESCRICAO_UNIDADE_COORDENADORIA_SETORIAL_DA_GESTAO_DA_INFORMATICA) |
||
| 58 | .comSigla(DESCRICAO_UNIDADE_COORDENADORIA_SETORIAL_DA_GESTAO_DA_INFORMATICA) |
||
| 59 | .comSituacaoAtivo("NÃO") |
||
| 60 | .build(); |
||
| 61 | |||
| 62 | setEntidade(unidade); |
||
| 63 | } |
||
| 64 | |||
| 65 | @Override |
||
| 66 | protected GenericService<Unidade> getService() { |
||
| 67 | return unidadeService; |
||
| 68 | } |
||
| 69 | |||
| 70 | @Override |
||
| 71 | protected GenericRepository<Unidade> getRepositoryMock() { |
||
| 72 | return unidadeRepositoryMock; |
||
| 73 | } |
||
| 74 | |||
| 75 | @Override |
||
| 76 | protected void expectativasDeNegocioFuncionalidadeCadastrar() { |
||
| 77 | getContexto().checking(new Expectations(){{ |
||
| 78 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 79 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 80 | will(returnValue(new ArrayList<Unidade>())); |
||
| 81 | }}); |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | @Override |
||
| 86 | protected void expectativasDeNegocioFuncionalidadeAlterar() { |
||
| 87 | getContexto().checking(new Expectations(){{ |
||
| 88 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 89 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 90 | will(returnValue(new ArrayList<Unidade>())); |
||
| 91 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 92 | will(returnValue(orgao)); |
||
| 93 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 94 | will(returnValue(false)); |
||
| 95 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 96 | will(returnValue(false)); |
||
| 97 | }}); |
||
| 98 | } |
||
| 99 | |||
| 100 | @Test |
||
| 101 | public void aoCadastrarDeveriaDelegarParaConsultarPassandoRestricoes() throws Exception { |
||
| 102 | getContexto().checking(new Expectations(){{ |
||
| 103 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Cadastrar.class})); |
||
| 104 | oneOf(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 105 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 106 | will(returnValue(new ArrayList<Unidade>())); |
||
| 107 | ignoring(getRepositoryMock()).cadastrar(with(same(getEntidade()))); |
||
| 108 | }}); |
||
| 109 | getService().cadastrar(getEntidade()); |
||
| 110 | } |
||
| 111 | |||
| 112 | @Test |
||
| 113 | public void aoCadastrarUnidadeComDescricaoJaCadastradaDeveriaLancarExcecao() throws Exception { |
||
| 114 | final List<Unidade> unidades = new ArrayList<Unidade>(); |
||
| 115 | unidades.add(getEntidade()); |
||
| 116 | |||
| 117 | getContexto().checking(new Expectations(){{ |
||
| 118 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Cadastrar.class})); |
||
| 119 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 120 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 121 | will(returnValue(unidades)); |
||
| 122 | ignoring(getRepositoryMock()).cadastrar(with(same(getEntidade()))); |
||
| 123 | }}); |
||
| 124 | |||
| 125 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 126 | @Override |
||
| 127 | public void execute() throws Exception { |
||
| 128 | getService().cadastrar(getEntidade()); |
||
| 129 | } |
||
| 130 | |||
| 131 | }, "Unidade já cadastrada."); |
||
| 132 | } |
||
| 133 | |||
| 134 | @Test |
||
| 135 | public void aoAlterarDeveriaDelegarParaConsultarPassandoRestricoes() throws Exception { |
||
| 136 | getContexto().checking(new Expectations(){{ |
||
| 137 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 138 | oneOf(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 139 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 140 | will(returnValue(new ArrayList<Unidade>())); |
||
| 141 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 142 | will(returnValue(orgao)); |
||
| 143 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 144 | ignoring(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 145 | }}); |
||
| 146 | getService().alterar(getEntidade()); |
||
| 147 | } |
||
| 148 | |||
| 149 | @Test |
||
| 150 | public void aoAlterarDeveriaDelegarParaOhOrgaoRepositoryConsultarPorId() throws Exception { |
||
| 151 | getEntidade().setAtivo(true); |
||
| 152 | getEntidade().setCodigo( new Integer(SEQUENCIAL_UNIDADE_30)); |
||
| 153 | orgao.setAtivo(true); |
||
| 154 | |||
| 155 | getContexto().checking(new Expectations(){{ |
||
| 156 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 157 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 158 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 159 | will(returnValue(new ArrayList<Unidade>())); |
||
| 160 | oneOf(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 161 | will(returnValue(orgao)); |
||
| 162 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 163 | ignoring(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 164 | }}); |
||
| 165 | getService().alterar(getEntidade()); |
||
| 166 | } |
||
| 167 | |||
| 168 | @Test |
||
| 169 | public void aoAlterarUnidadeComDescricaoJaCadastradaDeveriaLancarExcecao() throws Exception { |
||
| 170 | final List<Unidade> unidades = new ArrayList<Unidade>(); |
||
| 171 | unidades.add(getEntidade()); |
||
| 172 | |||
| 173 | getContexto().checking(new Expectations(){{ |
||
| 174 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 175 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 176 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 177 | will(returnValue(unidades)); |
||
| 178 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 179 | will(returnValue(orgao)); |
||
| 180 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 181 | ignoring(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 182 | }}); |
||
| 183 | |||
| 184 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 185 | @Override |
||
| 186 | public void execute() throws Exception { |
||
| 187 | getService().alterar(getEntidade()); |
||
| 188 | } |
||
| 189 | |||
| 190 | }, "Unidade já cadastrada."); |
||
| 191 | } |
||
| 192 | |||
| 193 | @Test |
||
| 194 | public void aoAlterarUnidadeComUnidadeAtivaIhOrgaoInativoDeveriaLancarExcecao() throws Exception { |
||
| 195 | getEntidade().setAtivo(true); |
||
| 196 | orgao.setAtivo(false); |
||
| 197 | |||
| 198 | getContexto().checking(new Expectations(){{ |
||
| 199 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 200 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 201 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 202 | will(returnValue(new ArrayList<Unidade>())); |
||
| 203 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 204 | will(returnValue(orgao)); |
||
| 205 | ignoring(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 206 | ignoring(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 207 | }}); |
||
| 208 | |||
| 209 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 210 | @Override |
||
| 211 | public void execute() throws Exception { |
||
| 212 | getService().alterar(getEntidade()); |
||
| 213 | } |
||
| 214 | |||
| 215 | }, "Não é permitido ativar unidade vinculada a órgão inativado."); |
||
| 216 | } |
||
| 217 | |||
| 218 | @Test |
||
| 219 | public void aoAlterarUnidadeInativaDeveriaDelegarParaServidorRepositoryConsultarDadoFuncionalPassandoRestricoes() throws Exception { |
||
| 220 | final List<ServidorDadoFuncional> dadosFuncionais = new ArrayList<ServidorDadoFuncional>(); |
||
| 221 | |||
| 222 | getContexto().checking(new Expectations(){{ |
||
| 223 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 224 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 225 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 226 | will(returnValue(new ArrayList<Unidade>())); |
||
| 227 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 228 | will(returnValue(orgao)); |
||
| 229 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 230 | will(returnValue(false)); |
||
| 231 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 232 | will(returnValue(false)); |
||
| 233 | oneOf(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 234 | }}); |
||
| 235 | |||
| 236 | getService().alterar(getEntidade()); |
||
| 237 | } |
||
| 238 | |||
| 239 | @Test |
||
| 240 | public void aoAlterarUnidadeLotacaoInativaDeveriaLancarExcecaoDeUnidadeVinculadaAhDadoFuncional() throws Exception { |
||
| 241 | getContexto().checking(new Expectations(){{ |
||
| 242 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 243 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 244 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 245 | will(returnValue(new ArrayList<Unidade>())); |
||
| 246 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 247 | will(returnValue(orgao)); |
||
| 248 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 249 | will(returnValue(true)); |
||
| 250 | never(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 251 | never(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 252 | }}); |
||
| 253 | |||
| 254 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 255 | @Override |
||
| 256 | public void execute() throws Exception { |
||
| 257 | getService().alterar(getEntidade()); |
||
| 258 | } |
||
| 259 | |||
| 260 | }, "Não é possível inativar unidade que possua dado funcional de servidor vinculado."); |
||
| 261 | } |
||
| 262 | |||
| 263 | @Test |
||
| 264 | public void aoAlterarUnidadeExercicioInativaDeveriaLancarExcecaoDeUnidadeVinculadaAhDadoFuncional() throws Exception { |
||
| 265 | getContexto().checking(new Expectations(){{ |
||
| 266 | ignoring(getValidadorMock()).validar(with(same(getEntidade())), with(new Class<?>[]{Alterar.class})); |
||
| 267 | ignoring(unidadeRepositoryMock).consultarPassandoRestricoes(with(getEntidade()), |
||
| 268 | with(any(Integer.class)), with(any(Integer.class)), with(any(List.class))); |
||
| 269 | will(returnValue(new ArrayList<Unidade>())); |
||
| 270 | ignoring(orgaoRepositoryMock).consultarPorId(with(any(Orgao.class))); |
||
| 271 | will(returnValue(orgao)); |
||
| 272 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 273 | will(returnValue(false)); |
||
| 274 | oneOf(servidorRepositoryMock).consultarSeExisteDadoFuncionalPassandoRestricoes(with(any(List.class))); |
||
| 275 | will(returnValue(true)); |
||
| 276 | never(getRepositoryMock()).alterar(with(same(getEntidade()))); |
||
| 277 | }}); |
||
| 278 | |||
| 279 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 280 | @Override |
||
| 281 | public void execute() throws Exception { |
||
| 282 | getService().alterar(getEntidade()); |
||
| 283 | } |
||
| 284 | |||
| 285 | }, "Não é possível inativar unidade que possua dado funcional de servidor vinculado."); |
||
| 286 | } |
||
| 287 | |||
| 288 | @Test |
||
| 289 | public void aoConsultarUnidadeDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 290 | getContexto().checking(new Expectations(){{ |
||
| 291 | ((UnidadeRepository) oneOf(getRepositoryMock())).consultarUnidades(with(any(Unidade.class)), with(any(Integer.class)), with(any(Integer.class)) ); |
||
| 292 | }}); |
||
| 293 | |||
| 294 | |||
| 295 | ((UnidadeService) getService()).consultarUnidades(new Unidade(), 0, Integer.MAX_VALUE); |
||
| 296 | } |
||
| 297 | |||
| 298 | @Test |
||
| 299 | public void aoListarUnidadeAtivaDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 300 | getContexto().checking(new Expectations(){{ |
||
| 301 | oneOf(unidadeRepositoryMock).listarUnidadeAtiva(with(any(Unidade.class))); |
||
| 302 | }}); |
||
| 303 | unidadeService.listarUnidadeAtiva(new Unidade()); |
||
| 304 | } |
||
| 305 | |||
| 306 | @Test |
||
| 307 | public void aoListarUnidadeDoServidorDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 308 | getContexto().checking(new Expectations(){{ |
||
| 309 | oneOf(unidadeRepositoryMock).listarUnidadeDoServidor(with(any(Long.class))); |
||
| 310 | }}); |
||
| 311 | unidadeService.listarUnidadeDoServidor(new Long("1")); |
||
| 312 | } |
||
| 313 | |||
| 314 | @Test |
||
| 315 | public void aoObterQuantidadeRegistrosParaConsultarUnidadesDeveriaDelegarParaOhRepositorio() throws Exception { |
||
| 316 | getContexto().checking(new Expectations(){{ |
||
| 317 | oneOf(unidadeRepositoryMock).obterQuantidadeDeRegistrosParaConsultarUnidades(with(any(Unidade.class))); |
||
| 318 | }}); |
||
| 319 | unidadeService.obterQuantidadeDeRegistrosParaConsultarUnidades(new Unidade()); |
||
| 320 | } |
||
| 321 | |||
| 322 | } |