package com.google.moviestvsentiments.service.assetSentiment;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import android.content.Context;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.moviestvsentiments.model.Asset;
import com.google.moviestvsentiments.model.AssetType;
import com.google.moviestvsentiments.service.database.SentimentsDatabase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class AssetSentimentDaoTest {

    @Rule
    public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

    private AssetSentimentDao assetSentimentDao;
    private SentimentsDatabase database;
    private Asset asset = Asset.builder().setId("assetId")
            .setType(AssetType.MOVIE).setTitle("assetTitle").setPoster("posterURL")
            .setBanner("bannerURL").setPlot("plotDescription").setRuntime("runtime").setYear("year")
            .setTimestamp(1).build();

    @Before
    public void createDatabase() {
        Context context = ApplicationProvider.getApplicationContext();
        database = Room.inMemoryDatabaseBuilder(context, SentimentsDatabase.class)
                .allowMainThreadQueries().build();
        assetSentimentDao = database.assetSentimentDao();
    }

    @After
    public void closeDatabase() {
        database.close();
    }

    @Test
    public void addAndGetAsset_withIncorrectId_returnsNull() {
        assetSentimentDao.addAsset(asset);
        Asset result = assetSentimentDao.getAsset("incorrectId", AssetType.MOVIE);

        assertNull(result);
    }

    @Test
    public void addAndGetAsset_withIncorrectType_returnsNull() {
        assetSentimentDao.addAsset(asset);
        Asset result = assetSentimentDao.getAsset("assetId", AssetType.SHOW);

        assertNull(result);
    }

    @Test
    public void addAndGetAsset_returnsAsset() {
        assetSentimentDao.addAsset(asset);
        Asset result = assetSentimentDao.getAsset("assetId", AssetType.MOVIE);

        assertEquals("assetTitle", result.title());
    }
}

@@ -760,220 +760,46 @@
     
-@Rule%0A    public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();%0A%0A    private AssetSentimentDao assetSentimentDao;%0A    private SentimentsDatabase database;%0A    private Asset asset
+private static final Asset MOVIE_ASSET
  = A
@@ -1050,24 +1050,218 @@
 ).build();%0A%0A
+    @Rule%0A    public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();%0A%0A    private AssetSentimentDao assetSentimentDao;%0A    private SentimentsDatabase database;%0A%0A
     @Before%0A
@@ -1743,37 +1743,43 @@
 entDao.addAsset(
-asset
+MOVIE_ASSET
 );%0A        Asset
@@ -1986,37 +1986,43 @@
 entDao.addAsset(
-asset
+MOVIE_ASSET
 );%0A        Asset
@@ -2215,21 +2215,27 @@
 ddAsset(
-asset
+MOVIE_ASSET
 );%0A     

Make test resource static final