Commit 4a825e27 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Added some tests and other minor changes

parent 2e517208
......@@ -160,8 +160,6 @@ public class InvitedRegistrationManager extends UserAwareComponent {
}
invitedRegistration.setUserId(getCurrentUserId());
// FIXME (workaround): separated update for user and done in order to use triggers
invitedRegistrationDAO.setRegistrationUser(invitedRegistration);
invitedRegistrationDAO.setRegistrationDone(invitedRegistration);
}
......
......@@ -42,15 +42,17 @@ public class GroupsDAO {
groupsHook.beforeCreate(group);
}
String sql = "INSERT INTO gms_group (id, name, path, is_leaf, created_by) VALUES (?, ?, ?, ?, ?)";
String sql = "INSERT INTO gms_group (id, name, path, is_leaf, locked, created_by) VALUES (?, ?, ?, ?, ?, ?)";
jdbcTemplate.update(conn -> {
int i = 0;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, group.getId());
ps.setString(2, group.getName());
ps.setObject(3, group.getPath(), Types.OTHER);
ps.setBoolean(4, group.isLeaf());
ps.setString(5, group.getCreatedBy());
ps.setString(++i, group.getId());
ps.setString(++i, group.getName());
ps.setObject(++i, group.getPath(), Types.OTHER);
ps.setBoolean(++i, group.isLeaf());
ps.setBoolean(++i, group.isLocked());
ps.setString(++i, group.getCreatedBy());
return ps;
});
......@@ -63,14 +65,16 @@ public class GroupsDAO {
groupsHook.beforeUpdate(group);
}
String sql = "UPDATE gms_group SET name = ?, path = ?, is_leaf = ? WHERE id = ?";
String sql = "UPDATE gms_group SET name = ?, path = ?, is_leaf = ?, locked = ? WHERE id = ?";
jdbcTemplate.update(conn -> {
int i = 0;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, group.getName());
ps.setObject(2, group.getPath(), Types.OTHER);
ps.setBoolean(3, group.isLeaf());
ps.setString(4, group.getId());
ps.setString(++i, group.getName());
ps.setObject(++i, group.getPath(), Types.OTHER);
ps.setBoolean(++i, group.isLeaf());
ps.setBoolean(++i, group.isLocked());
ps.setString(++i, group.getId());
return ps;
});
......@@ -97,12 +101,7 @@ public class GroupsDAO {
return ps;
}, resultSet -> {
if (resultSet.next()) {
GroupEntity group = new GroupEntity();
group.setId(resultSet.getString("id"));
group.setName(resultSet.getString("name"));
group.setPath(resultSet.getString("path"));
group.setLeaf(resultSet.getBoolean("is_leaf"));
group.setLocked(resultSet.getBoolean("locked"));
GroupEntity group = getGroupFromResultSet(resultSet);
return Optional.of(group);
}
return Optional.empty();
......
......@@ -100,9 +100,9 @@ public class InvitedRegistrationDAO {
return Optional.ofNullable(registration);
}
public void setRegistrationUser(InvitedRegistration invitedRegistration) {
public void setRegistrationDone(InvitedRegistration invitedRegistration) {
String sql = "UPDATE invited_registration_request SET \"user\" = ? WHERE id = ?";
String sql = "UPDATE invited_registration_request SET \"user\" = ?, done = true WHERE id = ?";
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
......@@ -112,17 +112,6 @@ public class InvitedRegistrationDAO {
});
}
public void setRegistrationDone(InvitedRegistration invitedRegistration) {
String sql = "UPDATE invited_registration_request SET done = true WHERE id = ?";
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, invitedRegistration.getId());
return ps;
});
}
/**
* Called before deleting a group.
*/
......
......@@ -11,6 +11,7 @@ import it.inaf.ia2.gms.persistence.LoggingDAO;
import it.inaf.ia2.gms.persistence.MembershipsDAO;
import it.inaf.ia2.gms.persistence.PermissionsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
......@@ -70,6 +71,7 @@ public class GroupsService {
group.setPath(path);
group.setLeaf(leaf);
group.setCreatedBy(createdBy);
group.setCreationTime(new Date());
groupsDAO.createGroup(group);
loggingDAO.logAction("Added group: parent_path=" + parent.getPath() + ", group_name=" + groupName);
......
......@@ -98,10 +98,6 @@ public class GroupsDAOTest {
assertTrue(group.isPresent());
assertEquals(lbtInaf, group.get());
// Find unexisting group
group = dao.findGroupById("not-found");
assertFalse(group.isPresent());
// Sub list
List<GroupEntity> groups = dao.getDirectSubGroups(root.getPath());
assertEquals(2, groups.size());
......@@ -146,7 +142,7 @@ public class GroupsDAOTest {
assertTrue(childrenMap.get(lbt.getId()));
assertFalse(childrenMap.get(tng.getId()));
// Rename
// Update
String newName = "renamed";
tng.setName(newName);
dao.updateGroup(tng);
......@@ -196,6 +192,46 @@ public class GroupsDAOTest {
return UUID.randomUUID().toString().replaceAll("-", "");
}
@Test
public void testFields() {
GroupEntity group = new GroupEntity();
group.setId("group_id");
group.setName("group_name");
group.setPath("group_path");
group.setLeaf(true);
group.setLocked(true);
group.setCreatedBy("creator_id");
dao.createGroup(group);
GroupEntity savedGroup = dao.findGroupById("group_id").get();
assertEquals("group_id", savedGroup.getId());
assertEquals("group_name", savedGroup.getName());
assertEquals("group_path", savedGroup.getPath());
assertTrue(savedGroup.isLeaf());
assertTrue(savedGroup.isLocked());
assertEquals("creator_id", savedGroup.getCreatedBy());
group.setName("new_name");
group.setLeaf(false);
group.setLocked(false);
dao.updateGroup(group);
savedGroup = dao.findGroupById("group_id").get();
assertEquals("new_name", savedGroup.getName());
assertFalse(savedGroup.isLeaf());
assertFalse(savedGroup.isLocked());
}
@Test
public void testGetInexistentGroupById() {
assertTrue(dao.findGroupById("not-found").isEmpty());
}
@Test
public void testGroupCompleteNamesEmptyInput() {
assertTrue(dao.getGroupCompleteNamesFromId(new HashSet<>()).isEmpty());
......
package it.inaf.ia2.gms.service;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.InvitedRegistrationDAO;
import it.inaf.ia2.gms.persistence.LoggingDAO;
import it.inaf.ia2.gms.persistence.MembershipsDAO;
import it.inaf.ia2.gms.persistence.PermissionsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.argThat;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class GroupsServiceTest {
@Mock
private GroupsDAO groupsDAO;
@Mock
private PermissionsDAO permissionsDAO;
@Mock
private MembershipsDAO membershipsDAO;
@Mock
private InvitedRegistrationDAO invitedRegistrationDAO;
@Mock
private LoggingDAO loggingDAO;
@InjectMocks
private GroupsService groupsService;
private final GroupEntity root = group("ROOT", "ROOT", "");
@Test
public void testAddGroup() {
GroupEntity group1 = groupsService.addGroup(root, "group1", false, "creator_id");
verify(groupsDAO, times(1)).createGroup(argThat(g -> "group1".equals(g.getName())));
assertNotNull(group1.getId());
assertEquals("group1", group1.getName());
assertFalse(group1.isLeaf());
assertEquals("creator_id", group1.getCreatedBy());
assertNotNull(group1.getCreationTime());
GroupEntity group2 = groupsService.addGroup(group1, "group2", true, "creator_id");
verify(groupsDAO, times(1)).createGroup(argThat(g -> "group2".equals(g.getName())));
assertNotNull(group2.getId());
assertEquals("group2", group2.getName());
assertTrue(group2.isLeaf());
assertEquals(group1.getId() + "." + group2.getId(), group2.getPath());
}
private GroupEntity group(String id, String name, String path) {
GroupEntity group = new GroupEntity();
group.setId(id);
group.setName(name);
group.setPath(path);
return group;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment