package net.pokeranalytics.android

import io.realm.Realm
import io.realm.log.RealmLog
import net.pokeranalytics.android.model.realm.Session
import org.junit.After
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito
import org.powermock.api.mockito.PowerMockito.`when`
import org.powermock.api.mockito.PowerMockito.mockStatic
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor
import org.powermock.modules.junit4.rule.PowerMockRule
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config


@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, sdk = [19])
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "android.*")
@SuppressStaticInitializationFor("io.realm.internal.Util")
@PrepareForTest(Realm::class, RealmLog::class)
open class RealmUnitTest {

	@get:Rule
	var rule = PowerMockRule()

	lateinit var mockRealm: Realm

	@Before
	fun setup() {
		mockStatic(RealmLog::class.java)
		mockStatic(Realm::class.java)

		val mockRealm = PowerMockito.mock(Realm::class.java)
		`when`(Realm.getDefaultInstance()).thenReturn(mockRealm)
		this.mockRealm = mockRealm
	}

	@Test
	fun shouldBeAbleToCreateARealmObject() {
		val session = Session()
		`when`(mockRealm.createObject(Session::class.java)).thenReturn(session)
		val output = mockRealm.createObject(Session::class.java)
		assertThat(output, org.hamcrest.CoreMatchers.`is`(session))
	}

	@After
	@Throws(Exception::class)
	fun tearDown() {
		mockRealm.close()
	}

}