Skip to content

Commit

Permalink
FINERACT-984: Test improvements to not rely on database storage order…
Browse files Browse the repository at this point in the history
…ing and some database independent query implementations
  • Loading branch information
galovics authored and vidakovic committed Feb 20, 2022
1 parent 72fd2fe commit 45264ea
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
package org.apache.fineract.useradministration.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public AppUserData mapRow(final ResultSet rs, @SuppressWarnings("unused") final
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, "
+ " 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";
}

}
Expand All @@ -211,7 +211,7 @@ public AppUserData mapRow(final ResultSet rs, @SuppressWarnings("unused") final

public String schema() {
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";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private PlatformDataIntegrityException handleDataIntegrityIssues(final JsonComma
// TODO: this needs to be fixed. The error condition should be independent from the underlying message and
// naming
// of the constraint
if (realCause.getMessage().contains("'username_org'")) {
if (realCause.getMessage().contains("username_org")) {
final String username = command.stringValueOfParameterNamed("username");
final StringBuilder defaultMessageBuilder = new StringBuilder("User with username ").append(username)
.append(" already exists.");
Expand All @@ -328,7 +328,7 @@ private PlatformDataIntegrityException handleDataIntegrityIssues(final JsonComma
// TODO: this needs to be fixed. The error condition should be independent from the underlying message and
// naming
// 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",
"Self Service User Id is already created. Go to Admin->Users to edit or delete the self-service user.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String permissionSchema() {
/* get all non-CHECKER permissions */
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'"
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
+ " order by p.grouping, coalesce(entity_name, ''), p.code";
}

public String makerCheckerablePermissionSchema() {
Expand All @@ -107,13 +107,13 @@ public String makerCheckerablePermissionSchema() {

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'"
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
+ " order by p.grouping, coalesce(entity_name, ''), p.code";
}

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 = ? "
+ " order by p.grouping, ifnull(entity_name, ''), p.code";
+ " order by p.grouping, COALESCE(entity_name, ''), p.code";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Collection<RoleData> retrieveAll() {

@Override
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testListCenters() {

@Test
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;
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build();
CenterDomain center = CenterHelper.retrieveByID(id, requestSpec, responseSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void setup() {
public void creditBureauConfigurationTest() {

// create creditBureauConfiguration
final Integer configurationId = CreditBureauConfigurationHelper.createCreditBureauConfiguration(this.requestSpec,
this.responseSpec);
final Integer configurationId = CreditBureauConfigurationHelper.createCreditBureauConfiguration(this.requestSpec, this.responseSpec,
Utils.randomNameGenerator("testConfigKey_", 5));
Assertions.assertNotNull(configurationId);

// update creditBureauConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public static ArrayList<CenterDomain> listCenters(final RequestSpecification req
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,
final ResponseSpecification responseSpec) {
return createCenter(name, officeId, null, -1, null, null, requestSpec, responseSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public CreditBureauConfigurationHelper(final RequestSpecification requestSpec, f
this.responseSpec = responseSpec;
}

public static Integer createCreditBureauConfiguration(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
return createCreditBureauConfiguration(requestSpec, responseSpec, "1");
public static Integer createCreditBureauConfiguration(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
String configKey) {
return createCreditBureauConfiguration(requestSpec, responseSpec, "1", configKey);
}

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---------------------------------------------");
final String CREDITBUREAU_CONFIGURATION_URL = " /fineract-provider/api/v1/CreditBureauConfiguration/configuration/" + creditBureauId
+ "?" + Utils.TENANT_IDENTIFIER;
return Utils.performServerPost(requestSpec, responseSpec, CREDITBUREAU_CONFIGURATION_URL,
creditBureauConfigurationAsJson("testConfigKey", "testConfigKeyValue", "description"), "resourceId");
creditBureauConfigurationAsJson(configKey, "testConfigKeyValue", "description"), "resourceId");
}

/*
Expand Down

0 comments on commit 45264ea

Please sign in to comment.