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

Test/1012 service #1050

Merged
merged 24 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aa16cb0
tests for PersistenceBase class
clean-coder Oct 16, 2024
66dd939
remove tests from QuarterPersistenceServiceIT which are already cover…
clean-coder Oct 16, 2024
6582e3b
remove no longer used OrganisationXXX() methods
clean-coder Oct 16, 2024
0b856ed
remove tests from TeamPersistenceServiceIT which are already covered …
clean-coder Oct 16, 2024
940e2ac
remove tests from UserPersistenceServiceIT which are already covered …
clean-coder Oct 16, 2024
61e044a
remove tests from CheckInPersistenceServiceIT which are already cover…
clean-coder Oct 17, 2024
481a33b
umbau ObjectivePersistenceServiceIT: remove tests which are already c…
clean-coder Oct 17, 2024
c9c7349
tests for AuthorizationCriteria
clean-coder Oct 21, 2024
df612b3
tests for AuthorizationCriteria
clean-coder Oct 21, 2024
f856d2a
tests for AuthorizationCriteria
clean-coder Oct 21, 2024
508481e
enable debug trace
Oct 23, 2024
fdb7ece
debug found okr-champions in test
Oct 23, 2024
3275487
add logging in findAllOkrChampionsShouldReturnAllOkrChampions() test
clean-coder Oct 23, 2024
5cfed89
disable tests which call setOkrChampion()
clean-coder Oct 23, 2024
e01c8c1
enable some disabled tests
clean-coder Oct 23, 2024
1bf0e80
enable some disabled tests
clean-coder Oct 23, 2024
82900c6
enable last disabled test .. should fail
clean-coder Oct 23, 2024
5fb338f
ugly temp fix
clean-coder Oct 23, 2024
9fd331d
cleanup
clean-coder Oct 24, 2024
24cc486
cleanup
clean-coder Oct 24, 2024
648befe
cleanup
clean-coder Oct 24, 2024
87cfa3b
cleanup
clean-coder Oct 24, 2024
30e7f7f
cleanup
clean-coder Oct 24, 2024
4ee8349
cleanup
clean-coder Oct 24, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/backend-test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
distribution: 'adopt'

- name: Use Maven to run unittests and integration tests
run: mvn clean verify
run: mvn clean verify -X
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package ch.puzzle.okr.repository;

