Skip to content

Commit

Permalink
unit tests only, no integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-bernat committed Nov 4, 2024
1 parent ac4f516 commit 18b2cff
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.data;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hisp.dhis.test.TestBase.createOrganisationUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Set;
import org.hisp.dhis.analytics.AnalyticsSecurityManager;
import org.hisp.dhis.analytics.DataQueryParams;
import org.hisp.dhis.analytics.DataQueryService;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.UserOrgUnitType;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.user.User;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class DataQueryServiceTest {
private static OrganisationUnit ouA;

private static OrganisationUnit ouB;

private static OrganisationUnit ouC;

private static OrganisationUnit ouD;

private static DataQueryParams dataQueryParams;

@BeforeAll
static void setUp() {
ouA = createOrganisationUnit('A');
ouB = createOrganisationUnit('B');
ouC = createOrganisationUnit('C');
ouD = createOrganisationUnit('D');
dataQueryParams =
DataQueryParams.newBuilder().withUserOrgUnitType(UserOrgUnitType.DATA_OUTPUT).build();
}

@Test
void testGetUserOrgUnitsWithExplicitGrantedForAnalyticsOrganisationUnits() {
// given
User currentUser = mock(User.class);
AnalyticsSecurityManager analyticsSecurityManager = mock(AnalyticsSecurityManager.class);
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of(ouB, ouC, ouD));
when(analyticsSecurityManager.getCurrentUser(dataQueryParams)).thenReturn(currentUser);
DataQueryService dataQueryService =
new DefaultDataQueryService(
mock(DimensionalObjectProducer.class),
mock(IdentifiableObjectManager.class),
analyticsSecurityManager);

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(3, userOrgUnits.size());
assertThat(
userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(),
containsInAnyOrder("OrganisationUnitB", "OrganisationUnitC", "OrganisationUnitD"));
}

@Test
void testGetUserOrgUnitsWithDeniedForAnalyticsOrganisationUnits() {
// given
User currentUser = mock(User.class);
AnalyticsSecurityManager analyticsSecurityManager = mock(AnalyticsSecurityManager.class);
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of());
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of(ouA));
when(analyticsSecurityManager.getCurrentUser(dataQueryParams)).thenReturn(currentUser);
DataQueryService dataQueryService =
new DefaultDataQueryService(
mock(DimensionalObjectProducer.class),
mock(IdentifiableObjectManager.class),
analyticsSecurityManager);

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(1, userOrgUnits.size());
assertThat(
userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(),
containsInAnyOrder("OrganisationUnitA"));
}

@Test
void testGetUserOrgUnitsWithDeniedForAllOrganisationUnits() {
// given
User currentUser = mock(User.class);
AnalyticsSecurityManager analyticsSecurityManager = mock(AnalyticsSecurityManager.class);
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of());
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of());
when(analyticsSecurityManager.getCurrentUser(dataQueryParams)).thenReturn(currentUser);
DataQueryService dataQueryService =
new DefaultDataQueryService(
mock(DimensionalObjectProducer.class),
mock(IdentifiableObjectManager.class),
analyticsSecurityManager);

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(0, userOrgUnits.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,9 @@
import org.hisp.dhis.user.UserRole;
import org.hisp.dhis.user.UserService;
import org.hisp.dhis.visualization.Visualization;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -183,10 +179,6 @@ class DataQueryServiceTest extends PostgresIntegrationTestBase {

private PeriodType monthly = PeriodType.getPeriodType(PeriodTypeEnum.MONTHLY);

private final String userWithDataViewOrgUnitsTag = "UserWithDataViewOrgUnits";

private final String userWithoutDataViewOrgUnitsTag = "UserWithoutDataViewOrgUnits";

@Autowired private DataQueryService dataQueryService;

@Autowired private AttributeService attributeService;
Expand Down Expand Up @@ -325,44 +317,12 @@ void setUp() {
user.addOrganisationUnit(ouA);
user.setDataViewOrganisationUnits(Set.of(ouB, ouC, ouD));
user.getUserRoles().add(role);

userService.addUser(user);

injectSecurityContextUser(user);
}

@BeforeEach
void setUp(TestInfo testInfo) {
if (testInfo.getTags().contains(userWithDataViewOrgUnitsTag)
|| testInfo.getTags().contains(userWithoutDataViewOrgUnitsTag)) {
UserRole role = createUserRole('X', "ALL");
userService.addUserRole(role);
User user = makeUser("X");
user.addOrganisationUnit(ouA);

if (testInfo.getTags().contains(userWithDataViewOrgUnitsTag)) {
user.setDataViewOrganisationUnits(Set.of(ouB, ouC, ouD));
} else {
user.setDataViewOrganisationUnits(Set.of());
}

user.getUserRoles().add(role);
userService.addUser(user);
injectSecurityContextUser(user);
}
}

@AfterEach
void tearDown(TestInfo testInfo) {
if (testInfo.getTags().contains(userWithDataViewOrgUnitsTag)
|| testInfo.getTags().contains(userWithoutDataViewOrgUnitsTag)) {
injectSecurityContextUser(userService.getUser(BASE_USER_UID + "A"));
User user = userService.getUser(BASE_USER_UID + "X");
UserRole role = userService.getUserRole(BASE_UID + "X");
userService.deleteUser(user);
userService.deleteUserRole(role);
}
}

@Test
void testGetDimensionalObjects() {
Set<String> dimensionParams = new LinkedHashSet<>();
Expand Down Expand Up @@ -1066,7 +1026,6 @@ void testGetUserOrgUnitsWithGrantedForTrackerOrganisationUnits() {
}

@Test
@Tag(userWithDataViewOrgUnitsTag)
void testGetUserOrgUnitsWithGrantedForAnalyticsOrganisationUnits() {
// given
DataQueryParams dataQueryParams =
Expand All @@ -1082,23 +1041,6 @@ void testGetUserOrgUnitsWithGrantedForAnalyticsOrganisationUnits() {
containsInAnyOrder("OrganisationUnitB", "OrganisationUnitC", "OrganisationUnitD"));
}

@Test
@Tag(userWithoutDataViewOrgUnitsTag)
void testGetUserOrgUnitsWithDeniedForAnalyticsOrganisationUnits() {
// given
DataQueryParams dataQueryParams =
DataQueryParams.newBuilder().withUserOrgUnitType(UserOrgUnitType.DATA_OUTPUT).build();

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(1, userOrgUnits.size());
assertThat(
userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(),
containsInAnyOrder("OrganisationUnitA"));
}

@Test
void testPeriodGetDimension() {
DimensionalObject dimension =
Expand Down

0 comments on commit 18b2cff

Please sign in to comment.