Skip to content

Commit

Permalink
Increase Unit test code coverage
Browse files Browse the repository at this point in the history
cover exceptions begin convering JsonUtil

Added test for describeTo

Added test for toLocalDate

Added assertDoesNotThrow for functions to handle exception

Added test for uhCasAttributes default constructor

Added toString test for Membership class (type)

Added test for person class subject constructor (type)

Added test for group class isMember (type)

Added tests for GroupingsServiceResultException class constructor, getGsr and setGsr (type)

Added test for GroupingMember

Added test for GroupingsUpdatedAttributesResult  (groupings)

Added test for GroupingGroupsMembers (groupings)

Added test for GroupingSyncDestination (groupings)

Added test for hasUHAttributes (wrapper -> SubjectTest)

Added test for getBoolean (Wrapper -> ResultsTest)

Added test for isTimeUpdated (Wrapper -> UpdatedTimestampResult)

Create AttributeAssignValueResultTest and add tests

Refactor construction to constructor

Added more tests for AssignAttributesResultsTest

Added nullResultCode (wrapper -> UpdatedTimestampResult)

Added null test for ParentGroupingPath (service → PathFilter)

Begin covering MemberService

Cover some test in GroupingAssignmentServiceTest, and MemberServiceTest

Remove unused functions

Cover JavaVersionChecker

Implement AsyncJobsManagerTest and cover (Service)

FIx import

Implement ExecutorServiceTest and add tests

Add addAdminTest

Implement addOwnershipsTest

Implement removeAdminTest

Implement removeOwnershipsTest and addIncludeMembersTest

Implemented addExcludeMembersTest, removeIncludeMembersTest, removeIncludeMemberTest, removeExcludeMembersTest, removeExcludeMemberTest, optInTest, and optOutTest

Removed getGroupingTest

Increase Unit test code coverage

cover exceptions begin convering JsonUtil

Added test for describeTo

Added test for toLocalDate

Added assertDoesNotThrow for functions to handle exception

Added test for uhCasAttributes default constructor

Added toString test for Membership class (type)

Added test for person class subject constructor (type)

Added test for group class isMember (type)

Added tests for GroupingsServiceResultException class constructor, getGsr and setGsr (type)

Added test for GroupingMember

Added test for GroupingsUpdatedAttributesResult  (groupings)

Added test for GroupingGroupsMembers (groupings)

Added test for GroupingSyncDestination (groupings)

Added test for hasUHAttributes (wrapper -> SubjectTest)

Added test for getBoolean (Wrapper -> ResultsTest)

Added test for isTimeUpdated (Wrapper -> UpdatedTimestampResult)

Create AttributeAssignValueResultTest and add tests

Refactor construction to constructor

Added more tests for AssignAttributesResultsTest

Added nullResultCode (wrapper -> UpdatedTimestampResult)

Added null test for ParentGroupingPath (service → PathFilter)

Begin covering MemberService

Cover some test in GroupingAssignmentServiceTest, and MemberServiceTest

Remove unused functions

Cover JavaVersionChecker

Implement AsyncJobsManagerTest and cover (Service)

FIx import

Implement ExecutorServiceTest and add tests

Add addAdminTest

Implement addOwnershipsTest

Implement removeAdminTest

Implement removeOwnershipsTest and addIncludeMembersTest

Implemented addExcludeMembersTest, removeIncludeMembersTest, removeIncludeMemberTest, removeExcludeMembersTest, removeExcludeMemberTest, optInTest, and optOutTest

Fixed conflict

Removed unused import
  • Loading branch information