import ch.puzzle.okr.models.Team;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TeamRepository extends CrudRepository<Team, Long> {

@Query(value = """
select distinct teamOrg.team_id from team_organisation teamOrg
inner join organisation o on o.id = teamOrg.organisation_id
where o.org_name in (:organisationNames)
""", nativeQuery = true)
List<Long> findTeamIdsByOrganisationNames(@Param("organisationNames") List<String> organisationNames);

List<Team> findTeamsByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public String getModelName() {
return OBJECTIVE;
}

/**
* Get the number of Objectives of a Team in a Quarter. The underling sql looks like "select count(*) from objective
* where teamId = team.id and quarterId = quarter.id."
*
* @param team
* Team
* @param quarter
* Quarter
*
* @return number of Objectives of team in quarter
*/
public Integer countByTeamAndQuarter(Team team, Quarter quarter) {
return getRepository().countByTeamAndQuarter(team, quarter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ public String getModelName() {
return TEAM;
}

public List<Long> findTeamIdsByOrganisationName(String organisationName) {
return findTeamIdsByOrganisationNames(List.of(organisationName));
}

public List<Long> findTeamIdsByOrganisationNames(List<String> organisationNames) {
return getRepository().findTeamIdsByOrganisationNames(organisationNames);
}

public List<Team> findTeamsByName(String name) {
return getRepository().findTeamsByName(name);
}
Expand Down
14 changes: 14 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/util/CollectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.puzzle.okr.util;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class CollectionUtils {

public static <T> List<T> iterableToList(Iterable<T> iterable) {
return StreamSupport //
.stream(iterable.spliterator(), false) //
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import ch.puzzle.okr.service.authorization.AuthorizationRegistrationService;
import ch.puzzle.okr.test.SpringIntegrationTest;
import ch.puzzle.okr.test.TestHelper;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import static ch.puzzle.okr.SpringCachingConfig.AUTHORIZATION_USER_CACHE;
import static ch.puzzle.okr.test.TestHelper.defaultUser;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringIntegrationTest
class SpringCachingConfigTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import ch.puzzle.okr.multitenancy.TenantContext;
import ch.puzzle.okr.service.persistence.UserPersistenceService;
import ch.puzzle.okr.test.SpringIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
Expand All @@ -34,17 +31,39 @@ class AuthorizationRegistrationServiceIT {
private final String tenant = TestHelper.SCHEMA_PITC;
private final String key = tenant + "_" + user.getEmail();

private static final String EMAIL_WUNDERLAND = "[email protected]";

@BeforeEach
void setUp() {
TenantContext.setCurrentTenant(tenant);
}

@AfterEach
void tearDown() {
resetOkrChampionStatus();
clearCache();
TenantContext.setCurrentTenant(null);
}

private void resetOkrChampionStatus() {
Optional<User> userFromDb = userPersistenceService.findByEmail(EMAIL_WUNDERLAND);
assertTrue(userFromDb.isPresent());

userFromDb.get().setOkrChampion(false);
userPersistenceService.save(userFromDb.get());
assertOkrChampionStatusInDb(userFromDb.get().getEmail(), false);
}

private void clearCache() {
Cache cache = cacheManager.getCache(AUTHORIZATION_USER_CACHE);
assertNotNull(cache);
cache.clear();
TenantContext.setCurrentTenant(null);
}

private void assertOkrChampionStatusInDb(String email, boolean expectedOkrChampionStatus) {
var userInDb = userPersistenceService.findByEmail(email);
assertTrue(userInDb.isPresent());
assertEquals(expectedOkrChampionStatus, userInDb.get().isOkrChampion());
}

@Test
Expand Down Expand Up @@ -81,6 +100,8 @@ void registerAuthorizationUser_shouldSetOkrChampionsToFalse() {
// assert
assertFalse(processedUser.user().isOkrChampion());
Optional<User> userFromDB = userPersistenceService.findByEmail(user.getEmail());

assertTrue(userFromDB.isPresent());
assertFalse(userFromDB.get().isOkrChampion());

// cleanup
Expand All @@ -89,30 +110,31 @@ void registerAuthorizationUser_shouldSetOkrChampionsToFalse() {

/*
* Special test setup. <pre> - the user [email protected] is an existing user in the H2 db (created via
* X_TestData.sql) - the user [email protected] is also defined in application-integration-test.properties as
* user champion - with this combination we can test, that the user in the db (which has initial isOkrChampion ==
* false) is after calling updateOrAddAuthorizationUser() a user champion. - because the user [email protected]
* exists before the test, we make no clean in db (we don't remove it) </pre>
* V100_0_0__TestData.sql) - the user [email protected] is also defined in
* application-integration-test.properties as user champion - with this combination we can test, that the user in
* the db (which has initial isOkrChampion == false) is after calling updateOrAddAuthorizationUser() a user
* champion. - the OkrChampion status must manually be reset (in the tearDown method) </pre>
*/
@Test
@DisplayName("registerAuthorizationUser for a user with an email defined in the application-integration-test.properties should set OkrChampions to true")
void registerAuthorizationUserShouldSetOkrChampionsToTrue() {
// arrange
User user = User.Builder.builder() //
.withFirstname("Alice") //
.withLastname("Wunderland") //
.withEmail("[email protected]") // user.champion.emails from application-integration-test.properties
.build();

userPersistenceService.getOrCreateUser(user); // updates input user with id from DB !!!
assertOkrChampionStatusInDb(EMAIL_WUNDERLAND, false); // pre-condition

// act
AuthorizationUser processedUser = authorizationRegistrationService.updateOrAddAuthorizationUser(user);
// load user from db (by email) and set OkrChampion status based on property
// "okr.tenants.pitc.user.champion.emails" from application-integration-test.properties file
AuthorizationUser processedUser = authorizationRegistrationService
.updateOrAddAuthorizationUser(User.Builder.builder() //
.withFirstname("Alice") //
.withLastname("Wunderland") //
.withEmail(EMAIL_WUNDERLAND) // user.champion.emails from
// application-integration-test.properties
.build());

// assert
assertTrue(processedUser.user().isOkrChampion());
Optional<User> userFromDB = userPersistenceService.findByEmail(user.getEmail());
assertTrue(userFromDB.get().isOkrChampion());
assertOkrChampionStatusInDb(processedUser.user().getEmail(), true);
}

@Test
Expand All @@ -138,6 +160,7 @@ void registerAuthorizationUser_shouldSetFirstnameAndLastnameFromToken() {

// assert
Optional<User> userFromDB = userPersistenceService.findByEmail(user.getEmail());
assertTrue(userFromDB.isPresent());
assertEquals(userFromDB.get().getFirstname(), firstNameFromToken);
assertEquals(userFromDB.get().getLastname(), lastNameFromToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.puzzle.okr.service.authorization;

import ch.puzzle.okr.test.TestHelper;
import ch.puzzle.okr.dto.ErrorDto;
import ch.puzzle.okr.exception.OkrResponseStatusException;
import ch.puzzle.okr.models.Objective;
Expand All @@ -13,6 +12,7 @@
import ch.puzzle.okr.models.keyresult.KeyResultMetric;
import ch.puzzle.okr.security.JwtHelper;
import ch.puzzle.okr.service.persistence.ObjectivePersistenceService;
import ch.puzzle.okr.test.TestHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -28,9 +28,9 @@
import java.util.List;

import static ch.puzzle.okr.ErrorKey.*;
import static ch.puzzle.okr.test.TestHelper.*;
import static ch.puzzle.okr.service.authorization.AuthorizationService.hasRoleWriteAndReadAll;
import static ch.puzzle.okr.service.authorization.AuthorizationService.hasRoleWriteForTeam;
import static ch.puzzle.okr.test.TestHelper.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void getAllTeamsShouldReturnAllTeams(boolean isWriteable) {
if (isWriteable) {
when(authorizationService.updateOrAddAuthorizationUser()).thenReturn(okrChampionUser);
} else {
when(authorizationService.updateOrAddAuthorizationUser()).thenReturn(userWithoutWriteAllRole());
when(authorizationService.updateOrAddAuthorizationUser()).thenReturn(defaultAuthorizationUser());
}
when(teamBusinessService.getAllTeams(any())).thenReturn(teamList);

Expand Down
Loading
Loading