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 #5: GAE Abstraction part 1 - Topic Page handler [Blocked: #85] #78

Merged
merged 29 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7269d15
Initial introduction of the data source module.
BenHenning Aug 27, 2019
ac16d10
Data module dependencies, retrofit and models
Aug 27, 2019
bf96010
Removed kapt from build.gradle in data
Aug 27, 2019
ef62740
Javadoc comments
Aug 27, 2019
93d703b
Introduce one possible integration with Dagger 2.
BenHenning Aug 28, 2019
3679c4c
Javadoc comments added
Aug 28, 2019
a4e0ad5
Reolved Javadoc mistakes
Aug 28, 2019
d187b56
Update build.gradle proto directive to use 'compile' instead of (#81)
Aug 28, 2019
d27d161
Merge remote-tracking branch 'upstream/introduce-data-module' into ga…
Aug 28, 2019
5d5da22
Renaming test cases
Aug 28, 2019
07b51d5
Test cases for TopicService
Aug 30, 2019
a52bb57
Made changes to OppiaGaeClient
Aug 30, 2019
1d6c39b
Made changes to OppiaGaeClient
Aug 30, 2019
b476e8e
TopicService test code using Json response
Sep 3, 2019
4aadf7a
Revert changes in codeStyles/Project.xml
Sep 3, 2019
d03a527
Revert changes in codeStyles/Project.xml
Sep 3, 2019
a380eaa
Optimise imports
Sep 3, 2019
1da9475
Check NetworkInterceptor part
Sep 3, 2019
842dc21
Fix part of #9: Introduce Retrofit classroom data handler service (#83)
veena14cs Sep 4, 2019
ffde992
Resolve typo
Sep 5, 2019
f5ae749
Merge branch 'gae-abstraction-base' of https://github.com/oppia/oppia…
Sep 5, 2019
0049548
Resolve merge conflicts with develop branch
Sep 5, 2019
cd94945
NetworkModule introduced
Sep 5, 2019
e392406
Daggerify NetworkInterceptor
Sep 5, 2019
5eccceb
Make retrofit dependency as api
Sep 5, 2019
1751f36
Javadocs and linking of oppia-web to models
Sep 5, 2019
798cf41
Changes in Javadoc, naming and test cases code
Sep 6, 2019
80249a8
Merge remote-tracking branch 'upstream/develop' into gae-abstraction-…
Sep 10, 2019
bb62037
Updated TODO comments and NetworkInterceptorTest
Sep 10, 2019
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
Prev Previous commit
Next Next commit
TopicService test code using Json response
  • Loading branch information
Rajat Talesra committed Sep 3, 2019
commit b476e8eb4845ae55ccdbab047d85d8b9fbf50f5c
15 changes: 15 additions & 0 deletions data/src/test/java/org/oppia/data/FakeJsonResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.oppia.data

import org.oppia.data.backends.gae.NetworkSettings

/** An object that contains fake json responses for test cases */
object FakeJsonResponse {

/** Dummy json response with XSSI Prfix for [NetworkInterceptorTest] */
const val DUMMY_RESPONSE_WITH_XSSI_PREFIX: String = NetworkSettings.XSSI_PREFIX + "\n" + "{\"is_valid_response\": true}"
/** Dummy json response without XSSI Prfix for [NetworkInterceptorTest] */
const val DUMMY_RESPONSE_WITHOUT_XSSI_PREFIX: String = "{\"is_valid_response\": true}"
/** Fake json response for [MockTopicService] */
const val TOPIC_SERVICE_RESPONSE: String = "{\"canonical_story_dicts\":[{\"title\":\"Story 1\",\"description\":\"Story Description\",\"id\":\"4wv9hSZ4F67I\"}],\"is_moderator\":true,\"is_admin\":true,\"topic_name\":\"Topic1\",\"username\":\"rt4914\",\"skill_descriptions\":{\"A9j9taXAqqkV\":\"Skill 1\"},\"user_email\":\"test@example.com\",\"iframed\":false,\"additional_story_dicts\":[],\"additional_angular_modules\":[],\"is_topic_manager\":false,\"uncategorized_skill_ids\":[],\"topic_id\":\"baWJOUFeUAnn\",\"degrees_of_mastery\":{\"A9j9taXAqqkV\":null},\"subtopics\":[{\"skill_ids\":[\"A9j9taXAqqkV\"],\"title\":\"Subtopic 1\",\"id\":1}],\"is_super_admin\":true}"

}
28 changes: 15 additions & 13 deletions data/src/test/java/org/oppia/data/MockTopicService.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package org.oppia.data

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import org.oppia.data.backends.gae.api.TopicService
import org.oppia.data.backends.gae.model.GaeTopic
import retrofit2.Call
import retrofit2.mock.BehaviorDelegate

/**
* Mock TopicService with dummy data
* Mock TopicService with dummy data from [FakeJsonResponse]
*/
class MockTopicService(private val delegate: BehaviorDelegate<TopicService>) : TopicService {
override fun getTopicByName(topicName: String): Call<GaeTopic> {
val topic = GaeTopic(
"1",
"Test Topic",
null,
null,
null,
null,
null,
null
)
return delegate.returningResponse(topic).getTopicByName("Test Topic")
val topic = MockGaeTopic()
return delegate.returningResponse(topic).getTopicByName(topicName)
}
}

private fun MockGaeTopic(): Any? {
val jsonResponse: String = FakeJsonResponse.TOPIC_SERVICE_RESPONSE

val moshi = Moshi.Builder().build()
val adapter: JsonAdapter<GaeTopic> = moshi.adapter(GaeTopic::class.java)
val mockGaeTopic = adapter.fromJson(jsonResponse)

return mockGaeTopic!!
}
}
16 changes: 4 additions & 12 deletions data/src/test/java/org/oppia/data/MockTopicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.data.backends.gae.NetworkInterceptor
import org.oppia.data.backends.gae.OppiaGaeClient
import org.oppia.data.backends.gae.api.TopicService
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
Expand All @@ -24,16 +25,7 @@ class MockTopicTest {
@Before
@Throws(Exception::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? I thought this is implied in Kotlin since it doesn't require using checked exceptions. I thought this only serves to set Kotlin methods up for Java use, but in this case I believe JUnit methods are called reflectively and are assumed to throw exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did search this on internet but haven't found a proper answer for this. I have seen different resources which uses this and there are some resources which do not use this.

Even I am not sure, whether we actually need this or not.

fun setUp() {
val networkInterceptor = NetworkInterceptor()

val client = OkHttpClient.Builder()
client.addInterceptor(networkInterceptor)

retrofit = Retrofit.Builder()
.baseUrl("https://www.testoppia.com")
.addConverterFactory(MoshiConverterFactory.create())
.client(client.build())
.build()
retrofit = OppiaGaeClient.retrofitInstance

val behavior = NetworkBehavior.create()
mockRetrofit = MockRetrofit.Builder(retrofit!!)
Expand All @@ -47,10 +39,10 @@ class MockTopicTest {
val delegate = mockRetrofit!!.create(TopicService::class.java)
val mockTopicService = MockTopicService(delegate)

val topic = mockTopicService.getTopicByName("Test Topic")
val topic = mockTopicService.getTopicByName("Topic1")
val topicResponse = topic.execute()

assertThat(topicResponse.isSuccessful).isTrue()
assertThat("Test Topic").isEqualTo(topicResponse.body()!!.topicName)
assertThat("Topic1").isEqualTo(topicResponse.body()!!.topicName)
}
}
16 changes: 6 additions & 10 deletions data/src/test/java/org/oppia/data/NetworkInterceptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@ import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.data.backends.gae.NetworkInterceptor
import org.oppia.data.backends.gae.NetworkSettings

private const val FAKE_RESPONSE_WITHOUT_XSSI_PREFIX: String = "{\"is_valid_response\": true}"
private const val FAKE_RESPONSE_WITH_XSSI_PREFIX: String =
NetworkSettings.XSSI_PREFIX + "\n" + "{\"is_valid_response\": true}"

/** Tests for [NetworkInterceptor] */
@RunWith(AndroidJUnit4::class)
class NetworkInterceptorTest {

@Test
fun testNetworkInterceptor_withXssiPrefix_removesXssiPrefix() {
val networkInterceptor = NetworkInterceptor()
val rawJson: String = networkInterceptor.removeXSSIPrefix(FAKE_RESPONSE_WITH_XSSI_PREFIX)
val rawJson: String =
networkInterceptor.removeXSSIPrefix(FakeJsonResponse.DUMMY_RESPONSE_WITH_XSSI_PREFIX)

assertThat(rawJson).isEqualTo(FAKE_RESPONSE_WITHOUT_XSSI_PREFIX)
assertThat(rawJson).isEqualTo(FakeJsonResponse.DUMMY_RESPONSE_WITHOUT_XSSI_PREFIX)
}

@Test
fun testNetworkInterceptor_withoutXssiPrefix_removesXssiPrefix() {
val networkInterceptor = NetworkInterceptor()
val rawJson: String = networkInterceptor.removeXSSIPrefix(FAKE_RESPONSE_WITHOUT_XSSI_PREFIX)
val rawJson: String =
networkInterceptor.removeXSSIPrefix(FakeJsonResponse.DUMMY_RESPONSE_WITHOUT_XSSI_PREFIX)

assertThat(rawJson).isEqualTo(FAKE_RESPONSE_WITHOUT_XSSI_PREFIX)
assertThat(rawJson).isEqualTo(FakeJsonResponse.DUMMY_RESPONSE_WITHOUT_XSSI_PREFIX)
}
}