Search in sources :

Example 1 with GenericEntity

use of org.jboss.pnc.model.GenericEntity in project pnc by project-ncl.

the class DeliverableAnalyzerOperationProviderImplTest method prepareRepositoryMock.

@Before
@Override
public void prepareRepositoryMock() {
    when(em.getReference(any(), any())).thenAnswer(inv -> {
        Class<GenericEntity> type = inv.getArgument(0, Class.class);
        Serializable id = inv.getArgument(1, Serializable.class);
        GenericEntity mock = mock(type);
        when(mock.getId()).thenReturn(id);
        return mock;
    });
    when(repository().queryWithPredicates(any(), any(), any())).thenAnswer(new ListAnswer(repositoryList));
    when(repository().count(any())).thenAnswer(inv -> repositoryList.size());
    when(repository().queryById(any())).thenAnswer(inv -> {
        Base32LongID id = inv.getArgument(0);
        return repositoryList.stream().filter(a -> id.equals(a.getId())).findFirst().orElse(null);
    });
    when(repository().save(any())).thenAnswer(inv -> {
        DeliverableAnalyzerOperation entity = inv.getArgument(0);
        if (entity.getId() == null) {
            entity.setId(getNextId());
            repositoryList.add(entity);
            return entity;
        } else {
            boolean found = false;
            for (int i = 0; i < repositoryList.size(); i++) {
                if (repositoryList.get(i).getId().equals(entity.getId())) {
                    repositoryList.set(i, entity);
                    return entity;
                }
            }
            if (!found) {
                repositoryList.add(entity);
                return entity;
            }
        }
        throw new IllegalArgumentException("Provided entity has ID but is not in the repository.");
    });
    doAnswer(inv -> {
        Base32LongID id = inv.getArgument(0);
        DeliverableAnalyzerOperation object = repositoryList.stream().filter(a -> id.equals(a.getId())).findFirst().orElse(null);
        repositoryList.remove(object);
        return null;
    }).when(repository()).delete(any());
}
Also used : Serializable(java.io.Serializable) Base32LongID(org.jboss.pnc.model.Base32LongID) GenericEntity(org.jboss.pnc.model.GenericEntity) DeliverableAnalyzerOperation(org.jboss.pnc.model.DeliverableAnalyzerOperation) Before(org.junit.Before)

Example 2 with GenericEntity

use of org.jboss.pnc.model.GenericEntity in project pnc by project-ncl.

the class AbstractProviderTest method prepareRepositoryMock.

@Before
public void prepareRepositoryMock() {
    when(em.getReference(any(), any())).thenAnswer(inv -> {
        Class<GenericEntity> type = inv.getArgument(0, Class.class);
        Serializable id = inv.getArgument(1, Serializable.class);
        GenericEntity mock = mock(type);
        when(mock.getId()).thenReturn(id);
        return mock;
    });
    when(repository().queryWithPredicates(any(), any(), any())).thenAnswer(new ListAnswer(repositoryList));
    when(repository().count(any())).thenAnswer(inv -> repositoryList.size());
    when(repository().save(any())).thenAnswer(inv -> {
        T entity = inv.getArgument(0);
        if (entity.getId() == null) {
            entity.setId(getNextId());
            repositoryList.add(entity);
            return entity;
        } else {
            for (int i = 0; i < repositoryList.size(); i++) {
                if (repositoryList.get(i).getId().equals(entity.getId())) {
                    repositoryList.set(i, entity);
                    return entity;
                }
            }
        }
        throw new IllegalArgumentException("Provided entity has ID but is not in the repository.");
    });
    when(repository().queryById(any())).thenAnswer(inv -> {
        Integer id = inv.getArgument(0);
        return repositoryList.stream().filter(a -> id.equals(a.getId())).findFirst().orElse(null);
    });
    doAnswer(inv -> {
        Integer id = inv.getArgument(0);
        T object = repositoryList.stream().filter(a -> id.equals(a.getId())).findFirst().orElse(null);
        repositoryList.remove(object);
        return null;
    }).when(repository()).delete(any());
}
Also used : Serializable(java.io.Serializable) GenericEntity(org.jboss.pnc.model.GenericEntity) Before(org.junit.Before)

Aggregations

Serializable (java.io.Serializable)2 GenericEntity (org.jboss.pnc.model.GenericEntity)2 Before (org.junit.Before)2 Base32LongID (org.jboss.pnc.model.Base32LongID)1 DeliverableAnalyzerOperation (org.jboss.pnc.model.DeliverableAnalyzerOperation)1