From 315eca7c5eb87d8604a8a26e7793e1e878822bf5 Mon Sep 17 00:00:00 2001 From: Adetunji Dahunsi Date: Fri, 18 Feb 2022 10:12:10 -0500 Subject: [PATCH] Add tests for serializing NewsResourceTypes from netwrok and to database Change-Id: Id43d49217cfb3147072dc559cd4dfc2305ddddb1 --- .../core/database/util/Converters.kt | 5 +- .../util/NewsResourceTypeConverterTest.kt | 96 ++++++++++++++++++ .../util/NewsResourceTypeSerializerTest.kt | 97 +++++++++++++++++++ 3 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 core-domain/src/test/java/com/google/samples/apps/nowinandroid/core/database/util/NewsResourceTypeConverterTest.kt create mode 100644 core-network/src/test/java/com/google/samples/apps/nowinandroid/core/network/model/util/NewsResourceTypeSerializerTest.kt diff --git a/core-database/src/main/java/com/google/samples/apps/nowinandroid/core/database/util/Converters.kt b/core-database/src/main/java/com/google/samples/apps/nowinandroid/core/database/util/Converters.kt index e09915d46..eff1742ee 100644 --- a/core-database/src/main/java/com/google/samples/apps/nowinandroid/core/database/util/Converters.kt +++ b/core-database/src/main/java/com/google/samples/apps/nowinandroid/core/database/util/Converters.kt @@ -34,8 +34,9 @@ class InstantConverter { class NewsResourceTypeConverter { @TypeConverter fun newsResourceTypeToString(value: NewsResourceType?): String? = - value?.let(NewsResourceType::name) + value?.let(NewsResourceType::serializedName) @TypeConverter - fun stringToNewsResourceType(name: String?): NewsResourceType = name.asNewsResourceType() + fun stringToNewsResourceType(serializedName: String?): NewsResourceType = + serializedName.asNewsResourceType() } diff --git a/core-domain/src/test/java/com/google/samples/apps/nowinandroid/core/database/util/NewsResourceTypeConverterTest.kt b/core-domain/src/test/java/com/google/samples/apps/nowinandroid/core/database/util/NewsResourceTypeConverterTest.kt new file mode 100644 index 000000000..4a52bd5b7 --- /dev/null +++ b/core-domain/src/test/java/com/google/samples/apps/nowinandroid/core/database/util/NewsResourceTypeConverterTest.kt @@ -0,0 +1,96 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid.core.database.util + +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType +import org.junit.Assert.assertEquals +import org.junit.Test + +class NewsResourceTypeConverterTest { + + @Test + fun test_room_news_resource_type_converter_for_video() { + assertEquals( + NewsResourceType.Video, + NewsResourceTypeConverter().stringToNewsResourceType("Video 📺") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_article() { + assertEquals( + NewsResourceType.Article, + NewsResourceTypeConverter().stringToNewsResourceType("Article 📚") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_api_change() { + assertEquals( + NewsResourceType.APIChange, + NewsResourceTypeConverter().stringToNewsResourceType("API change") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_codelab() { + assertEquals( + NewsResourceType.Codelab, + NewsResourceTypeConverter().stringToNewsResourceType("Codelab") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_podcast() { + assertEquals( + NewsResourceType.Podcast, + NewsResourceTypeConverter().stringToNewsResourceType("Podcast 🎙") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_docs() { + assertEquals( + NewsResourceType.Docs, + NewsResourceTypeConverter().stringToNewsResourceType("Docs 📑") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_event() { + assertEquals( + NewsResourceType.Event, + NewsResourceTypeConverter().stringToNewsResourceType("Event 📆") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_dac() { + assertEquals( + NewsResourceType.DAC, + NewsResourceTypeConverter().stringToNewsResourceType("DAC") + ) + } + + @Test + fun test_room_news_resource_type_converter_for_umm() { + assertEquals( + NewsResourceType.Unknown, + NewsResourceTypeConverter().stringToNewsResourceType("umm") + ) + } +} diff --git a/core-network/src/test/java/com/google/samples/apps/nowinandroid/core/network/model/util/NewsResourceTypeSerializerTest.kt b/core-network/src/test/java/com/google/samples/apps/nowinandroid/core/network/model/util/NewsResourceTypeSerializerTest.kt new file mode 100644 index 000000000..20d12bc51 --- /dev/null +++ b/core-network/src/test/java/com/google/samples/apps/nowinandroid/core/network/model/util/NewsResourceTypeSerializerTest.kt @@ -0,0 +1,97 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid.core.network.model.util + +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType +import kotlinx.serialization.json.Json +import org.junit.Assert.assertEquals +import org.junit.Test + +class NewsResourceTypeSerializerTest { + + @Test + fun test_news_resource_serializer_video() { + assertEquals( + NewsResourceType.Video, + Json.decodeFromString(NewsResourceTypeSerializer, """"Video 📺"""") + ) + } + + @Test + fun test_news_resource_serializer_article() { + assertEquals( + NewsResourceType.Article, + Json.decodeFromString(NewsResourceTypeSerializer, """"Article 📚"""") + ) + } + + @Test + fun test_news_resource_serializer_api_change() { + assertEquals( + NewsResourceType.APIChange, + Json.decodeFromString(NewsResourceTypeSerializer, """"API change"""") + ) + } + + @Test + fun test_news_resource_serializer_codelab() { + assertEquals( + NewsResourceType.Codelab, + Json.decodeFromString(NewsResourceTypeSerializer, """"Codelab"""") + ) + } + + @Test + fun test_news_resource_serializer_podcast() { + assertEquals( + NewsResourceType.Podcast, + Json.decodeFromString(NewsResourceTypeSerializer, """"Podcast 🎙"""") + ) + } + + @Test + fun test_news_resource_serializer_docs() { + assertEquals( + NewsResourceType.Docs, + Json.decodeFromString(NewsResourceTypeSerializer, """"Docs 📑"""") + ) + } + + @Test + fun test_news_resource_serializer_event() { + assertEquals( + NewsResourceType.Event, + Json.decodeFromString(NewsResourceTypeSerializer, """"Event 📆"""") + ) + } + + @Test + fun test_news_resource_serializer_dac() { + assertEquals( + NewsResourceType.DAC, + Json.decodeFromString(NewsResourceTypeSerializer, """"DAC"""") + ) + } + + @Test + fun test_news_resource_serializer_unknown() { + assertEquals( + NewsResourceType.Unknown, + Json.decodeFromString(NewsResourceTypeSerializer, """"umm"""") + ) + } +}