FINERACT-984: Test improvements to not rely on database storage ordering and some database independent query implementations

This commit is contained in:
Arnold Galovics
2022-02-20 23:24:50 +01:00
committed by Aleksandar Vidakovic
parent 72fd2fe134
commit 45264ea209
10 changed files with 33 additions and 18 deletions
@@ -19,8 +19,14 @@
package org.apache.fineract.useradministration.domain; package org.apache.fineract.useradministration.domain;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface PermissionRepository extends JpaRepository<Permission, Long> { public interface PermissionRepository extends JpaRepository<Permission, Long> {
// It's important to use the same case for equality check because there are cases when the codes are not capitalized
// the same way. (UNDOTRANSACTION_CLIENT vs UNDOTRANSACTION_client)
// Also, trimming leading and trailing spaces is critical ("CREATE_STANDINGINSTRUCTION" vs
// "CREATE_STANDINGINSTRUCTION ").
@Query("SELECT p FROM Permission p WHERE LOWER(TRIM(BOTH FROM p.code)) = LOWER(TRIM(BOTH FROM ?1))")
Permission findOneByCode(String code); Permission findOneByCode(String code);
} }
@@ -28,7 +28,7 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
@Query("SELECT COUNT(a) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId AND a.deleted = false") @Query("SELECT COUNT(a) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId AND a.deleted = false")
Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId); Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId);
@Query("SELECT role FROM Role role WHERE role.name = :name") @Query("SELECT role FROM Role role WHERE LOWER(role.name) = LOWER(:name)")
Role getRoleByName(@Param("name") String name); Role getRoleByName(@Param("name") String name);
} }
@@ -193,7 +193,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
public String schema() { public String schema() {
return " u.id as id, u.username as username, u.firstname as firstname, u.lastname as lastname, u.email as email, u.password_never_expires as passwordNeverExpires, " return " u.id as id, u.username as username, u.firstname as firstname, u.lastname as lastname, u.email as email, u.password_never_expires as passwordNeverExpires, "
+ " u.office_id as officeId, o.name as officeName, u.staff_id as staffId, u.is_self_service_user as isSelfServiceUser from m_appuser u " + " u.office_id as officeId, o.name as officeName, u.staff_id as staffId, u.is_self_service_user as isSelfServiceUser from m_appuser u "
+ " join m_office o on o.id = u.office_id where o.hierarchy like ? and u.is_deleted=0 order by u.username"; + " join m_office o on o.id = u.office_id where o.hierarchy like ? and u.is_deleted=false order by u.username";
} }
} }
@@ -211,7 +211,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
public String schema() { public String schema() {
return " u.id as id, u.username as username from m_appuser u " return " u.id as id, u.username as username from m_appuser u "
+ " join m_office o on o.id = u.office_id where o.hierarchy like ? and u.is_deleted=0 order by u.username"; + " join m_office o on o.id = u.office_id where o.hierarchy like ? and u.is_deleted=false order by u.username";
} }
} }
@@ -317,7 +317,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
// TODO: this needs to be fixed. The error condition should be independent from the underlying message and // TODO: this needs to be fixed. The error condition should be independent from the underlying message and
// naming // naming
// of the constraint // of the constraint
if (realCause.getMessage().contains("'username_org'")) { if (realCause.getMessage().contains("username_org")) {
final String username = command.stringValueOfParameterNamed("username"); final String username = command.stringValueOfParameterNamed("username");
final StringBuilder defaultMessageBuilder = new StringBuilder("User with username ").append(username) final StringBuilder defaultMessageBuilder = new StringBuilder("User with username ").append(username)
.append(" already exists."); .append(" already exists.");
@@ -328,7 +328,7 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
// TODO: this needs to be fixed. The error condition should be independent from the underlying message and // TODO: this needs to be fixed. The error condition should be independent from the underlying message and
// naming // naming
// of the constraint // of the constraint
if (realCause.getMessage().contains("'unique_self_client'")) { if (realCause.getMessage().contains("unique_self_client")) {
return new PlatformDataIntegrityException("error.msg.user.self.service.user.already.exist", return new PlatformDataIntegrityException("error.msg.user.self.service.user.already.exist",
"Self Service User Id is already created. Go to Admin->Users to edit or delete the self-service user."); "Self Service User Id is already created. Go to Admin->Users to edit or delete the self-service user.");
} }
@@ -96,7 +96,7 @@ public class PermissionReadPlatformServiceImpl implements PermissionReadPlatform
/* get all non-CHECKER permissions */ /* get all non-CHECKER permissions */
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, true as selected" return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, true as selected"
+ " from m_permission p " + " where code not like '%\\_CHECKER'" + " from m_permission p " + " where code not like '%\\_CHECKER'"
+ " order by p.grouping, ifnull(entity_name, ''), p.code"; + " order by p.grouping, coalesce(entity_name, ''), p.code";
} }
public String makerCheckerablePermissionSchema() { public String makerCheckerablePermissionSchema() {
@@ -107,13 +107,13 @@ public class PermissionReadPlatformServiceImpl implements PermissionReadPlatform
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, p.can_maker_checker as selected" return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, p.can_maker_checker as selected"
+ " from m_permission p " + " where `grouping` != 'special' and code not like 'READ_%' and code not like '%\\_CHECKER'" + " from m_permission p " + " where `grouping` != 'special' and code not like 'READ_%' and code not like '%\\_CHECKER'"
+ " order by p.grouping, ifnull(entity_name, ''), p.code"; + " order by p.grouping, coalesce(entity_name, ''), p.code";
} }
public String rolePermissionSchema() { public String rolePermissionSchema() {
return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, if(isnull(rp.role_id), false, true) as selected " return "select p.grouping, p.code, p.entity_name as entityName, p.action_name as actionName, rp.role_id IS NOT NULL as selected "
+ " from m_permission p " + " left join m_role_permission rp on rp.permission_id = p.id and rp.role_id = ? " + " from m_permission p " + " left join m_role_permission rp on rp.permission_id = p.id and rp.role_id = ? "
+ " order by p.grouping, ifnull(entity_name, ''), p.code"; + " order by p.grouping, COALESCE(entity_name, ''), p.code";
} }
} }
@@ -53,7 +53,7 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService {
@Override @Override
public Collection<RoleData> retrieveAllActiveRoles() { public Collection<RoleData> retrieveAllActiveRoles() {
final String sql = "select " + this.roleRowMapper.schema() + " where r.is_disabled = 0 order by r.id"; final String sql = "select " + this.roleRowMapper.schema() + " where r.is_disabled = false order by r.id";
return this.jdbcTemplate.query(sql, this.roleRowMapper); return this.jdbcTemplate.query(sql, this.roleRowMapper);
} }
@@ -113,7 +113,7 @@ public class CenterIntegrationTest {
@Test @Test
public void testVoidCenterRetrieval() { public void testVoidCenterRetrieval() {
ArrayList<CenterDomain> arr = CenterHelper.listCenters(requestSpec, responseSpec); ArrayList<CenterDomain> arr = CenterHelper.listCentersOrdered(requestSpec, responseSpec);
int id = arr.get(arr.size() - 1).getId() + 1; int id = arr.get(arr.size() - 1).getId() + 1;
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build(); ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build();
CenterDomain center = CenterHelper.retrieveByID(id, requestSpec, responseSpec); CenterDomain center = CenterHelper.retrieveByID(id, requestSpec, responseSpec);
@@ -56,8 +56,8 @@ public class CreditBureauConfigurationTest {
public void creditBureauConfigurationTest() { public void creditBureauConfigurationTest() {
// create creditBureauConfiguration // create creditBureauConfiguration
final Integer configurationId = CreditBureauConfigurationHelper.createCreditBureauConfiguration(this.requestSpec, final Integer configurationId = CreditBureauConfigurationHelper.createCreditBureauConfiguration(this.requestSpec, this.responseSpec,
this.responseSpec); Utils.randomNameGenerator("testConfigKey_", 5));
Assertions.assertNotNull(configurationId); Assertions.assertNotNull(configurationId);
// update creditBureauConfiguration // update creditBureauConfiguration
@@ -68,6 +68,15 @@ public final class CenterHelper {
return new Gson().fromJson(jsonData, new TypeToken<ArrayList<CenterDomain>>() {}.getType()); return new Gson().fromJson(jsonData, new TypeToken<ArrayList<CenterDomain>>() {}.getType());
} }
public static ArrayList<CenterDomain> listCentersOrdered(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
final String GET_CENTER = CENTERS_URL + "?limit=-1&orderBy=id&sortOrder=asc&" + Utils.TENANT_IDENTIFIER;
LOG.info("------------------------ RETRIEVING CENTERS-------------------------");
Object get = Utils.performServerGet(requestSpec, responseSpec, GET_CENTER, "");
final String jsonData = new Gson().toJson(get);
return new Gson().fromJson(jsonData, new TypeToken<ArrayList<CenterDomain>>() {}.getType());
}
public static int createCenter(final String name, final int officeId, final RequestSpecification requestSpec, public static int createCenter(final String name, final int officeId, final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) { final ResponseSpecification responseSpec) {
return createCenter(name, officeId, null, -1, null, null, requestSpec, responseSpec); return createCenter(name, officeId, null, -1, null, null, requestSpec, responseSpec);
@@ -40,18 +40,18 @@ public class CreditBureauConfigurationHelper {
this.responseSpec = responseSpec; this.responseSpec = responseSpec;
} }
public static Integer createCreditBureauConfiguration(final RequestSpecification requestSpec, public static Integer createCreditBureauConfiguration(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final ResponseSpecification responseSpec) { String configKey) {
return createCreditBureauConfiguration(requestSpec, responseSpec, "1"); return createCreditBureauConfiguration(requestSpec, responseSpec, "1", configKey);
} }
public static Integer createCreditBureauConfiguration(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, public static Integer createCreditBureauConfiguration(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String creditBureauId) { final String creditBureauId, String configKey) {
LOG.info("---------------------------------CREATING A CREDIT_BUREAU_CONFIGURATION---------------------------------------------"); LOG.info("---------------------------------CREATING A CREDIT_BUREAU_CONFIGURATION---------------------------------------------");
final String CREDITBUREAU_CONFIGURATION_URL = " /fineract-provider/api/v1/CreditBureauConfiguration/configuration/" + creditBureauId final String CREDITBUREAU_CONFIGURATION_URL = " /fineract-provider/api/v1/CreditBureauConfiguration/configuration/" + creditBureauId
+ "?" + Utils.TENANT_IDENTIFIER; + "?" + Utils.TENANT_IDENTIFIER;
return Utils.performServerPost(requestSpec, responseSpec, CREDITBUREAU_CONFIGURATION_URL, return Utils.performServerPost(requestSpec, responseSpec, CREDITBUREAU_CONFIGURATION_URL,
creditBureauConfigurationAsJson("testConfigKey", "testConfigKeyValue", "description"), "resourceId"); creditBureauConfigurationAsJson(configKey, "testConfigKeyValue", "description"), "resourceId");
} }
/* /*