/*
 * Copyright 2021 Google LLC
 *
 * 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.android.ground.repository;

import static com.google.android.ground.FakeData.newJob;
import static com.google.android.ground.FakeData.newSurvey;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.google.android.ground.BaseHiltTest;
import com.google.android.ground.model.Role;
import com.google.android.ground.model.Survey;
import com.google.android.ground.model.job.Job;
import com.google.android.ground.persistence.local.LocalDataStore;
import com.google.android.ground.persistence.local.LocalDataStoreModule;
import com.google.android.ground.persistence.remote.FakeRemoteDataStore;
import com.google.common.collect.ImmutableList;
import dagger.hilt.android.testing.BindValue;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.UninstallModules;
import io.reactivex.Maybe;
import java8.util.Optional;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;

@HiltAndroidTest
@UninstallModules({LocalDataStoreModule.class})
@RunWith(RobolectricTestRunner.class)
public class SurveyRepositoryTest extends BaseHiltTest {

  @BindValue @Mock LocalDataStore mockLocalDataStore;
  @BindValue @Mock UserRepository userRepository;

  @Inject SurveyRepository surveyRepository;
  @Inject FakeRemoteDataStore fakeRemoteDataStore;

  @Test
  public void testActivateSurvey() {
    Survey survey = newSurvey().build();
    setTestSurvey(survey);

    surveyRepository.activateSurvey("id");

    surveyRepository.getActiveSurvey().test().assertValue(Optional.of(survey));
  }

  @Test
  public void testActivateSurvey_managersCanAddFeaturesToAllJobs() {
    Job job = newJob().setId("job").build();
    setTestSurvey(newSurvey().putJob(job).build());
    when(userRepository.getUserRole(any())).thenReturn(Role.SURVEY_ORGANIZER);

    surveyRepository.activateSurvey("id");

    surveyRepository
        .getActiveSurvey()
        .test()
        .assertValue(p -> p.get().getJobs().equals(ImmutableList.of(job)));
  }

  @Test
  public void testActivateSurvey_ownersCanAddFeaturesToAllJobs() {
    Job job = newJob().setId("job").build();
    setTestSurvey(newSurvey().putJob(job).build());
    when(userRepository.getUserRole(any())).thenReturn(Role.OWNER);

    surveyRepository.activateSurvey("id");

    surveyRepository
        .getActiveSurvey()
        .test()
        .assertValue(p -> p.get().getJobs().equals(ImmutableList.of(job)));
  }

  @Test
  public void testActivateSurvey_contributorsCannotAddFeaturesToAnyJobs() {
    Job job = newJob().setId("job").build();
    setTestSurvey(newSurvey().putJob(job).build());
    when(userRepository.getUserRole(any())).thenReturn(Role.DATA_COLLECTOR);

    surveyRepository.activateSurvey("id");

    surveyRepository
        .getActiveSurvey()
        .test()
        .assertValue(p -> p.get().getJobs().equals(ImmutableList.of(job)));
  }

  private void setTestSurvey(Survey survey) {
    fakeRemoteDataStore.setTestSurvey(survey);
    when(mockLocalDataStore.getSurveyById(any())).thenReturn(Maybe.just(survey));
  }
}

@@ -640,65 +640,8 @@
 y;%0A%0A
-import static com.google.android.ground.FakeData.newJob;%0A
 impo
@@ -692,24 +692,24 @@
 .newSurvey;%0A
+
 import stati
@@ -836,53 +836,8 @@
 st;%0A
-import com.google.android.ground.model.Role;%0A
 impo
@@ -883,56 +883,8 @@
 ey;%0A
-import com.google.android.ground.model.job.Job;%0A
 impo
@@ -1096,56 +1096,8 @@
 re;%0A
-import com.google.common.collect.ImmutableList;%0A
 impo
@@ -1967,32 +1967,32 @@
 eSurvey(%22id%22);%0A%0A
+
     surveyReposi
@@ -2064,1329 +2064,8 @@
  %7D%0A%0A
-  @Test%0A  public void testActivateSurvey_managersCanAddFeaturesToAllJobs() %7B%0A    Job job = newJob().setId(%22job%22).build();%0A    setTestSurvey(newSurvey().putJob(job).build());%0A    when(userRepository.getUserRole(any())).thenReturn(Role.SURVEY_ORGANIZER);%0A%0A    surveyRepository.activateSurvey(%22id%22);%0A%0A    surveyRepository%0A        .getActiveSurvey()%0A        .test()%0A        .assertValue(p -%3E p.get().getJobs().equals(ImmutableList.of(job)));%0A  %7D%0A%0A  @Test%0A  public void testActivateSurvey_ownersCanAddFeaturesToAllJobs() %7B%0A    Job job = newJob().setId(%22job%22).build();%0A    setTestSurvey(newSurvey().putJob(job).build());%0A    when(userRepository.getUserRole(any())).thenReturn(Role.OWNER);%0A%0A    surveyRepository.activateSurvey(%22id%22);%0A%0A    surveyRepository%0A        .getActiveSurvey()%0A        .test()%0A        .assertValue(p -%3E p.get().getJobs().equals(ImmutableList.of(job)));%0A  %7D%0A%0A  @Test%0A  public void testActivateSurvey_contributorsCannotAddFeaturesToAnyJobs() %7B%0A    Job job = newJob().setId(%22job%22).build();%0A    setTestSurvey(newSurvey().putJob(job).build());%0A    when(userRepository.getUserRole(any())).thenReturn(Role.DATA_COLLECTOR);%0A%0A    surveyRepository.activateSurvey(%22id%22);%0A%0A    surveyRepository%0A        .getActiveSurvey()%0A        .test()%0A        .assertValue(p -%3E p.get().getJobs().equals(ImmutableList.of(job)));%0A  %7D%0A%0A
   pr

Remove obsolete tests