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

fix: Fix Retrieving User Badges When same as User score - MEED-7505 - Meeds-io/meeds#2392 #1740

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ private void buildLatestWonBadge(long programId, long score, JSONArray userBadge
JSONObject reputation = null;
int index = 0;
Iterator<BadgeDTO> iterable = allBadges.iterator();
while(iterable.hasNext()) {
badgeDTO = iterable.next();
if (badgeDTO.getNeededScore() < score) {
++index;
}
while (iterable.hasNext()) {
badgeDTO = iterable.next();
if (badgeDTO.getNeededScore() <= score) {
++index;
}
}
if (index > 0) {
badgeDTO = allBadges.get(index - 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package io.meeds.gamification.rest;

import static io.meeds.gamification.constant.GamificationConstant.ACTIVITY_OBJECT_TYPE;

import java.util.Map;

import org.json.JSONArray;
import org.junit.Before;
import org.junit.Test;

import org.exoplatform.services.rest.impl.ContainerResponse;
import org.exoplatform.services.security.Identity;

import io.meeds.gamification.entity.BadgeEntity;
import io.meeds.gamification.entity.ProgramEntity;
import io.meeds.gamification.model.RealizationDTO;
import io.meeds.gamification.model.RuleDTO;
import io.meeds.gamification.test.AbstractServiceTest;

public class TestUserReputationEndpoint extends AbstractServiceTest {

private static final String ADMIN_USER = "root1";

private Identity adminAclIdentity;

private String adminIdentityId;

protected Class<?> getComponentClass() {
return UserReputationEndpoint.class;
}
Expand All @@ -25,6 +40,9 @@ public void setUp() throws Exception {
newRealizationEntity("rule1", domainEntity.getId());
newRealizationEntity("rule2", domainEntity.getId());
newRealizationEntity("rule3", domainEntity.getId());

adminAclIdentity = registerAdministratorUser(ADMIN_USER);
adminIdentityId = identityManager.getOrCreateUserIdentity(ADMIN_USER).getId();
}

@Test
Expand All @@ -34,11 +52,36 @@ public void testGetReputationStatus() throws Exception {
assertEquals(200, response.getStatus());
}

@SuppressWarnings("rawtypes")
@Test
public void testGetUserBadges() throws Exception {
ContainerResponse response = getResponse("GET", getURLResource("reputation/badges/1"), null);
RuleDTO rule = newRuleDTO();

RealizationDTO realization = realizationService.createRealizations(rule.getEvent().getTitle(),
null,
adminIdentityId,
adminIdentityId,
ACTIVITY_ID,
ACTIVITY_OBJECT_TYPE)
.getFirst();
assertNotNull(realization);
assertTrue(realization.getId() > 0);

BadgeEntity badge = newBadge("Badge1", realization.getProgram().getId());
badge.setNeededScore((int) realization.getActionScore());

ContainerResponse response = getResponse("GET", getURLResource("reputation/badges/" + adminIdentityId), null);
assertNotNull(response);
assertEquals(200, response.getStatus());
String userBadgesString = (String) response.getEntity();
JSONArray userBadges = new JSONArray(userBadgesString);
assertNotNull(userBadges);
assertTrue(userBadges.length() >= 1);
assertTrue(userBadges.toList()
.stream()
.map(o -> (Map) o)
.map(m -> m.get("id"))
.anyMatch(id -> String.valueOf(id).equals(String.valueOf(badge.getId()))));

response = getResponse("GET", getURLResource("reputation/badges"), null);
assertNotNull(response);
Expand Down