@Test public void testDeleteRole() { // Setup role Role role = new Role(); role.setName("TestRole"); // Save role, verify has ID value after save assertNull(role.getId()); // Null before save roleRepository.save(role); assertNotNull(role.getId()); // Not null after save // Fetch from database Role fetchedRole = roleRepository.findOne(role.getId()); // Should not be null assertNotNull(fetchedRole); // Delete role from database roleRepository.delete(fetchedRole.getId()); // Fetch from database (should not exist anymore) fetchedRole = roleRepository.findOne(role.getId()); // Should be null assertNull(fetchedRole); }
@Test public void testSaveRole() { // Setup role Role role = new Role(); role.setName("TestRole"); // Save role, verify has ID value after save assertNull(role.getId()); // Null before save roleRepository.save(role); assertNotNull(role.getId()); // Not null after save // Fetch from database Role fetchedRole = roleRepository.findOne(role.getId()); // Should not be null assertNotNull(fetchedRole); // Should equals assertEquals(role.getId(), fetchedRole.getId()); assertEquals(role.getName(), fetchedRole.getName()); // Update description and save fetchedRole.setName("NewRoleName"); roleRepository.save(fetchedRole); // Get from database, should be updated Role fetchedUpdatedRole = roleRepository.findOne(fetchedRole.getId()); assertEquals(fetchedRole.getName(), fetchedUpdatedRole.getName()); // Verify count of roles in database long roleCount = roleRepository.count(); assertEquals(roleCount, origRoleRepositorySize + 1); // One role has been added to the database // Get all roles, list should only have one more then initial value Iterable<Role> roles = roleRepository.findAll(); int count = 0; for (Role p : roles) { count++; } // There are originally 'origRoleRepositorySize' roles declared in the database (+1 has been // added in this test) assertEquals(count, origRoleRepositorySize + 1); }