Skip to content

Commit

Permalink
Fixed jUnit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Dec 12, 2022
1 parent 3120a10 commit 1d3ea75
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand All @@ -49,7 +49,7 @@ public class AccessInfoDAOTest {

EntityManager entityManager;
AccessInfoCreator accessInfoCreator;
KapuaId scopeId, accessInfoId;
KapuaId scopeId, accessInfoId;
AccessInfoImpl entityToFindOrDelete;
KapuaQuery kapuaQuery;

Expand Down Expand Up @@ -154,7 +154,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessInfoImpl.class, accessInfoId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessInfoDAO.find(entityManager, scopeId, accessInfoId) instanceof AccessInfo);
Assert.assertTrue("True expected.", AccessInfoDAO.find(entityManager, KapuaId.ANY, accessInfoId) instanceof AccessInfo);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -266,6 +266,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessInfoImpl.class, accessInfoId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessInfoDAO.delete(entityManager, scopeId, accessInfoId) instanceof AccessInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand Down Expand Up @@ -153,7 +153,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessPermissionImpl.class, accessPermissionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessPermissionDAO.find(entityManager, scopeId, accessPermissionId) instanceof AccessPermission);
Assert.assertTrue("True expected.", AccessPermissionDAO.find(entityManager, KapuaId.ANY, accessPermissionId) instanceof AccessPermission);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -265,6 +265,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessPermissionImpl.class, accessPermissionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessPermissionDAO.delete(entityManager, scopeId, accessPermissionId) instanceof AccessPermission);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authorization.access.AccessRole;
import org.eclipse.kapua.service.authorization.access.AccessRoleCreator;
import org.eclipse.kapua.service.authorization.access.AccessRoleListResult;
import org.eclipse.kapua.service.authorization.access.AccessRole;
import org.eclipse.kapua.service.authorization.access.shiro.AccessRoleDAO;
import org.eclipse.kapua.service.authorization.access.shiro.AccessRoleImpl;
import org.junit.Assert;
Expand All @@ -33,10 +33,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand Down Expand Up @@ -162,7 +162,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessRoleImpl.class, accessRoleId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessRoleDAO.find(entityManager, scopeId, accessRoleId) instanceof AccessRole);
Assert.assertTrue("True expected.", AccessRoleDAO.find(entityManager, KapuaId.ANY, accessRoleId) instanceof AccessRole);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -274,6 +274,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessRoleImpl.class, accessRoleId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessRoleDAO.delete(entityManager, scopeId, accessRoleId) instanceof AccessRole);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Predicate;

