Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop' into OP-1189
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudFonzam committed Sep 3, 2024
2 parents 741f9c8 + 8eab0aa commit 3b606d4
Show file tree
Hide file tree
Showing 23 changed files with 1,434 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/isf/utils/excel/ExcelExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void exportTableToCSV(JTable jtable, File file, String separator) throws
}
outFile.write("\n");

int rowCount = model.getColumnCount();
int rowCount = model.getRowCount();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
String strVal;
Expand Down Expand Up @@ -232,7 +232,7 @@ private void exportResultsetToCSV(ResultSet resultSet, File exportFile, String s

int colCount = rsmd.getColumnCount();
for (int i = 1; i <= colCount; i++) {
if (i == colCount - 1) {
if (i == colCount) {
output.write(rsmd.getColumnName(i));
} else {
output.write(rsmd.getColumnName(i) + separator);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/isf/OHCoreTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
*/
package org.isf;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
Expand Down Expand Up @@ -56,4 +60,18 @@ public void cleanH2InMemoryDb() {
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate();
}

public void executeSQLScript(String fileName) {
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
try {
Path path = Paths.get(getClass().getResource(fileName).toURI());
Stream<String> lines = Files.lines(path);
for (String line : (Iterable<String>) lines::iterator) {
entityManager.createNativeQuery(line).executeUpdate();
}
lines.close();
} catch(Exception exception) {
LOGGER.error("Error trying to execute script: " + fileName, exception);
}
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate();
}
}
51 changes: 51 additions & 0 deletions src/test/java/org/isf/permissions/TestGroupPermission.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
*
* Open Hospital is a free and open source software for healthcare data management.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.isf.permissions;

import static org.assertj.core.api.Assertions.assertThat;

import org.isf.OHCoreTestCase;
import org.isf.menu.model.UserGroup;
import org.isf.permissions.model.GroupPermission;
import org.isf.permissions.model.Permission;
import org.junit.jupiter.api.Test;

class TestGroupPermission extends OHCoreTestCase {

@Test
void testModel() throws Exception {
GroupPermission groupPermission = new GroupPermission();

groupPermission.setId(-1);
assertThat(groupPermission.getId()).isEqualTo(-1);

Permission permission = new Permission();
permission.setId(-1);
groupPermission.setPermission(permission);
assertThat(groupPermission.getPermission().getId()).isEqualTo(-1);

UserGroup userGroup = new UserGroup();
userGroup.setCode("code");
groupPermission.setUserGroup(userGroup);
assertThat(groupPermission.getUserGroup().getCode()).isEqualTo("code");
}
}
56 changes: 56 additions & 0 deletions src/test/java/org/isf/permissions/TestGroupPermissionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
*
* Open Hospital is a free and open source software for healthcare data management.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.isf.permissions;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.List;

import org.isf.OHCoreTestCase;
import org.isf.permissions.manager.GroupPermissionManager;
import org.isf.permissions.model.GroupPermission;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

class TestGroupPermissionManager extends OHCoreTestCase {

@Autowired
GroupPermissionManager groupPermissionManager;

@BeforeEach
void setUp() {
cleanH2InMemoryDb();
executeSQLScript("LoadPermissionTables.sql");
}

@Test
void testFindByIdIn() throws Exception {
List<Integer> listIds = new ArrayList<>();
listIds.add(1);
listIds.add(10);
List<GroupPermission> groupPermissionList = groupPermissionManager.findByIdIn(listIds);
assertThat(groupPermissionList).isNotEmpty();
assertThat(groupPermissionList).hasSize(2);
}
}
54 changes: 54 additions & 0 deletions src/test/java/org/isf/permissions/TestPermission.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
*
* Open Hospital is a free and open source software for healthcare data management.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.isf.permissions;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.List;

import org.isf.OHCoreTestCase;
import org.isf.permissions.model.GroupPermission;
import org.isf.permissions.model.Permission;
import org.junit.jupiter.api.Test;

class TestPermission extends OHCoreTestCase {

@Test
void testModel() throws Exception {
Permission permission = new Permission();

permission.setId(-1);
assertThat(permission.getId()).isEqualTo(-1);

permission.setDescription("description");
assertThat(permission.getDescription()).isEqualTo("description");

permission.setName("name");
assertThat(permission.getName()).isEqualTo("name");

List<GroupPermission> groupPermissions = new ArrayList<>();
groupPermissions.add(new GroupPermission());
permission.setGroupPermission(groupPermissions);
assertThat(permission.getGroupPermission()).hasSize(1);
}
}
97 changes: 97 additions & 0 deletions src/test/java/org/isf/permissions/TestPermissionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
*
* Open Hospital is a free and open source software for healthcare data management.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.isf.permissions;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.isf.OHCoreTestCase;
import org.isf.permissions.manager.PermissionManager;
import org.isf.permissions.model.Permission;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

class TestPermissionManager extends OHCoreTestCase {

@Autowired
PermissionManager permissionManager;

@BeforeEach
void setUp() {
cleanH2InMemoryDb();
executeSQLScript("LoadPermissionTables.sql");
}

@Test
void testRetrieveByGroupCode() throws Exception {
List<Permission> permissions = permissionManager.retrievePermissionsByGroupCode("doctor");
assertThat(permissions).isNotEmpty();
assertThat(permissions.get(0).getName()).isEqualTo("admissions.create");
assertThat(permissions.get(0).getDescription()).isEmpty();
}

@Test
void testRetrieveByUsername() throws Exception {
List<Permission> permissions = permissionManager.retrievePermissionsByUsername("admin");
assertThat(permissions).isNotEmpty();
assertThat(permissions.get(0).getName()).isEqualTo("admissions.create");
assertThat(permissions.get(0).getDescription()).isEmpty();
}

@Test
void testRetrieveById() throws Exception {
Permission permission = permissionManager.retrievePermissionById(3);
assertThat(permission).isNotNull();
assertThat(permission.getName()).isEqualTo("admissions.update");
assertThat(permission.getDescription()).isEmpty();
assertThat(permission.getId()).isEqualTo(3);
assertThat(permission.getGroupPermission()).isNotNull();
}

@Test
void testRetrieveByName() throws Exception {
Permission permission = permissionManager.retrievePermissionByName("admissions.create");
assertThat(permission).isNotNull();
assertThat(permission.getName()).isEqualTo("admissions.create");
assertThat(permission.getDescription()).isEmpty();
}

@Test
void testRetrieveAllPermissions() throws Exception {
List<Permission> permissions = permissionManager.retrieveAllPermissions();
assertThat(permissions).isNotEmpty();
assertThat(permissions.get(0).getName()).isEqualTo("admissions.create");
assertThat(permissions.get(0).getDescription()).isEmpty();

assertThat(permissions).hasSize(30);
}

@Test
void testExists() throws Exception {
Permission permission = permissionManager.retrievePermissionById(3);
assertThat(permission).isNotNull();
assertThat(permissionManager.exists(5)).isTrue();
assertThat(permissionManager.exists(-999)).isFalse();
}
}
66 changes: 66 additions & 0 deletions src/test/java/org/isf/utils/db/TestDbJpaUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
*
* Open Hospital is a free and open source software for healthcare data management.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0-standalone.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.isf.utils.db;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;

import org.isf.menu.manager.Context;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.context.ApplicationContext;

class TestDbJpaUtil {

@Mock
EntityManagerFactory entityManagerFactoryMock;
@Mock
EntityManager entityManagerMock;
@Mock
ApplicationContext applicationContextMock;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
Context.setApplicationContext(applicationContextMock);
when(applicationContextMock.getBean("entityManagerFactory", EntityManagerFactory.class)).thenReturn(entityManagerFactoryMock);
}

@Test
void testNew() throws Exception {
DbJpaUtil dbJpaUtil = new DbJpaUtil();
assertThat(dbJpaUtil).isNotNull();
}

@Test
void testOpen() throws Exception {
DbJpaUtil dbJpaUtil = new DbJpaUtil();
when(entityManagerFactoryMock.createEntityManager()).thenReturn(entityManagerMock);
dbJpaUtil.open();
}

}
Loading

0 comments on commit 3b606d4

Please sign in to comment.