/*
 * Copyright 2019 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.gnd.repository;

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

import com.google.android.gnd.model.Project;
import com.google.android.gnd.model.feature.Feature;
import com.google.android.gnd.model.feature.Point;
import com.google.android.gnd.model.layer.Layer;
import org.junit.Before;
import org.junit.Test;

public class InMemoryCacheTest {

  private InMemoryCache inMemoryCache;
  private Project project;
  private Feature feature;

  @Before
  public void setUp() {
    inMemoryCache = new InMemoryCache();

    project =
        Project.newBuilder()
            .setId("foo project id")
            .setTitle("foo title")
            .setDescription("foo description")
            .build();

    feature =
        Feature.newBuilder()
            .setId("foo feature id")
            .setProject(Project.newBuilder().build())
            .setLayer(Layer.newBuilder().build())
            .setPoint(Point.newBuilder().setLatitude(0.0).setLongitude(0.0).build())
            .build();
  }

  @Test
  public void testPutFeature() {
    assertEquals(0, inMemoryCache.getFeatures().size());

    // save feature
    inMemoryCache.putFeature(feature);

    assertEquals(1, inMemoryCache.getFeatures().size());
    assertEquals(feature, inMemoryCache.getFeatures().asList().get(0));
  }

  @Test
  public void testRemoveFeature() {
    inMemoryCache.putFeature(feature);

    assertEquals(1, inMemoryCache.getFeatures().size());

    // remove feature
    inMemoryCache.removeFeature(feature.getId());

    assertEquals(0, inMemoryCache.getFeatures().size());
  }

  @Test
  public void testSetActiveProject() {
    inMemoryCache.putFeature(feature);

    assertNull(inMemoryCache.getActiveProject());
    assertEquals(1, inMemoryCache.getFeatures().size());

    // save project
    inMemoryCache.setActiveProject(project);

    assertEquals(project, inMemoryCache.getActiveProject());
    assertEquals(0, inMemoryCache.getFeatures().size());
  }

  @Test
  public void testClearActiveProject() {
    inMemoryCache.setActiveProject(project);
    inMemoryCache.putFeature(feature);

    assertNotNull(inMemoryCache.getActiveProject());
    assertEquals(1, inMemoryCache.getFeatures().size());

    // remove saved project
    inMemoryCache.clearActiveProject();

    assertNull(inMemoryCache.getActiveProject());
    assertEquals(0, inMemoryCache.getFeatures().size());
  }
}

@@ -1801,29 +1801,8 @@
 ());
-%0A%0A    // save feature
 %0A   
@@ -1829,33 +1829,32 @@
 ature(feature);%0A
-%0A
     assertEquals
@@ -2046,33 +2046,32 @@
 ature(feature);%0A
-%0A
     assertEquals
@@ -2114,31 +2114,8 @@
 ());
-%0A%0A    // remove feature
 %0A   
@@ -2157,25 +2157,24 @@
 e.getId());%0A
-%0A
     assertEq
@@ -2274,48 +2274,8 @@
 ) %7B%0A
-    inMemoryCache.putFeature(feature);%0A%0A
     
@@ -2312,32 +2312,244 @@
 tiveProject());%0A
+    inMemoryCache.setActiveProject(project);%0A    assertEquals(project, inMemoryCache.getActiveProject());%0A  %7D%0A%0A  @Test%0A  public void testClearFeaturesOnSetActiveProject() %7B%0A    inMemoryCache.putFeature(feature);%0A
     assertEquals
@@ -2592,29 +2592,8 @@
 ());
-%0A%0A    // save project
 %0A   
@@ -2637,70 +2637,8 @@
 ct);
-%0A%0A    assertEquals(project, inMemoryCache.getActiveProject());
 %0A   
@@ -2794,48 +2794,8 @@
 t);%0A
-    inMemoryCache.putFeature(feature);%0A%0A
     
@@ -2851,89 +2851,162 @@
     
-assertEquals(1, inMemoryCache.getFeatures().size());%0A%0A    // remove saved p
+inMemoryCache.clearActiveProject();%0A    assertNull(inMemoryCache.getActiveProject());%0A  %7D%0A%0A  @Test%0A  public void testClearFeaturesOnClearActiveP
 roject
+() %7B
 %0A   
@@ -3024,31 +3024,29 @@
 che.
-clearActiveProject(
+putFeature(feature
 );%0A
-%0A
     
@@ -3043,37 +3043,42 @@
 ure);%0A    assert
-Null(
+Equals(1, 
 inMemoryCache.ge
@@ -3070,32 +3070,75 @@
 nMemoryCache.get
+Features().size());%0A    inMemoryCache.clear
 ActiveProject())
@@ -3132,25 +3132,24 @@
 iveProject()
-)
 ;%0A    assert

Create new tests for testing individual behaviors