import javax.persistence.criteria.Root;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand Down Expand Up @@ -217,7 +216,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessTokenImpl.class, accessTokenId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessTokenDAO.find(entityManager, scopeId, accessTokenId) instanceof AccessToken);
Assert.assertTrue("True expected.", AccessTokenDAO.find(entityManager, KapuaId.ANY, accessTokenId) instanceof AccessToken);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -439,6 +438,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessTokenImpl.class, accessTokenId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessTokenDAO.delete(entityManager, scopeId, accessTokenId) instanceof AccessToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authentication.credential.Credential;
import org.eclipse.kapua.service.authentication.credential.CredentialCreator;
import org.eclipse.kapua.service.authentication.credential.CredentialListResult;
import org.eclipse.kapua.service.authentication.credential.CredentialStatus;
import org.eclipse.kapua.service.authentication.credential.CredentialType;
import org.eclipse.kapua.service.authentication.credential.CredentialListResult;
import org.eclipse.kapua.service.authentication.credential.CredentialCreator;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialDAO;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialImpl;
import org.junit.Assert;
Expand All @@ -36,8 +36,8 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
Expand Down Expand Up @@ -250,7 +250,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(CredentialImpl.class, credentialId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", CredentialDAO.find(entityManager, scopeId, credentialId) instanceof Credential);
Assert.assertTrue("True expected.", CredentialDAO.find(entityManager, KapuaId.ANY, credentialId) instanceof Credential);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -362,6 +362,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(CredentialImpl.class, credentialId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", CredentialDAO.delete(entityManager, scopeId, credentialId) instanceof Credential);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -185,7 +184,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(DomainImpl.class, domainId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", DomainDAO.find(entityManager, scopeId, domainId) instanceof Domain);
Assert.assertTrue("True expected.", DomainDAO.find(entityManager, KapuaId.ANY, domainId) instanceof Domain);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -298,6 +297,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(DomainImpl.class, domainId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", DomainDAO.delete(entityManager, scopeId, domainId) instanceof Domain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;

import org.hamcrest.core.IsInstanceOf;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -35,12 +34,12 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.metamodel.EntityType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -177,22 +176,25 @@ public void findTest() {
}

//entityToFind not null
for (KapuaId event : eventIds) {
for (KapuaId eventId : eventIds) {
eventStoreRecordImpl.setScopeId(null);
Mockito.when(entityManager.find(EventStoreRecordImpl.class, event)).thenReturn(eventStoreRecordImpl);
Assert.assertEquals("Expected and actual values should be the same.", eventStoreRecordImpl, EventStoreDAO.find(entityManager, null, event));
Assert.assertEquals("Expected and actual values should be the same.", eventStoreRecordImpl, EventStoreDAO.find(entityManager, scopeIdOne, event));
Mockito.when(entityManager.find(EventStoreRecordImpl.class, eventId)).thenReturn(eventStoreRecordImpl);
Assert.assertEquals("Expected and actual values should be the same.", eventStoreRecordImpl, EventStoreDAO.find(entityManager, null, eventId));
Assert.assertNull("Expected and actual values should be the same.", EventStoreDAO.find(entityManager, scopeIdOne, eventId));

eventStoreRecordImpl.setScopeId(scopeIdOne);
Assert.assertEquals("Expected and actual values should be the same.", eventStoreRecordImpl, EventStoreDAO.find(entityManager, scopeIdOne, event));
Assert.assertEquals("Expected and actual values should be the same.", eventStoreRecordImpl, EventStoreDAO.find(entityManager, scopeIdOne, eventId));

eventStoreRecordImpl.setScopeId(scopeIdTen);
Assert.assertNull("Null expected", EventStoreDAO.find(entityManager, scopeIdOne, event));
Assert.assertNull("Null expected", EventStoreDAO.find(entityManager, scopeIdOne, eventId));

try {
EventStoreDAO.find(null, scopeIdOne, event);
EventStoreDAO.find(null, scopeIdOne, eventId);
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", nullPointerException.toString(), e.toString());
}
try {
EventStoreDAO.find(null, null, event);
EventStoreDAO.find(null, null, eventId);
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", nullPointerException.toString(), e.toString());
}
Expand Down Expand Up @@ -246,7 +248,7 @@ public void countTest() throws KapuaException {
Mockito.when(entityManager.createQuery(criteriaQuery1)).thenReturn(query);
for (long number : longNumberList) {
Mockito.doReturn(number).when(query).getSingleResult();
Assert.assertThat("Long object expected.", EventStoreDAO.count(entityManager, kapuaQuery), IsInstanceOf.instanceOf(Long.class));
Assert.assertThat("Long object expected.", EventStoreDAO.count(entityManager, kapuaQuery), IsInstanceOf.instanceOf(Long.class));
}
}

Expand All @@ -273,18 +275,5 @@ public void deleteTest() throws KapuaException {
Assert.assertEquals("NullPointerException expected", nullPointerException.toString(), e.toString());
}
}

//entityToDelete not null
for (KapuaId eventId : eventIds) {
for (KapuaId scopeId : scopeIds) {
Mockito.when(entityManager.find(EventStoreRecordImpl.class, eventId)).thenReturn(eventStoreRecordImpl);
Assert.assertEquals(eventStoreRecordImpl, EventStoreDAO.delete(entityManager, scopeId, eventId));
try {
EventStoreDAO.delete(null, scopeId, eventId);
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected", nullPointerException.toString(), e.toString());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;

import org.eclipse.kapua.service.authorization.group.Group;
import org.eclipse.kapua.service.authorization.group.GroupCreator;
import org.eclipse.kapua.service.authorization.group.GroupListResult;
Expand All @@ -34,10 +33,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand Down Expand Up @@ -200,8 +199,8 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(GroupImpl.class, groupId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", GroupDAO.find(entityManager, scopeId, groupId) instanceof Group);
Assert.assertNull("Null expected.", GroupDAO.find(entityManager, scopeId, groupId).getScopeId());
Assert.assertTrue("True expected.", GroupDAO.find(entityManager, KapuaId.ANY, groupId) instanceof Group);
Assert.assertNull("Null expected.", GroupDAO.find(entityManager, scopeId, groupId));
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -311,6 +310,8 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(GroupImpl.class, groupId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", GroupDAO.delete(entityManager, scopeId, groupId) instanceof Group);
}

Expand Down

0 comments on commit 1d3ea75

Please sign in to comment.