package br.gov.al.saude.srv.core.domain;
import static br.gov.al.saude.test.ConstantesTestHelper.OrgaoConstantes.DESCRICAO_ORGAO_SECRETARIA_ESTADO_SAUDE;
import static br.gov.al.saude.test.ConstantesTestHelper.OrgaoConstantes.SEQUENCIAL_ORGAO_SECRETARIA_ESTADO_SAUDE_20;
import static br.gov.al.saude.test.ConstantesTestHelper.UnidadeConstantes.DESCRICAO_UNIDADE_COORDENADORIA_SETORIAL_DA_GESTAO_DA_INFORMATICA;
import static br.gov.al.saude.test.ConstantesTestHelper.UnidadeConstantes.SEQUENCIAL_UNIDADE_30;
import java.util.ArrayList;
import java.util.List;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import br.gov.al.saude.framework.core.generic.GenericRepository;
import br.gov.al.saude.framework.core.generic.GenericService;
import br.gov.al.saude.framework.core.interfaces.Alterar;
import br.gov.al.saude.framework.core.interfaces.Cadastrar;
import br.gov.al.saude.framework.core.validador.Validador;
import br.gov.al.saude.srv.core.domain.unidade.UnidadeService;
import br.gov.al.saude.srv.core.domain.unidade.impl.UnidadeServiceImpl;
import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.orgao.OrgaoRepository;
import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.servidor.ServidorRepository;
import br.gov.al.saude.srv.core.infrastructure.persistence.jpa.unidade.UnidadeRepository;
import br.gov.al.saude.srv.model.Orgao;
import br.gov.al.saude.srv.model.ServidorDadoFuncional;
import br.gov.al.saude.srv.model.Unidade;
import br.gov.al.saude.test.builder.OrgaoBuilder;
import br.gov.al.saude.test.builder.UnidadeBuilder;
import br.gov.al.saude.test.unidade.generic.service.ServiceImplTest;
import br.gov.al.saude.test.util.CommandSemMensagem;
import br.gov.al.saude.test.util.VerificadorLancamentoException;
public class UnidadeServiceImplTest
extends ServiceImplTest
<Unidade
> {
private UnidadeService unidadeService
;
private UnidadeRepository unidadeRepositoryMock
;
private OrgaoRepository orgaoRepositoryMock
;
private ServidorRepository servidorRepositoryMock
;
private Unidade unidade
;
private Orgao orgao
;
@Before
public void inicializarContexto
() {
unidadeRepositoryMock = getContexto
().
mock(UnidadeRepository.
class);
orgaoRepositoryMock = getContexto
().
mock(OrgaoRepository.
class);
validadorMock = getContexto
().
mock(Validador.
class);
servidorRepositoryMock = getContexto
().
mock(ServidorRepository.
class);
unidadeService =
new UnidadeServiceImpl
(validadorMock, unidadeRepositoryMock, orgaoRepositoryMock, servidorRepositoryMock
);
orgao =
new OrgaoBuilder
().
comCodigoOrgao(SEQUENCIAL_ORGAO_SECRETARIA_ESTADO_SAUDE_20
)
.
comDescricao(DESCRICAO_ORGAO_SECRETARIA_ESTADO_SAUDE
)
.
comSituacaoAtivo("SIM")
.
build();
unidade =
new UnidadeBuilder
().
comOrgao(SEQUENCIAL_ORGAO_SECRETARIA_ESTADO_SAUDE_20
)
.
comDescricao(DESCRICAO_UNIDADE_COORDENADORIA_SETORIAL_DA_GESTAO_DA_INFORMATICA
)
.
comSigla(DESCRICAO_UNIDADE_COORDENADORIA_SETORIAL_DA_GESTAO_DA_INFORMATICA
)
.
comSituacaoAtivo("NÃO")
.
build();
setEntidade
(unidade
);
}
@
Override
protected GenericService
<Unidade
> getService
() {
return unidadeService
;
}
@
Override
protected GenericRepository
<Unidade
> getRepositoryMock
() {
return unidadeRepositoryMock
;
}
@
Override
protected void expectativasDeNegocioFuncionalidadeCadastrar
() {
getContexto
().
checking(new Expectations
(){{
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
}});
}
@
Override
protected void expectativasDeNegocioFuncionalidadeAlterar
() {
getContexto
().
checking(new Expectations
(){{
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(false));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(false));
}});
}
@Test
public void aoCadastrarDeveriaDelegarParaConsultarPassandoRestricoes
() throws Exception {
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Cadastrar.
class}));
oneOf
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(getRepositoryMock
()).
cadastrar(with
(same
(getEntidade
())));
}});
getService
().
cadastrar(getEntidade
());
}
@Test
public void aoCadastrarUnidadeComDescricaoJaCadastradaDeveriaLancarExcecao
() throws Exception {
final List<Unidade
> unidades =
new ArrayList<Unidade
>();
unidades.
add(getEntidade
());
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Cadastrar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(unidades
));
ignoring
(getRepositoryMock
()).
cadastrar(with
(same
(getEntidade
())));
}});
VerificadorLancamentoException.
verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem
() {
@
Override
public void execute
() throws Exception {
getService
().
cadastrar(getEntidade
());
}
},
"Unidade já cadastrada.");
}
@Test
public void aoAlterarDeveriaDelegarParaConsultarPassandoRestricoes
() throws Exception {
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
oneOf
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
ignoring
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
ignoring
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
getService
().
alterar(getEntidade
());
}
@Test
public void aoAlterarDeveriaDelegarParaOhOrgaoRepositoryConsultarPorId
() throws Exception {
getEntidade
().
setAtivo(true);
getEntidade
().
setCodigo( new Integer(SEQUENCIAL_UNIDADE_30
));
orgao.
setAtivo(true);
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
oneOf
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
ignoring
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
ignoring
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
getService
().
alterar(getEntidade
());
}
@Test
public void aoAlterarUnidadeComDescricaoJaCadastradaDeveriaLancarExcecao
() throws Exception {
final List<Unidade
> unidades =
new ArrayList<Unidade
>();
unidades.
add(getEntidade
());
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(unidades
));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
ignoring
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
ignoring
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
VerificadorLancamentoException.
verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem
() {
@
Override
public void execute
() throws Exception {
getService
().
alterar(getEntidade
());
}
},
"Unidade já cadastrada.");
}
@Test
public void aoAlterarUnidadeComUnidadeAtivaIhOrgaoInativoDeveriaLancarExcecao
() throws Exception {
getEntidade
().
setAtivo(true);
orgao.
setAtivo(false);
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
ignoring
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
ignoring
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
VerificadorLancamentoException.
verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem
() {
@
Override
public void execute
() throws Exception {
getService
().
alterar(getEntidade
());
}
},
"Não é permitido ativar unidade vinculada a órgão inativado.");
}
@Test
public void aoAlterarUnidadeInativaDeveriaDelegarParaServidorRepositoryConsultarDadoFuncionalPassandoRestricoes
() throws Exception {
final List<ServidorDadoFuncional
> dadosFuncionais =
new ArrayList<ServidorDadoFuncional
>();
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(false));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(false));
oneOf
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
getService
().
alterar(getEntidade
());
}
@Test
public void aoAlterarUnidadeLotacaoInativaDeveriaLancarExcecaoDeUnidadeVinculadaAhDadoFuncional
() throws Exception {
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(true));
never
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
never
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
VerificadorLancamentoException.
verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem
() {
@
Override
public void execute
() throws Exception {
getService
().
alterar(getEntidade
());
}
},
"Não é possível inativar unidade que possua dado funcional de servidor vinculado.");
}
@Test
public void aoAlterarUnidadeExercicioInativaDeveriaLancarExcecaoDeUnidadeVinculadaAhDadoFuncional
() throws Exception {
getContexto
().
checking(new Expectations
(){{
ignoring
(getValidadorMock
()).
validar(with
(same
(getEntidade
())), with
(new Class<?>[]{Alterar.
class}));
ignoring
(unidadeRepositoryMock
).
consultarPassandoRestricoes(with
(getEntidade
()),
with
(any
(Integer.
class)), with
(any
(Integer.
class)), with
(any
(List.
class)));
will
(returnValue
(new ArrayList<Unidade
>()));
ignoring
(orgaoRepositoryMock
).
consultarPorId(with
(any
(Orgao.
class)));
will
(returnValue
(orgao
));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(false));
oneOf
(servidorRepositoryMock
).
consultarSeExisteDadoFuncionalPassandoRestricoes(with
(any
(List.
class)));
will
(returnValue
(true));
never
(getRepositoryMock
()).
alterar(with
(same
(getEntidade
())));
}});
VerificadorLancamentoException.
verificarMensagemLancamentoExceptionSemRetornoMensagem(new CommandSemMensagem
() {
@
Override
public void execute
() throws Exception {
getService
().
alterar(getEntidade
());
}
},
"Não é possível inativar unidade que possua dado funcional de servidor vinculado.");
}
@Test
public void aoConsultarUnidadeDeveriaDelegarParaOhRepositorio
() throws Exception {
getContexto
().
checking(new Expectations
(){{
((UnidadeRepository
) oneOf
(getRepositoryMock
())).
consultarUnidades(with
(any
(Unidade.
class)), with
(any
(Integer.
class)), with
(any
(Integer.
class)) );
}});
((UnidadeService
) getService
()).
consultarUnidades(new Unidade
(),
0,
Integer.
MAX_VALUE);
}
@Test
public void aoListarUnidadeAtivaDeveriaDelegarParaOhRepositorio
() throws Exception {
getContexto
().
checking(new Expectations
(){{
oneOf
(unidadeRepositoryMock
).
listarUnidadeAtiva(with
(any
(Unidade.
class)));
}});
unidadeService.
listarUnidadeAtiva(new Unidade
());
}
@Test
public void aoListarUnidadeDoServidorDeveriaDelegarParaOhRepositorio
() throws Exception {
getContexto
().
checking(new Expectations
(){{
oneOf
(unidadeRepositoryMock
).
listarUnidadeDoServidor(with
(any
(Long.
class)));
}});
unidadeService.
listarUnidadeDoServidor(new Long("1"));
}
@Test
public void aoObterQuantidadeRegistrosParaConsultarUnidadesDeveriaDelegarParaOhRepositorio
() throws Exception {
getContexto
().
checking(new Expectations
(){{
oneOf
(unidadeRepositoryMock
).
obterQuantidadeDeRegistrosParaConsultarUnidades(with
(any
(Unidade.
class)));
}});
unidadeService.
obterQuantidadeDeRegistrosParaConsultarUnidades(new Unidade
());
}
}