/*
 * 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 putFeature() {
    assertEquals(0, inMemoryCache.getFeatures().size());
    inMemoryCache.putFeature(feature);
    assertEquals(1, inMemoryCache.getFeatures().size());
    assertEquals(feature, inMemoryCache.getFeatures().asList().get(0));
  }

  @Test
  public void removeFeature() {
    inMemoryCache.putFeature(feature);
    assertEquals(1, inMemoryCache.getFeatures().size());
    inMemoryCache.removeFeature(feature.getId());
    assertEquals(0, inMemoryCache.getFeatures().size());
  }

  @Test
  public void setActiveProject() {
    assertNull(inMemoryCache.getActiveProject());
    inMemoryCache.setActiveProject(project);
    assertEquals(project, inMemoryCache.getActiveProject());
  }

  @Test
  public void clearFeaturesOnSetActiveProject() {
    inMemoryCache.putFeature(feature);
    assertEquals(1, inMemoryCache.getFeatures().size());
    inMemoryCache.setActiveProject(project);
    assertEquals(0, inMemoryCache.getFeatures().size());
  }

  @Test
  public void clearActiveProject() {
    inMemoryCache.setActiveProject(project);
    assertNotNull(inMemoryCache.getActiveProject());
    inMemoryCache.clearActiveProject();
    assertNull(inMemoryCache.getActiveProject());
  }

  @Test
  public void clearFeaturesOnClearActiveProject() {
    inMemoryCache.putFeature(feature);
    assertEquals(1, inMemoryCache.getFeatures().size());
    inMemoryCache.clearActiveProject();
    assertEquals(0, inMemoryCache.getFeatures().size());
  }
}

@@ -1722,26 +1722,26 @@
 ic void 
-pu
+ge
 tFeature
 () %7B%0A   
@@ -1728,24 +1728,42 @@
 d getFeature
+s_EmptyIfNoneAdded
 () %7B%0A    ass
@@ -1804,32 +1804,74 @@
 ures().size());%0A
+  %7D%0A%0A  @Test%0A  public void putFeature() %7B%0A
     inMemoryCach
@@ -2297,17 +2297,17 @@
 ic void 
-s
+g
 etActive
@@ -2305,32 +2305,44 @@
 getActiveProject
+_NullIfUnset
 () %7B%0A    assertN
@@ -2372,32 +2372,80 @@
 tiveProject());%0A
+  %7D%0A%0A  @Test%0A  public void setActiveProject() %7B%0A
     inMemoryCach
@@ -2565,39 +2565,39 @@
 oid 
-clearFeaturesOnSetActiveProject
+setActiveProject_ClearsFeatures
 () %7B
@@ -2640,65 +2640,8 @@
 e);%0A
-    assertEquals(1, inMemoryCache.getFeatures().size());%0A
     
@@ -2837,61 +2837,8 @@
 t);%0A
-    assertNotNull(inMemoryCache.getActiveProject());%0A
     
@@ -2959,36 +2959,36 @@
 lear
-FeaturesOnClearActiveProject
+ActiveProject_ClearsFeatures
 () %7B
@@ -3031,65 +3031,8 @@
 e);%0A
-    assertEquals(1, inMemoryCache.getFeatures().size());%0A
     

Refactor so each test tests exactly one behavior