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.MensagensRetorno.CODIGO_ORGAO_JA_CADASTRADO; |
||
| 4 | import static br.gov.al.saude.test.ConstantesTestHelper.MensagensRetorno.DESCRICAO_ORGAO_JA_CADASTRADA; |
||
| 5 | import static br.gov.al.saude.test.ConstantesTestHelper.MensagensRetorno.NAO_EH_POSSIVEL_INATIVAR_ORGAO_VINCULADO_A_UNIDADE_ATIVA; |
||
| 6 | |||
| 7 | import java.util.ArrayList; |
||
| 8 | import java.util.List; |
||
| 9 | |||
| 10 | import org.jmock.Expectations; |
||
| 11 | import org.junit.Before; |
||
| 12 | import org.junit.Test; |
||
| 13 | |||
| 14 | import br.gov.al.saude.framework.core.generic.GenericRepository; |
||
| 15 | import br.gov.al.saude.framework.core.generic.GenericService; |
||
| 16 | import br.gov.al.saude.framework.core.interfaces.Alterar; |
||
| 17 | import br.gov.al.saude.framework.core.interfaces.Cadastrar; |
||
| 18 | import br.gov.al.saude.framework.core.validador.Validador; |
||
| 19 | import br.gov.al.saude.srv.core.domain.orgao.OrgaoService; |
||
| 20 | import br.gov.al.saude.srv.core.domain.orgao.impl.OrgaoServiceImpl; |
||
| 21 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.orgao.OrgaoRepository; |
||
| 22 | import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.unidade.UnidadeRepository; |
||
| 23 | import br.gov.al.saude.srv.model.Orgao; |
||
| 24 | import br.gov.al.saude.srv.model.Unidade; |
||
| 25 | import br.gov.al.saude.test.unidade.generic.service.ServiceImplTest; |
||
| 26 | import br.gov.al.saude.test.util.CommandSemMensagem; |
||
| 27 | import br.gov.al.saude.test.util.VerificadorLancamentoException; |
||
| 28 | |||
| 29 | public class OrgaoServiceImplTest extends ServiceImplTest<Orgao> { |
||
| 30 | |||
| 31 | private OrgaoService orgaoService; |
||
| 32 | private OrgaoRepository orgaoRepositoryMock; |
||
| 33 | private UnidadeRepository unidadeRepositoryMock; |
||
| 34 | |||
| 35 | private Orgao getOrgao() { |
||
| 36 | Orgao orgao = new Orgao(); |
||
| 37 | orgao.setSequencial(1); |
||
| 38 | orgao.setCodigoOrgao(1500); |
||
| 39 | orgao.setDescricao("SECRETARIA DE ALAGOAS"); |
||
| 40 | orgao.setEsfera("E"); |
||
| 41 | orgao.setSigla("SAL"); |
||
| 42 | orgao.setAtivo(false); |
||
| 43 | return orgao; |
||
| 44 | } |
||
| 45 | |||
| 46 | private List<Unidade> getUnidadeAtivas() { |
||
| 47 | List<Unidade> unidadesAtiva = new ArrayList<Unidade>(); |
||
| 48 | return unidadesAtiva; |
||
| 49 | } |
||
| 50 | |||
| 51 | @Before |
||
| 52 | public void inicializarContexto() { |
||
| 53 | orgaoRepositoryMock = getContexto().mock(OrgaoRepository.class); |
||
| 54 | unidadeRepositoryMock = getContexto().mock(UnidadeRepository.class); |
||
| 55 | validadorMock = getContexto().mock(Validador.class); |
||
| 56 | orgaoService = new OrgaoServiceImpl(validadorMock, orgaoRepositoryMock, unidadeRepositoryMock); |
||
| 57 | Orgao orgao = new Orgao(); |
||
| 58 | orgao.setAtivo(true); |
||
| 59 | setEntidade(orgao); |
||
| 60 | } |
||
| 61 | |||
| 62 | @Override |
||
| 63 | protected GenericService<Orgao> getService() { |
||
| 64 | return orgaoService; |
||
| 65 | } |
||
| 66 | |||
| 67 | @Override |
||
| 68 | protected void expectativasDeNegocioFuncionalidadeCadastrar() { |
||
| 69 | getContexto().checking(new Expectations(){{ |
||
| 70 | never(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 71 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 72 | will(returnValue(null)); |
||
| 73 | }}); |
||
| 74 | } |
||
| 75 | |||
| 76 | @Override |
||
| 77 | protected void expectativasDeNegocioFuncionalidadeAlterar() { |
||
| 78 | getContexto().checking(new Expectations(){{ |
||
| 79 | never(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 80 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 81 | will(returnValue(null)); |
||
| 82 | never(unidadeRepositoryMock).consultarUnidadeAtivasPorOrgao(with(any(Orgao.class))); |
||
| 83 | }}); |
||
| 84 | } |
||
| 85 | |||
| 86 | @Test |
||
| 87 | public void aoCadastrarOrgaoComCodigoDeveriaDelegarParaOhRepositorio() { |
||
| 88 | final Orgao orgao = getOrgao(); |
||
| 89 | |||
| 90 | getContexto().checking(new Expectations(){{ |
||
| 91 | oneOf(getValidadorMock()).validar(with(same(orgao)), with(new Class<?>[]{ Cadastrar.class })); |
||
| 92 | oneOf(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 93 | will(returnValue(null)); |
||
| 94 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 95 | will(returnValue(null)); |
||
| 96 | oneOf(orgaoRepositoryMock).cadastrar(with(same(orgao))); |
||
| 97 | }}); |
||
| 98 | |||
| 99 | orgaoService.cadastrar(orgao); |
||
| 100 | } |
||
| 101 | |||
| 102 | @Test |
||
| 103 | public void aoCadastrarOrgaoComCodigoJahCadastradoDeveriaRetornarMensagem() { |
||
| 104 | final Orgao orgao = getOrgao(); |
||
| 105 | |||
| 106 | getContexto().checking(new Expectations(){{ |
||
| 107 | oneOf(getValidadorMock()).validar(with(same(orgao)), with(new Class<?>[]{ Cadastrar.class })); |
||
| 108 | oneOf(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 109 | will(returnValue(orgao)); |
||
| 110 | never(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 111 | never(orgaoRepositoryMock).cadastrar(with(same(orgao))); |
||
| 112 | }}); |
||
| 113 | |||
| 114 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 115 | @Override |
||
| 116 | public void execute() throws Exception { |
||
| 117 | orgaoService.cadastrar(orgao); |
||
| 118 | } |
||
| 119 | }, CODIGO_ORGAO_JA_CADASTRADO); |
||
| 120 | } |
||
| 121 | |||
| 122 | @Test |
||
| 123 | public void aoCadastrarOrgaoComDescricaoJahCadastradaDeveriaRetornarMensagem() { |
||
| 124 | final Orgao orgao = getOrgao(); |
||
| 125 | |||
| 126 | getContexto().checking(new Expectations(){{ |
||
| 127 | oneOf(getValidadorMock()).validar(with(same(orgao)), with(new Class<?>[]{ Cadastrar.class })); |
||
| 128 | oneOf(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 129 | will(returnValue(null)); |
||
| 130 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 131 | will(returnValue(orgao)); |
||
| 132 | never(orgaoRepositoryMock).cadastrar(with(same(orgao))); |
||
| 133 | }}); |
||
| 134 | |||
| 135 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 136 | @Override |
||
| 137 | public void execute() throws Exception { |
||
| 138 | orgaoService.cadastrar(orgao); |
||
| 139 | } |
||
| 140 | }, DESCRICAO_ORGAO_JA_CADASTRADA); |
||
| 141 | } |
||
| 142 | |||
| 143 | @Test |
||
| 144 | public void aoAlterarOrgaoParaInativoComUnidadesAtivasDeveriaRetornarMensagem() { |
||
| 145 | final Orgao orgao = getOrgao(); |
||
| 146 | final List<Unidade> unidadesAtiva = getUnidadeAtivas(); |
||
| 147 | unidadesAtiva.add(new Unidade()); |
||
| 148 | |||
| 149 | getContexto().checking(new Expectations(){{ |
||
| 150 | oneOf(getValidadorMock()).validar(with(any(Orgao.class)), with(new Class<?>[]{ Alterar.class })); |
||
| 151 | oneOf(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 152 | will(returnValue(null)); |
||
| 153 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 154 | will(returnValue(null)); |
||
| 155 | oneOf(unidadeRepositoryMock).consultarUnidadeAtivasPorOrgao(with(any(Orgao.class))); |
||
| 156 | will(returnValue(unidadesAtiva)); |
||
| 157 | never(orgaoRepositoryMock).alterar(with(same(orgao))); |
||
| 158 | }}); |
||
| 159 | |||
| 160 | VerificadorLancamentoException.verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem() { |
||
| 161 | @Override |
||
| 162 | public void execute() throws Exception { |
||
| 163 | orgaoService.alterar(orgao); |
||
| 164 | } |
||
| 165 | }, NAO_EH_POSSIVEL_INATIVAR_ORGAO_VINCULADO_A_UNIDADE_ATIVA); |
||
| 166 | } |
||
| 167 | |||
| 168 | @Test |
||
| 169 | public void aoAlterarOrgaoParaInativoSemUnidadesAtivasDeveriaDelegarParaOhRepositorio() { |
||
| 170 | final Orgao orgao = getOrgao(); |
||
| 171 | final List<Unidade> unidadesAtiva = getUnidadeAtivas(); |
||
| 172 | |||
| 173 | getContexto().checking(new Expectations(){{ |
||
| 174 | oneOf(getValidadorMock()).validar(with(any(Orgao.class)), with(new Class<?>[]{ Alterar.class })); |
||
| 175 | oneOf(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 176 | will(returnValue(null)); |
||
| 177 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 178 | will(returnValue(null)); |
||
| 179 | oneOf(unidadeRepositoryMock).consultarUnidadeAtivasPorOrgao(with(any(Orgao.class))); |
||
| 180 | will(returnValue(unidadesAtiva)); |
||
| 181 | oneOf(orgaoRepositoryMock).alterar(with(same(orgao))); |
||
| 182 | }}); |
||
| 183 | |||
| 184 | orgaoService.alterar(orgao); |
||
| 185 | } |
||
| 186 | |||
| 187 | @Test |
||
| 188 | public void aoAlterarOrgaoSemAlterarDescricaoIhCodigoDeveriaDelegarParaOhRepositorio() { |
||
| 189 | final Orgao orgao = getOrgao(); |
||
| 190 | final List<Unidade> unidadesAtiva = getUnidadeAtivas(); |
||
| 191 | |||
| 192 | getContexto().checking(new Expectations(){{ |
||
| 193 | oneOf(getValidadorMock()).validar(with(any(Orgao.class)), with(new Class<?>[]{ Alterar.class })); |
||
| 194 | oneOf(orgaoRepositoryMock).consultarPorCodigoOrgao(with(any(Integer.class))); |
||
| 195 | will(returnValue(orgao)); |
||
| 196 | oneOf(orgaoRepositoryMock).consultarPorDescricao(with(any(String.class))); |
||
| 197 | will(returnValue(orgao)); |
||
| 198 | oneOf(unidadeRepositoryMock).consultarUnidadeAtivasPorOrgao(with(any(Orgao.class))); |
||
| 199 | will(returnValue(unidadesAtiva)); |
||
| 200 | oneOf(orgaoRepositoryMock).alterar(with(same(orgao))); |
||
| 201 | }}); |
||
| 202 | |||
| 203 | orgaoService.alterar(orgao); |
||
| 204 | } |
||
| 205 | |||
| 206 | @Test |
||
| 207 | public void aoConsultarOrgaosDeveriaDelegarParaOhRepositorio() { |
||
| 208 | getContexto().checking(new Expectations(){{ |
||
| 209 | oneOf(orgaoRepositoryMock).consultarOrgaos(with(any(Orgao.class)), with(any(Integer.class)), with(any(Integer.class))); |
||
| 210 | }}); |
||
| 211 | |||
| 212 | orgaoService.consultarOrgaos(getOrgao(), 0, 5); |
||
| 213 | } |
||
| 214 | |||
| 215 | @Test |
||
| 216 | public void aoObterQuantidadeDeRegistrosDeConsultarOrgaosDeveriaDelegarParaOhRepositorio() { |
||
| 217 | getContexto().checking(new Expectations(){{ |
||
| 218 | oneOf(orgaoRepositoryMock).obterQuantidadeDeRegistrosDeConsultarOrgaos(with(any(Orgao.class))); |
||
| 219 | }}); |
||
| 220 | |||
| 221 | orgaoService.obterQuantidadeDeRegistrosDeConsultarOrgaos(getOrgao()); |
||
| 222 | } |
||
| 223 | |||
| 224 | |||
| 225 | @Override |
||
| 226 | protected GenericRepository<Orgao> getRepositoryMock() { |
||
| 227 | return orgaoRepositoryMock; |
||
| 228 | } |
||
| 229 | |||
| 230 | @Test |
||
| 231 | public void aoListarOrgaosDeveriaDelegarParaOhRepositorio() { |
||
| 232 | getContexto().checking(new Expectations(){{ |
||
| 233 | oneOf(orgaoRepositoryMock).listarOrgao(); |
||
| 234 | }}); |
||
| 235 | |||
| 236 | orgaoService.listarOrgao(); |
||
| 237 | } |
||
| 238 | |||
| 239 | @Test |
||
| 240 | public void aoListarOrgaoAtivoDeveriaDelegarParaOhRepositorio() { |
||
| 241 | getContexto().checking(new Expectations(){{ |
||
| 242 | oneOf(orgaoRepositoryMock).listarOrgaoAtivo(); |
||
| 243 | }}); |
||
| 244 | orgaoService.listarOrgaoAtivo(); |
||
| 245 | } |
||
| 246 | } |