Skip to content

Commit

Permalink
SHIBUI-2024
Browse files Browse the repository at this point in the history
slight refactoring (test cleanup)
  • Loading branch information
chasegawa committed Aug 18, 2021
1 parent b68c821 commit d941498
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package edu.internet2.tier.shibboleth.admin.ui.security.repository

import javax.persistence.EntityManager
import javax.transaction.Transactional

import edu.internet2.tier.shibboleth.admin.ui.configuration.InternationalizationConfiguration
import edu.internet2.tier.shibboleth.admin.ui.security.model.Group
import edu.internet2.tier.shibboleth.admin.ui.security.model.Ownership
import edu.internet2.tier.shibboleth.admin.ui.security.model.listener.GroupUpdatedEntityListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Profile
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.test.annotation.Rollback
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration

import edu.internet2.tier.shibboleth.admin.ui.configuration.InternationalizationConfiguration
import edu.internet2.tier.shibboleth.admin.ui.security.model.Group
import edu.internet2.tier.shibboleth.admin.ui.security.model.Ownership
import edu.internet2.tier.shibboleth.admin.ui.security.model.listener.GroupUpdatedEntityListener
import spock.lang.Specification

import javax.transaction.Transactional

/**
* Tests to validate the repo and model for groups
* @author chasegawa
Expand All @@ -26,7 +27,7 @@ import spock.lang.Specification
@ContextConfiguration(classes=[InternationalizationConfiguration, LocalConfig])
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"])
@EntityScan("edu.internet2.tier.shibboleth.admin.ui")
@ActiveProfiles("test")
@ActiveProfiles(["test","gr-tests"])
class GroupsRepositoryTests extends Specification {
@Autowired
GroupsRepository groupsRepo
Expand Down Expand Up @@ -95,8 +96,8 @@ class GroupsRepositoryTests extends Specification {
it.resourceId = "g1"
it
}
def Group savedGroup = groupsRepo.saveAndFlush(group)
def Collection all = ownershipRepository.findAllByOwner(savedGroup)
Group savedGroup = groupsRepo.saveAndFlush(group)
Collection all = ownershipRepository.findAllByOwner(savedGroup)

then:
all.size() == 3
Expand Down Expand Up @@ -131,7 +132,7 @@ class GroupsRepositoryTests extends Specification {
def gList = groupsRepo.findAll()
gList.size() == 1
def groupFromDb = gList.get(0).asType(Group)
groupFromDb.equals(group) == true
groupFromDb.equals(group)

// fetch checks
groupsRepo.findByResourceId("not an id") == null
Expand All @@ -158,7 +159,7 @@ class GroupsRepositoryTests extends Specification {

then:
// Missing non-nullable field (name) should thrown error
final def exception = thrown(org.springframework.dao.DataIntegrityViolationException)
final def exception = thrown(DataIntegrityViolationException)
}

@Rollback
Expand Down Expand Up @@ -212,13 +213,14 @@ class GroupsRepositoryTests extends Specification {
groupsRepo.findAll().size() == 0

when:
def nothingThere = groupsRepo.findByResourceId(null);
def nothingThere = groupsRepo.findByResourceId(null)

then:
nothingThere == null
}

@org.springframework.boot.test.context.TestConfiguration
@TestConfiguration
@Profile("gr-tests")
static class LocalConfig {
@Bean
GroupUpdatedEntityListener groupUpdatedEntityListener(OwnershipRepository repo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class OwnershipRepositoryTests extends Specification {
@Rollback
def "test clearUsersGroups"() {
when: "remove entries where the user is the owned object of a group"
repo.clearUsersGroups("aaa");
repo.clearUsersGroups("aaa")
def result = repo.findAllGroupsForUser("aaa")

then:
Expand All @@ -102,7 +102,7 @@ class OwnershipRepositoryTests extends Specification {
}

when: "remove entries where the user is the owned object of groups"
repo.clearUsersGroups("ccc");
repo.clearUsersGroups("ccc")
result = repo.findAllGroupsForUser("ccc")

then:
Expand All @@ -113,9 +113,8 @@ class OwnershipRepositoryTests extends Specification {
def "test deleteEntriesForOwnedObject"() {
when: "remove entries where the user is the owned object of a group"
repo.deleteEntriesForOwnedObject(new Ownable() {
public String getObjectId() { return "aaa" }

public OwnableType getOwnableType() { OwnableType.USER }
String getObjectId() { return "aaa" }
OwnableType getOwnableType() { OwnableType.USER }
})
def result = repo.findAllGroupsForUser("aaa")

Expand All @@ -136,10 +135,9 @@ class OwnershipRepositoryTests extends Specification {

when: "remove entries where the user is the owned object of groups"
repo.deleteEntriesForOwnedObject(new Ownable() {
public String getObjectId() { return "ccc" }

public OwnableType getOwnableType() { OwnableType.USER }
});
String getObjectId() { return "ccc" }
OwnableType getOwnableType() { OwnableType.USER }
})
result = repo.findAllGroupsForUser("ccc")

then:
Expand All @@ -153,9 +151,8 @@ class OwnershipRepositoryTests extends Specification {
userIds.add("bbb")
userIds.add("ccc")
def result = repo.findUsersByOwner(new Owner() {
public String getOwnerId() { return "g1" }

public OwnerType getOwnerType() { OwnerType.GROUP }
String getOwnerId() { return "g1" }
OwnerType getOwnerType() { OwnerType.GROUP }
})

then:
Expand All @@ -167,9 +164,8 @@ class OwnershipRepositoryTests extends Specification {

when:
result = repo.findUsersByOwner(new Owner() {
public String getOwnerId() { return "aaa" }

public OwnerType getOwnerType() { return OwnerType.USER }
String getOwnerId() { return "aaa" }
OwnerType getOwnerType() { return OwnerType.USER }
})

then:
Expand All @@ -196,8 +192,8 @@ class OwnershipRepositoryTests extends Specification {
groupIds.add("g1")
groupIds.add("g2")
def result = repo.findOwnableObjectOwners(new Ownable() {
public String getObjectId() { return "ccc" }
public OwnableType getOwnableType() { return OwnableType.USER }
String getObjectId() { return "ccc" }
OwnableType getOwnableType() { return OwnableType.USER }
})

then:
Expand Down Expand Up @@ -242,9 +238,8 @@ class OwnershipRepositoryTests extends Specification {
userIds.add("bbb")
userIds.add("ccc")
def result = repo.findAllByOwner(new Owner() {
public String getOwnerId() { return "g1" }

public OwnerType getOwnerType() { return OwnerType.GROUP }
String getOwnerId() { return "g1" }
OwnerType getOwnerType() { return OwnerType.GROUP }
})

then:
Expand All @@ -256,9 +251,8 @@ class OwnershipRepositoryTests extends Specification {

when: "Find all items owned by user aaa"
result = repo.findAllByOwner(new Owner() {
public String getOwnerId() { return "aaa" }

public OwnerType getOwnerType() { return OwnerType.USER }
String getOwnerId() { return "aaa" }
OwnerType getOwnerType() { return OwnerType.USER }
})

then:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import spock.lang.Specification
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"])
@EntityScan("edu.internet2.tier.shibboleth.admin.ui")
@DirtiesContext
@ActiveProfiles(["local"])
@ActiveProfiles(["gs-test"])
class GroupServiceTests extends Specification {
@Autowired
GroupServiceForTesting groupService
Expand Down Expand Up @@ -126,7 +126,7 @@ class GroupServiceTests extends Specification {
}

@TestConfiguration
@Profile("local")
@Profile("gs-test")
static class LocalConfig {
@Bean
GroupServiceForTesting groupServiceForTesting(GroupsRepository repo, OwnershipRepository ownershipRepository) {
Expand Down
Loading

0 comments on commit d941498

Please sign in to comment.