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() .filter(accountBadge -> accountBadge.isVisible()
&& now.isBefore(accountBadge.getExpiration()) && now.isBefore(accountBadge.getExpiration())
&& knownBadges.containsKey(accountBadge.getId())) && 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() + "_name"),
resourceBundle.getString(accountBadge.getId() + "_description"))) resourceBundle.getString(accountBadge.getId() + "_description")))
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -11,20 +11,27 @@ import java.net.URL;
import java.util.Objects; import java.util.Objects;
public class Badge { public class Badge {
private final String id;
private final URL imageUrl; private final URL imageUrl;
private final String name; private final String name;
private final String description; private final String description;
@JsonCreator @JsonCreator
public Badge( public Badge(
@JsonProperty("id") final String id,
@JsonProperty("imageUrl") final URL imageUrl, @JsonProperty("imageUrl") final URL imageUrl,
@JsonProperty("name") final String name, @JsonProperty("name") final String name,
@JsonProperty("description") final String description) { @JsonProperty("description") final String description) {
this.id = id;
this.imageUrl = imageUrl; this.imageUrl = imageUrl;
this.name = name; this.name = name;
this.description = description; this.description = description;
} }
public String getId() {
return id;
}
public URL getImageUrl() { public URL getImageUrl() {
return imageUrl; return imageUrl;
} }
@ -46,12 +53,13 @@ public class Badge {
return false; return false;
} }
Badge badge = (Badge) o; Badge badge = (Badge) o;
return Objects.equals(imageUrl, badge.imageUrl) && Objects.equals(name, return Objects.equals(id, badge.id) && Objects.equals(imageUrl,
badge.name) && Objects.equals(description, badge.description); badge.imageUrl) && Objects.equals(name, badge.name) && Objects.equals(
description, badge.description);
} }
@Override @Override
public int hashCode() { 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)); when(clock.instant()).thenReturn(Instant.ofEpochSecond(42));
} }
private static String nameFor(int i) { private static String idFor(int i) {
return "Badge-" + 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; return "TRANSLATED NAME " + i;
} }
private static String rbDesriptionFor(int i) { private static String desriptionFor(int i) {
return "TRANSLATED DESCRIPTION " + i; return "TRANSLATED DESCRIPTION " + i;
} }
private static BadgeConfiguration newBadge(int 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) { private BadgesConfiguration createBadges(int count) {
@ -80,8 +80,8 @@ public class ConfiguredProfileBadgeConverterTest {
Object[][] objects = new Object[count * 2][2]; Object[][] objects = new Object[count * 2][2];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
badges.add(newBadge(i)); badges.add(newBadge(i));
objects[(i * 2)] = new Object[]{nameFor(i) + "_name", rbNameFor(i)}; objects[(i * 2)] = new Object[]{idFor(i) + "_name", nameFor(i)};
objects[(i * 2) + 1] = new Object[]{nameFor(i) + "_description", rbDesriptionFor(i)}; objects[(i * 2) + 1] = new Object[]{idFor(i) + "_description", desriptionFor(i)};
} }
resourceBundle = new ListResourceBundle() { resourceBundle = new ListResourceBundle() {
@Override @Override
@ -94,7 +94,7 @@ public class ConfiguredProfileBadgeConverterTest {
private BadgeConfiguration getBadge(BadgesConfiguration badgesConfiguration, int i) { private BadgeConfiguration getBadge(BadgesConfiguration badgesConfiguration, int i) {
return badgesConfiguration.getBadges().stream() return badgesConfiguration.getBadges().stream()
.filter(badgeConfiguration -> nameFor(i).equals(badgeConfiguration.getName())) .filter(badgeConfiguration -> idFor(i).equals(badgeConfiguration.getName()))
.findFirst().orElse(null); .findFirst().orElse(null);
} }
@ -137,14 +137,14 @@ public class ConfiguredProfileBadgeConverterTest {
Instant expired = Instant.ofEpochSecond(41); Instant expired = Instant.ofEpochSecond(41);
Instant notExpired = Instant.ofEpochSecond(43); Instant notExpired = Instant.ofEpochSecond(43);
return Stream.of( return Stream.of(
arguments(nameFor(0), expired, false, null), arguments(idFor(0), expired, false, null),
arguments(nameFor(0), notExpired, false, null), arguments(idFor(0), notExpired, false, null),
arguments(nameFor(0), expired, true, null), arguments(idFor(0), expired, true, null),
arguments(nameFor(0), notExpired, true, new Badge(imageUrlFor(0), rbNameFor(0), rbDesriptionFor(0))), arguments(idFor(0), notExpired, true, new Badge(idFor(0), imageUrlFor(0), nameFor(0), desriptionFor(0))),
arguments(nameFor(1), expired, false, null), arguments(idFor(1), expired, false, null),
arguments(nameFor(1), notExpired, false, null), arguments(idFor(1), notExpired, false, null),
arguments(nameFor(1), expired, true, null), arguments(idFor(1), expired, true, null),
arguments(nameFor(1), notExpired, true, null) arguments(idFor(1), notExpired, true, null)
); );
} }
@ -161,7 +161,7 @@ public class ConfiguredProfileBadgeConverterTest {
ArgumentCaptor<Control> controlArgumentCaptor = setupResourceBundle(enGb); ArgumentCaptor<Control> controlArgumentCaptor = setupResourceBundle(enGb);
badgeConverter.convert(List.of(enGb, en, esUs), 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(); Control control = controlArgumentCaptor.getValue();
assertThatNullPointerException().isThrownBy(() -> control.getFormats(null)); 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 // this should always terminate at the system default locale since the development defined bundle should get
// returned at that point anyhow // returned at that point anyhow
badgeConverter.convert(List.of(enGb, Locale.getDefault(), en, esUs), 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(); Control control2 = controlArgumentCaptor.getValue();
assertThat(control2.getFallbackLocale(ConfiguredProfileBadgeConverter.BASE_NAME, enGb)).isEqualTo( assertThat(control2.getFallbackLocale(ConfiguredProfileBadgeConverter.BASE_NAME, enGb)).isEqualTo(

View File

@ -104,7 +104,7 @@ class ProfileControllerTest {
(acceptableLanguages, accountBadges) -> { (acceptableLanguages, accountBadges) -> {
try { try {
return List.of( 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) { } catch (MalformedURLException e) {
throw new AssertionError(e); throw new AssertionError(e);