Skip to content

Commit

Permalink
Increase Unit test code coverage (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
hokwaichan authored Oct 13, 2023
1 parent d1ee03e commit 6fa7b4d
Show file tree
Hide file tree
Showing 42 changed files with 1,335 additions and 183 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
20 changes: 10 additions & 10 deletions src/main/java/edu/hawaii/its/api/service/UpdateMemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ 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 All @@ -137,8 +137,8 @@ public GroupingMoveMemberResult addIncludeMember(String currentUser, String grou
groupPathService.checkPath(groupingPath);
checkIfOwnerOrAdminUser(currentUser, groupingPath);
String validIdentifier = subjectService.getValidUhUuid(uhIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.INCLUDE.value(), groupingPath + GroupType.EXCLUDE.value(),
validIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.INCLUDE.value(),
groupingPath + GroupType.EXCLUDE.value(), validIdentifier);
}

public GroupingMoveMembersResult addExcludeMembers(String currentUser, String groupingPath,
Expand Down Expand Up @@ -170,8 +170,8 @@ public GroupingMoveMemberResult addExcludeMember(String currentUser, String grou
groupPathService.checkPath(groupingPath);
checkIfOwnerOrAdminUser(currentUser, groupingPath);
String validIdentifier = subjectService.getValidUhUuid(uhIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.EXCLUDE.value(), groupingPath + GroupType.INCLUDE.value(),
validIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.EXCLUDE.value(),
groupingPath + GroupType.INCLUDE.value(), validIdentifier);
}

public GroupingRemoveResults removeIncludeMembers(String currentUser, String groupingPath,
Expand Down Expand Up @@ -209,16 +209,16 @@ 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) {
log.info(String.format("optOut; currentUser: %s; groupingPath: %s; uhIdentifier %s;",
currentUser, groupingPath, uhIdentifier));
checkIfSelfOptOrAdmin(currentUser, uhIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.EXCLUDE.value(), groupingPath + GroupType.INCLUDE.value(),
uhIdentifier);
return moveGroupMember(currentUser, groupingPath + GroupType.EXCLUDE.value(),
groupingPath + GroupType.INCLUDE.value(), uhIdentifier);
}

public GroupingRemoveResults removeFromGroups(String currentUser, String uhIdentifier, List<String> groupPaths) {
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,13 @@ 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,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 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,17 @@
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,17 @@
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,17 @@
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,17 @@
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());
}

}
Loading

0 comments on commit 6fa7b4d

Please sign in to comment.