hokwaichan committed Oct 6, 2023
1 parent 1373ced commit ea29a21
Show file tree
Hide file tree
Showing 39 changed files with 1,237 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
@Component
public class PasswordFoundAnalyzer extends AbstractFailureAnalyzer<PasswordFoundException> {

@Override protected FailureAnalysis analyze(Throwable rootFailure, PasswordFoundException cause) {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, PasswordFoundException cause) {
String description = "A password(s) was found.";

String action = "Delete the password(s) in the following location(s):"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,26 @@ public class GroupingAssignmentService {
@Autowired
private GroupingsService groupingsService;

//returns an adminLists object containing the list of all admins and all groupings
public AdminListsHolder adminsGroupings(String adminUsername) {
logger.info(String.format("adminsGroupings; adminUsername: %s;", adminUsername));
if (!memberService.isAdmin(adminUsername)) {

/**
* returns an adminLists object containing the list of all admins and all groupings
*/
public AdminListsHolder adminsGroupings(String adminUhIdentifier) {
logger.info(String.format("adminsGroupings; adminUhIdentifier: %s;", adminUhIdentifier));
if (!memberService.isAdmin(adminUhIdentifier)) {
throw new AccessDeniedException();
}
AdminListsHolder adminListsHolder = new AdminListsHolder();

List<String> adminGrouping = Arrays.asList(GROUPING_ADMINS);
Group admin = getMembers(adminUsername, adminGrouping).get(GROUPING_ADMINS);
Group admin = getMembers(adminUhIdentifier, adminGrouping).get(GROUPING_ADMINS);
adminListsHolder.setAllGroupingPaths(groupingsService.allGroupingPaths());
adminListsHolder.setAdminGroup(admin);
return adminListsHolder;
}

//returns a group from grouper or the database
/**
* returns a group from grouper or the database
*/
public Map<String, Group> getMembers(String ownerUsername, List<String> groupPaths) {
GetMembersResults getMembersResults =
grouperApiService.getMembersResults(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public GroupingMoveMembersResult addIncludeMembers(String currentUser, String gr
groupPathService.checkPath(groupingPath);
checkIfOwnerOrAdminUser(currentUser, groupingPath);
List<String> validIdentifiers = subjectService.getValidUhUuids(uhIdentifiers);
return moveGroupMembers(currentUser, groupingPath + GroupType.INCLUDE.value(), groupingPath + GroupType.EXCLUDE.value(),
validIdentifiers);
return moveGroupMembers(currentUser, groupingPath + GroupType.INCLUDE.value(), groupingPath + GroupType.EXCLUDE.value(), validIdentifiers);
}

@Async
Expand Down Expand Up @@ -148,8 +147,7 @@ public GroupingMoveMembersResult addExcludeMembers(String currentUser, String gr
groupPathService.checkPath(groupingPath);
checkIfOwnerOrAdminUser(currentUser, groupingPath);
List<String> validIdentifiers = subjectService.getValidUhUuids(uhIdentifiers);
return moveGroupMembers(currentUser, groupingPath + GroupType.EXCLUDE.value(),
groupingPath + GroupType.INCLUDE.value(), validIdentifiers);
return moveGroupMembers(currentUser, groupingPath + GroupType.EXCLUDE.value(), groupingPath + GroupType.INCLUDE.value(), validIdentifiers);
}

@Async
Expand Down Expand Up @@ -209,8 +207,7 @@ public GroupingMoveMemberResult optIn(String currentUser, String groupingPath, S
log.info(String.format("optIn; currentUser: %s; groupingPath: %s; uhIdentifier %s;",
currentUser, groupingPath, uhIdentifier));
checkIfSelfOptOrAdmin(currentUser, uhIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.INCLUDE.value(), groupingPath + GroupType.EXCLUDE.value(),
uhIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.INCLUDE.value(), groupingPath + GroupType.EXCLUDE.value(), uhIdentifier);
}

public GroupingMoveMemberResult optOut(String currentUser, String groupingPath, String uhIdentifier) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/hawaii/its/api/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void printJson(Object obj) {
logger.error("Error: " + e);
}
}

public static void prettyPrint(Object object) {
try {
String json = new ObjectMapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,12 @@ public void misc() {

assertThat(attributes.toString(), containsString("uid=duckart"));
}

@Test
public void uhCasAttributesTest() {
UhCasAttributes attributes = new UhCasAttributes();
assertThat(attributes.getUsername(), is(""));
assertThat(attributes.getUhUuid(), is(""));
assertThat(attributes.getUid(), is(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package edu.hawaii.its.api.exception;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

Expand All @@ -13,33 +12,33 @@ class AccessDeniedExceptionTest {
public void construction() {
AccessDeniedException exception = new AccessDeniedException();
assertNotNull(exception);
assertThat(exception.getMessage(), equalTo("Insufficient Privileges"));
assertEquals("Insufficient Privileges", exception.getMessage());

exception = new AccessDeniedException("fail");
assertNotNull(exception);
assertThat(exception.getMessage(), equalTo("fail"));
assertEquals("fail", exception.getMessage());

Throwable throwable = new Throwable();
assertNotNull(throwable);
assertNull(throwable.getMessage());
exception = new AccessDeniedException(throwable);
assertNotNull(exception);
assertThat(exception.getMessage(), equalTo(throwable.toString()));
assertThat(exception.getMessage(), equalTo("java.lang.Throwable"));
assertThat(exception.getCause(), equalTo(throwable));
assertEquals(throwable.toString(), exception.getMessage());
assertEquals("java.lang.Throwable", exception.getMessage());
assertEquals(throwable, exception.getCause());

throwable = new Throwable("fail");
assertNotNull(throwable);
assertThat(throwable.getMessage(), equalTo("fail"));
assertEquals("fail", throwable.getMessage());
exception = new AccessDeniedException(throwable);
assertNotNull(exception);
assertThat(exception.getMessage(), equalTo(throwable.toString()));
assertThat(exception.getMessage(), equalTo("java.lang.Throwable: fail"));
assertThat(exception.getCause(), equalTo(throwable));
assertEquals(throwable.toString(), exception.getMessage());
assertEquals("java.lang.Throwable: fail", exception.getMessage());
assertEquals(throwable, exception.getCause());

exception = new AccessDeniedException("fail", throwable);
assertNotNull(exception);
assertThat(exception.getMessage(), equalTo("fail"));
assertThat(exception.getCause(), equalTo(throwable));
assertEquals("fail", exception.getMessage());
assertEquals(throwable, exception.getCause());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.hawaii.its.api.exception;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class InvalidGroupPathExceptionTest {

@Test
public void construction() {
InvalidGroupPathException exception = new InvalidGroupPathException("fail");
assertNotNull(exception);
assertEquals("404 NOT_FOUND \"fail\"", exception.getMessage());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.hawaii.its.api.exception;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class JavaVersionExceptionTest {

@Test
public void construction() {
JavaVersionException exception = new JavaVersionException("fail");
assertNotNull(exception);
assertEquals("fail", exception.getMessage());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.hawaii.its.api.exception;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

public class JavaVersionFailureAnalyzerTest {

@Test
public void analyze() {
JavaVersionFailureAnalyzer javaVersionFailureAnalyzer = new JavaVersionFailureAnalyzer();
Throwable throwable = new Throwable();
JavaVersionException exception = new JavaVersionException("");
assertNotNull(javaVersionFailureAnalyzer.analyze(throwable, exception));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.hawaii.its.api.exception;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

public class PasswordFoundAnalyzerTest {

@Test
public void analyze() {
PasswordFoundAnalyzer passwordFoundAnalyzer = new PasswordFoundAnalyzer();
Throwable throwable = new Throwable();
PasswordFoundException exception = new PasswordFoundException("");
assertNotNull(passwordFoundAnalyzer.analyze(throwable, exception));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.hawaii.its.api.exception;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
public class PasswordFoundExceptionTest {

@Test
public void construction() {
PasswordFoundException exception = new PasswordFoundException("location");
assertNotNull(exception);
assertEquals("location", exception.getMessage());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.hawaii.its.api.exception;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
public class PatternFoundExceptionTest {

@Test
public void construction() {
PatternFoundException exception = new PatternFoundException("fail");
assertNotNull(exception);
assertEquals("fail", exception.getMessage());

}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package edu.hawaii.its.api.exception;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class UhMemberNotFoundExceptionTest {

@Test
public void construction() {
UhMemberNotFoundException ex = new UhMemberNotFoundException("fail");
assertNotNull(ex);
assertThat(ex.getMessage(), equalTo("404 NOT_FOUND \"fail\""));
UhMemberNotFoundException exception = new UhMemberNotFoundException("fail");
assertNotNull(exception);
assertEquals("404 NOT_FOUND \"fail\"", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,18 @@ public void emptyResult() {
assertNotNull(groupingGroupsMembers.getAllMembers().getMembers());
assertTrue(groupingGroupsMembers.getAllMembers().getMembers().isEmpty());
}

@Test
public void construction() {
GroupingGroupsMembers groupingGroupsMembers = new GroupingGroupsMembers();
assertEquals("", groupingGroupsMembers.getGroupPath());
assertEquals("", groupingGroupsMembers.getResultCode());
assertNotNull(groupingGroupsMembers.getAllMembers());
assertFalse(groupingGroupsMembers.isBasis());
assertFalse(groupingGroupsMembers.isInclude());
assertFalse(groupingGroupsMembers.isExclude());
assertFalse(groupingGroupsMembers.isOwners());
assertEquals(Integer.valueOf(0), groupingGroupsMembers.getPageNumber());
assertTrue(groupingGroupsMembers.isPaginationComplete());
}
}
49 changes: 49 additions & 0 deletions src/test/java/edu/hawaii/its/api/groupings/GroupingMemberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.hawaii.its.api.groupings;

import edu.hawaii.its.api.wrapper.Subject;
import edu.internet2.middleware.grouperClient.ws.beans.WsSubject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import edu.hawaii.its.api.util.JsonUtil;

import java.io.FileInputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class GroupingMemberTest {
private static Properties properties;

@BeforeAll
public static void beforeAll() throws Exception {
Path path = Paths.get("src/test/resources");
Path file = path.resolve("grouper.test.properties");
properties = new Properties();
properties.load(new FileInputStream(file.toFile()));
}

@Test
public void construction() {
String json = properties.getProperty("ws.subject.success.uid");
WsSubject wsSubject = JsonUtil.asObject(json, WsSubject.class);
Subject subject = new Subject(wsSubject);
GroupingGroupMember groupingGroupMember = new GroupingGroupMember(subject);
GroupingMember groupingMember = new GroupingMember(groupingGroupMember, "Include");
assertNotNull(groupingMember);

assertEquals("Testf-iwt-a TestIAM-staff", groupingMember.getName());
assertEquals("99997010", groupingMember.getUhUuid());
assertEquals("testiwta", groupingMember.getUid());
assertEquals("Include", groupingMember.getWhereListed());

groupingMember = new GroupingMember();
assertNotNull(groupingMember);
assertNotNull(groupingMember.getName());
assertNotNull(groupingMember.getUhUuid());
assertNotNull(groupingMember.getUid());
assertNotNull(groupingMember.getWhereListed());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.hawaii.its.api.groupings;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class GroupingSyncDestinationTest {

@Test
public void test() {
GroupingSyncDestination dest = new GroupingSyncDestination();
dest.setDescription("description");
dest.setName("name");
dest.setHidden(false);
dest.setSynced(true);
dest.setTooltip("tooltip");

assertEquals("description", dest.getDescription());
assertEquals("name", dest.getName());
assertEquals(false, dest.getHidden());
assertEquals(true, dest.getSynced());
assertEquals("tooltip", dest.getTooltip());
}
}
Loading

0 comments on commit ea29a21

Please sign in to comment.