Rename name to id in the stored badge information and expose id in the profile endpoint

This commit is contained in:
Ehren Kret 2021-09-15 16:49:20 -05:00
parent 34e21b9f7b
commit 8a8e6e7b49
4 changed files with 32 additions and 22 deletions

View File

@ -90,7 +90,9 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter {
.filter(accountBadge -> accountBadge.isVisible()
&& now.isBefore(accountBadge.getExpiration())
&& knownBadges.containsKey(accountBadge.getId()))
.map(accountBadge -> new Badge(knownBadges.get(accountBadge.getId()).getImageUrl(),
.map(accountBadge -> new Badge(
accountBadge.getId(),
knownBadges.get(accountBadge.getId()).getImageUrl(),
resourceBundle.getString(accountBadge.getId() + "_name"),
resourceBundle.getString(accountBadge.getId() + "_description")))
.collect(Collectors.toList());

View File

@ -11,20 +11,27 @@ import java.net.URL;
import java.util.Objects;
public class Badge {
private final String id;
private final URL imageUrl;
private final String name;
private final String description;
@JsonCreator
public Badge(
@JsonProperty("id") final String id,
@JsonProperty("imageUrl") final URL imageUrl,
@JsonProperty("name") final String name,
@JsonProperty("description") final String description) {
this.id = id;
this.imageUrl = imageUrl;
this.name = name;
this.description = description;
}
public String getId() {
return id;
}
public URL getImageUrl() {
return imageUrl;
}
@ -46,12 +53,13 @@ public class Badge {
return false;
}
Badge badge = (Badge) o;
return Objects.equals(imageUrl, badge.imageUrl) && Objects.equals(name,
badge.name) && Objects.equals(description, badge.description);
return Objects.equals(id, badge.id) && Objects.equals(imageUrl,
badge.imageUrl) && Objects.equals(name, badge.name) && Objects.equals(
description, badge.description);
}
@Override
public int hashCode() {
return Objects.hash(imageUrl, name, description);
return Objects.hash(id, imageUrl, name, description);
}
}

View File

@ -51,7 +51,7 @@ public class ConfiguredProfileBadgeConverterTest {
when(clock.instant()).thenReturn(Instant.ofEpochSecond(42));
}
private static String nameFor(int i) {
private static String idFor(int i) {
return "Badge-" + i;
}
@ -63,16 +63,16 @@ public class ConfiguredProfileBadgeConverterTest {
}
}
private static String rbNameFor(int i) {
private static String nameFor(int i) {
return "TRANSLATED NAME " + i;
}
private static String rbDesriptionFor(int i) {
private static String desriptionFor(int i) {
return "TRANSLATED DESCRIPTION " + i;
}
private static BadgeConfiguration newBadge(int i) {
return new BadgeConfiguration(nameFor(i), imageUrlFor(i));
return new BadgeConfiguration(idFor(i), imageUrlFor(i));
}
private BadgesConfiguration createBadges(int count) {
@ -80,8 +80,8 @@ public class ConfiguredProfileBadgeConverterTest {
Object[][] objects = new Object[count * 2][2];
for (int i = 0; i < count; i++) {
badges.add(newBadge(i));
objects[(i * 2)] = new Object[]{nameFor(i) + "_name", rbNameFor(i)};
objects[(i * 2) + 1] = new Object[]{nameFor(i) + "_description", rbDesriptionFor(i)};
objects[(i * 2)] = new Object[]{idFor(i) + "_name", nameFor(i)};
objects[(i * 2) + 1] = new Object[]{idFor(i) + "_description", desriptionFor(i)};
}
resourceBundle = new ListResourceBundle() {
@Override
@ -94,7 +94,7 @@ public class ConfiguredProfileBadgeConverterTest {
private BadgeConfiguration getBadge(BadgesConfiguration badgesConfiguration, int i) {
return badgesConfiguration.getBadges().stream()
.filter(badgeConfiguration -> nameFor(i).equals(badgeConfiguration.getName()))
.filter(badgeConfiguration -> idFor(i).equals(badgeConfiguration.getName()))
.findFirst().orElse(null);
}
@ -137,14 +137,14 @@ public class ConfiguredProfileBadgeConverterTest {
Instant expired = Instant.ofEpochSecond(41);
Instant notExpired = Instant.ofEpochSecond(43);
return Stream.of(
arguments(nameFor(0), expired, false, null),
arguments(nameFor(0), notExpired, false, null),
arguments(nameFor(0), expired, true, null),
arguments(nameFor(0), notExpired, true, new Badge(imageUrlFor(0), rbNameFor(0), rbDesriptionFor(0))),
arguments(nameFor(1), expired, false, null),
arguments(nameFor(1), notExpired, false, null),
arguments(nameFor(1), expired, true, null),
arguments(nameFor(1), notExpired, true, null)
arguments(idFor(0), expired, false, null),
arguments(idFor(0), notExpired, false, null),
arguments(idFor(0), expired, true, null),
arguments(idFor(0), notExpired, true, new Badge(idFor(0), imageUrlFor(0), nameFor(0), desriptionFor(0))),
arguments(idFor(1), expired, false, null),
arguments(idFor(1), notExpired, false, null),
arguments(idFor(1), expired, true, null),
arguments(idFor(1), notExpired, true, null)
);
}
@ -161,7 +161,7 @@ public class ConfiguredProfileBadgeConverterTest {
ArgumentCaptor<Control> controlArgumentCaptor = setupResourceBundle(enGb);
badgeConverter.convert(List.of(enGb, en, esUs),
List.of(new AccountBadge(nameFor(0), Instant.ofEpochSecond(43), true)));
List.of(new AccountBadge(idFor(0), Instant.ofEpochSecond(43), true)));
Control control = controlArgumentCaptor.getValue();
assertThatNullPointerException().isThrownBy(() -> control.getFormats(null));
@ -186,7 +186,7 @@ public class ConfiguredProfileBadgeConverterTest {
// this should always terminate at the system default locale since the development defined bundle should get
// returned at that point anyhow
badgeConverter.convert(List.of(enGb, Locale.getDefault(), en, esUs),
List.of(new AccountBadge(nameFor(0), Instant.ofEpochSecond(43), true)));
List.of(new AccountBadge(idFor(0), Instant.ofEpochSecond(43), true)));
Control control2 = controlArgumentCaptor.getValue();
assertThat(control2.getFallbackLocale(ConfiguredProfileBadgeConverter.BASE_NAME, enGb)).isEqualTo(

View File

@ -104,7 +104,7 @@ class ProfileControllerTest {
(acceptableLanguages, accountBadges) -> {
try {
return List.of(
new Badge(new URL("https://example.com/badge/1"), "Test Badge", "This badge is in unit tests.")
new Badge("TEST1", new URL("https://example.com/badge/1"), "Test Badge", "This badge is in unit tests.")
);
} catch (MalformedURLException e) {
throw new AssertionError(e);