Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics #65

Merged
merged 10 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/webapp/app/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ h2 {
height: 30px;
}
#personal-block {
height: 10px;
height: 180px;
}
#form {
text-align: center;
Expand Down Expand Up @@ -112,4 +112,10 @@ a {
#rbtnUser {
margin-right: 100px;
}
div#metrics {
float: right;
margin-right: 40px;
font-family: "Segoe UI", Arial, sans-serif;
margin-bottom: 10px;
}

1 change: 0 additions & 1 deletion src/main/webapp/app/findgoals.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<td>{{daysLeft}}</td>
</tr>
</table>

</div>
</body>
</html>
29 changes: 27 additions & 2 deletions src/main/webapp/app/js/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,35 @@ function switchPersonalData() {
block.classList.add("w3-hide");
}
}

function displayError(response, expected) {
if (response.status !== expected) {
alert("Something went wrong! error: " + response.status.toString());
history.go(-1);
}
}
}
function generateMetrics(goals) {
var completed = 0;
var overdue = 0;
var open = 0;
for (var i = 0; i < goals.length; i++) {
var goal = goals[i];
switch (goal.goalStatus) {
case "ACHIEVED":
completed++;
break;
case "OVERDUE":
overdue++;
break;
default:
open++;
}
}
var statisticsTable = "<table>" +
"<tr><td><h4>Statistics</h4></td></tr>" +
"<tr><td>" + "Goals Total: " + goals.length + "</td></tr>" +
"<tr><td>" + "Goals Achieved: " + completed + "</td></tr>" +
"<tr><td>" + "Goals Overdue: " + overdue + "</td></tr>" +
"<tr><td>" + "Goals Open: " + open + "</td></tr>" +
"</table>";
document.getElementById("metrics").innerHTML = statisticsTable;
}
3 changes: 2 additions & 1 deletion src/main/webapp/app/js/start-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ function showUserGoals() {
};
}
w3DisplayData("goals-list", tabledata);
generateMetrics(goals);
});
}
}
3 changes: 2 additions & 1 deletion src/main/webapp/app/js/user-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function setDataToFields(userDto) {
if (list.length > 0) {
tabledata = {"goals": list};
w3DisplayData("goals-list", tabledata);
generateMetrics(list);
} else {
document.getElementById("hidden").classList.remove("w3-hide");
document.getElementById("goals-list").classList.add("w3-hide");
Expand All @@ -48,4 +49,4 @@ function getQueryVariable(variable) {
}
}
return (false);
}
}
1 change: 1 addition & 0 deletions src/main/webapp/app/start.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</div>
<div id="personal-block" class="w3-hide">
<h3 id="phone_email"></h3>
<div id="metrics"></div>
</div>
<table id="goals-list" class="w3-table-all w3-hoverable">
<tr class="w3-brown">
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/app/user.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</div>
<div id="personal-block" class="w3-hide">
<h3 id="email-and-phone">{{email}} | {{phone}}</h3>
<div id="metrics"></div>
</div>
<p id="hidden" class="w3-hide w3-display-middle w3-xlarge">At the moment this user has no goals.</p>
<table id="goals-list" class="w3-table-all w3-hoverable">
Expand Down
85 changes: 61 additions & 24 deletions src/test/java/lv/ctco/javaschool/goal/boundary/GoalApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void init() {

@Test
@DisplayName("Test getUserById(): returns user if User exists")
void testGetUserByIdReturnsUserDtoIfUserExists() {
void test_GetUserById_ReturnsUserDto_IfUserExists() {
goalList1.add(goal);
UserDto userDto = DtoConverter.convertToUserDto(user1, goalList1);
when(userStore.getUserById(1L))
Expand All @@ -153,15 +153,15 @@ void testGetUserByIdReturnsUserDtoIfUserExists() {

@Test
@DisplayName("Test getUserById(): throws InvalidUserException if user does not exists(wrong id)")
void testGetUserByIdThrowsError() {
void test_GetUserById_ThrowsError() {
when(userStore.getUserById(1L))
.thenReturn(Optional.empty());
assertThrows(InvalidUserException.class, () -> goalApi.getUserById(1L));
}

@Test
@DisplayName("Test findGoalsForCurrentUser(): Current user has No goals returns empty list of dto's")
void testFindGoalsForCurrentUser() {
void test_FindGoalsForCurrentUser() {
when(userStore.getCurrentUser())
.thenReturn(user1);
when(goalStore.getGoalsByUser(user1))
Expand All @@ -171,7 +171,7 @@ void testFindGoalsForCurrentUser() {

@Test
@DisplayName("Test findGoalsForCurrentUser(): Current user has goals returns list of dto's")
void testFindGoalsForCurrentUserToReturnListOfDto() {
void test_FindGoalsForCurrentUser_ToReturnListOfDto() {
goalList1.add(goal);
when(userStore.getCurrentUser())
.thenReturn(user1);
Expand All @@ -184,7 +184,7 @@ void testFindGoalsForCurrentUserToReturnListOfDto() {

@Test
@DisplayName("Test getGoalById(): returns dto of goal by id")
void testGetGoalById() {
void test_GetGoalById() {
when(goalStore.getGoalById(1L))
.thenReturn(java.util.Optional.ofNullable(goal));
assertThat(goalApi.findGoalDtoByGoalId(1L).getId(), is(1L));
Expand All @@ -194,15 +194,15 @@ void testGetGoalById() {

@Test
@DisplayName("Test getGoalById(): throws InvalidGoalException")
void testGetGoalByIdException() {
void test_GetGoalById_Exception() {
when(goalStore.getGoalById(1L))
.thenReturn(java.util.Optional.empty());
assertThrows(InvalidGoalException.class, () -> goalApi.findGoalDtoByGoalId(1L));
}

@Test
@DisplayName("Test saveGoal(): check if persists new Goal")
void testSaveGoal() {
void test_SaveGoal() {
goalFormDto.setDeadline(LocalDate.of(9018, 10, 25));
goalFormDto.setGoalMessage("hi");
goalFormDto.setTags("qwjye|iwefyg|ksdgf");
Expand All @@ -218,15 +218,15 @@ void testSaveGoal() {

@Test
@DisplayName("Test saveGoal(): check if throws exception if empty fields of dto object")
void testSaveGoalException() {
void test_SaveGoal_Exception() {
when(userStore.getCurrentUser())
.thenReturn(user1);
assertThrows(InvalidGoalException.class, () -> goalApi.saveGoal(new GoalFormDto()));
}

@Test
@DisplayName("Test saveGoal(): check if throws exception if empty message inserted")
void testSaveGoalEmptyMessage() {
void test_SaveGoal_EmptyMessage() {
GoalFormDto testGoalFormDto = new GoalFormDto();
when(userStore.getCurrentUser())
.thenReturn(user1);
Expand All @@ -241,7 +241,7 @@ void testSaveGoalEmptyMessage() {

@Test
@DisplayName("Test saveGoal(): check if throws exception if there is invalid deadline date of dto object")
void testSaveGoalInvalidDeadline() {
void test_SaveGoal_InvalidDeadline() {
goalFormDto.setDeadline(LocalDate.of(1,1,1));
goalFormDto.setGoalMessage("hi");
goalFormDto.setTags("qwjye|iwefyg|ksdgf");
Expand All @@ -253,7 +253,7 @@ void testSaveGoalInvalidDeadline() {

@Test
@DisplayName("Test findCommentsForGoalById(): returns Comments dto of goal by id")
void testFindCommentsForGoalById() {
void test_FindCommentsForGoalById() {
comments.add(comment1);
when(goalStore.getGoalById(1L))
.thenReturn(java.util.Optional.ofNullable(goal));
Expand All @@ -266,7 +266,7 @@ void testFindCommentsForGoalById() {

@Test
@DisplayName("Test findCommentsForGoalById(): returns empty Comments dto of goal")
void testFindCommentsForGoalByIdEmptyCommentsDto() {
void test_FindCommentsForGoalById_EmptyCommentsDto() {
when(goalStore.getGoalById(1L))
.thenReturn(java.util.Optional.ofNullable(goal));
when(goalStore.getCommentsForGoal(goal))
Expand All @@ -277,7 +277,7 @@ void testFindCommentsForGoalByIdEmptyCommentsDto() {

@Test
@DisplayName("Test saveCommentsForGoalById(): verify if persists Comments")
void testSaveCommentsForGoalById() {
void test_SaveCommentsForGoalById() {
when(userStore.getCurrentUser())
.thenReturn(user1);
when(goalStore.getGoalById(1L))
Expand All @@ -288,15 +288,15 @@ void testSaveCommentsForGoalById() {

@Test
@DisplayName("Test saveCommentsForGoalById(): verify if throws exception if optional<goal> isEmpty")
void testSaveCommentsForGoalByIdException() {
void test_SaveCommentsForGoalById_Exception() {
when(goalStore.getGoalById(1L))
.thenReturn(Optional.empty());
assertThrows(InvalidGoalException.class, () -> goalApi.saveCommentsForGoalById(1L, msg));
}

@Test
@DisplayName("Test isCurrentUsersGoal(Long goalId): verify if goal is for current user is returned true")
void testIsCurrentUsersGoalForCurrentUser() {
void test_IsCurrentUsersGoal_ForCurrentUser() {
when(userStore.getCurrentUser())
.thenReturn(user1);
when(goalStore.getUnachievedUserGoalById(user1, 1L))
Expand All @@ -306,7 +306,7 @@ void testIsCurrentUsersGoalForCurrentUser() {

@Test
@DisplayName("Test isCurrentUsersGoal(Long goalId): verify if goal is for other user not current, then returned false")
void testIsCurrentUsersGoalForDifferentUser() {
void test_IsCurrentUsersGoal_ForDifferentUser() {
when(userStore.getCurrentUser())
.thenReturn(user2);
when(goalStore.getUnachievedUserGoalById(user2, 1L))
Expand All @@ -329,7 +329,7 @@ void testEditGoalForGoalOwner() {

@Test
@DisplayName("Test editGoal(Long goalId, GoalFormDto newGoalDto): verify that goal is not edited, if user did not create goal")
void testEditGoalForDifferentUser() {
void test_EditGoal_ForDifferentUser() {
String messageBeforeEdit = goal.getGoalMessage();
LocalDate deadlineDateBeforeEdit = goal.getDeadlineDate();
when(userStore.getCurrentUser())
Expand All @@ -344,7 +344,7 @@ void testEditGoalForDifferentUser() {

@Test
@DisplayName("Test editGoal(Long goalId, GoalFormDto newGoalDto): verify that goal is not edited, if goal is achieved")
void testEditGoalForAchievedGoal() {
void test_EditGoal_ForAchievedGoal() {
goal.setStatus(GoalStatus.ACHIEVED);
String messageBeforeEdit = goal.getGoalMessage();
LocalDate deadlineDateBeforeEdit = goal.getDeadlineDate();
Expand All @@ -360,7 +360,7 @@ void testEditGoalForAchievedGoal() {

@Test
@DisplayName("Test editGoal(Long goalId, GoalFormDto newGoalDto): verify that goal status is changed, if overdue goal deadline date is edited")
void testEditGoalForOverdueGoal() {
void test_EditGoal_ForOverdueGoal() {
goal.setStatus(GoalStatus.OVERDUE);
when(userStore.getCurrentUser())
.thenReturn(user1);
Expand All @@ -374,7 +374,7 @@ void testEditGoalForOverdueGoal() {

@Test
@DisplayName("Test editGoal(Long goalId, GoalFormDto newGoalDto): verify that goal is edited, if users new goal is empty")
void testEditGoalWithoutInput() {
void test_EditGoal_WithoutInput() {

when(userStore.getCurrentUser())
.thenReturn(user1);
Expand All @@ -383,15 +383,15 @@ void testEditGoalWithoutInput() {

@Test
@DisplayName("Test editGoal(Long goalId, GoalFormDto newGoalDto): verify that goal is edited, if user input invalid deadline date")
void testEditGoalInvalidDeadline() {
void test_EditGoal_InvalidDeadline() {
goalFormDto.setGoalMessage("123");
goalFormDto.setDeadline(LocalDate.of(1, 1, 1));
assertThrows(ValidationException.class, () -> goalApi.editGoal(1L, goalFormDto));
}

@Test
@DisplayName("Test findAllTags(): verify if throws exception if optional<goal> isEmpty")
void testFindAllTags() {
void test_FindAllTags() {
when(goalStore.getAllTags()).thenReturn(tagList);
List<TagDto> dtoList = goalApi.findAllTags();
assertThat(dtoList.size(), is(tagList.size()));
Expand All @@ -403,7 +403,7 @@ void testFindAllTags() {

@Test
@DisplayName("Test getGoalsByTag(JsonObject searchDto): returns list of GoalDto by Tag")
void testGetGoalsByTag() {
void test_GetGoalsByTag() {
List<Tag> tags = new ArrayList<>();
Tag tag1 = new Tag("test1");
Tag tag2 = new Tag("test2");
Expand All @@ -428,7 +428,7 @@ void testGetGoalsByTag() {

@Test
@DisplayName("Test getUsersByUsername(JsonObject searchDto): returns list of UserSearchDto and calling userStore.getUsersByUsername() method")
void testGetUsersByUsername() {
void test_GetUsersByUsername() {
List<User> users = new ArrayList<>();
Collections.addAll(users, user1, user2);
when(userStore.getUsersByUsername(anyString())).thenReturn(users);
Expand All @@ -452,4 +452,41 @@ private GoalFormDto setupGoalFormDto() {
result.setDeadline(LocalDate.of(2918, 1, 1));
return result;
}

@Test
@DisplayName("test setStatusAchieved: changes status")
void test_SetStatusAchieved() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to put the underscore right after "test" - testSetStatusAchieved is OK.

Long id = goal.getId();
goal.setStatus(GoalStatus.OVERDUE);
when(userStore.getCurrentUser())
.thenReturn(user1);
when(goalStore.getGoalById(id))
.thenReturn(Optional.of(goal));
goalApi.setStatusAchieved(id);
assertThat(goal.getStatus(), is(GoalStatus.ACHIEVED));
}

@Test
@DisplayName("test setStatusAchieved: throws ValidationException wrong user")
void test_SetStatusAchieved_ThrowsException_IfWrongUser() {
Long id = goal.getId();
goal.setStatus(GoalStatus.OVERDUE);
when(userStore.getCurrentUser())
.thenReturn(user2);
when(goalStore.getGoalById(id))
.thenReturn(Optional.of(goal));
assertThrows(ValidationException.class, () -> goalApi.setStatusAchieved(id));
}

@Test
@DisplayName("test setStatusAchieved: throws ValidationException no goal")
void test_SetStatusAchieved_ThrowsException_IfNoGoal() {
Long id = goal.getId();
goal.setStatus(GoalStatus.OPEN);
when(userStore.getCurrentUser())
.thenReturn(user2);
when(goalStore.getGoalById(id))
.thenReturn(Optional.empty());
assertThrows(ValidationException.class, () -> goalApi.setStatusAchieved(id));
}
}