CardModelAdapter { public static JsonObject serialize(CardModel cardModel) { JsonObject result = new JsonObject(); result.addProperty("iconType", cardModel.iconType().name().toLowerCase()); result.addProperty("iconSrc", cardModel.iconSrc()); if (cardModel.description().getLeft().isPresent() || cardModel.description().getRight().isPresent()) { result.add("description", getTextAndUrl(cardModel.description())); } if (cardModel.content().size() != 0) { result.add("content", getContent(cardModel.content())); } return result; }  static JsonObject serialize(CardModel cardModel); static JsonArray serialize(List<CardModel> cards);  }
@Test void emptyListReturnsEmptyJsonArray() { assertThat(CardModelAdapter.serialize(Collections.emptyList())) .isEmpty(); }
@Test void cardModelWithoutDescriptionOrContent() { CardModel cardModel = getRandomCardModel(false, 0, false); JsonObject result = CardModelAdapter.serialize(cardModel); assertThat(result.has("iconType")).isTrue(); assertThat(result.has("iconSrc")).isTrue(); assertThat(result.has("description")).isFalse(); assertThat(result.has("content")).isFalse(); }
@Test void cardModelWithContentWithoutUrls() { CardModel cardModel = getRandomCardModel(true, 3, false); JsonObject result = CardModelAdapter.serialize(cardModel); assertThat(result.has("description")).isTrue(); JsonArray contentResult = result.get("content").getAsJsonArray(); assertThat(contentResult.size()).isEqualTo(3); contentResult.forEach(x -> { assertThat(x.getAsJsonObject().has("text")) .isTrue(); assertThat(x.getAsJsonObject().has("url")) .isFalse(); }); }
@Test void cardModelWithFullContent() { CardModel cardModel = getRandomCardModel(true, 2, true); JsonObject result = CardModelAdapter.serialize(cardModel); JsonArray contentResult = result.get("content").getAsJsonArray(); assertThat(contentResult.size()).isEqualTo(2); contentResult.forEach(x -> { assertThat(x.getAsJsonObject().has("text")) .isTrue(); assertThat(x.getAsJsonObject().has("url")) .isTrue(); }); }
@Test void cardModelsWithRandomContent() { int numberOfCardModels = RANDOM.nextInt(200); List<CardModel> cardModels = getRandomCardModels(numberOfCardModels); JsonArray result = CardModelAdapter.serialize(cardModels); assertThat(result.size()).isEqualTo(numberOfCardModels); result.forEach(x -> { assertThat(x.getAsJsonObject().has("iconType")).isTrue(); assertThat(x.getAsJsonObject().has("iconSrc")).isTrue(); }); }