// 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.sps;

import static org.mockito.Mockito.when;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.sps.servlets.SpendingAnalyticsServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public final class SpendingAnalyticsServletTest {
  private static final Map<String, Double> EXPECTED_STORES_ANALYTICS =
      Map.of("walmart", 26.12, "contoso", 14.51, "main street restaurant", 29.01);
  private static final Map<String, Double> EXPECTED_CATEGORY_ANALYTICS =
      Map.of("candy", 26.12, "drink", 26.12, "cappuccino", 14.51, "food", 43.52);

  // Local Datastore
  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()).setEnvIsLoggedIn(true);

  @Mock private SpendingAnalyticsServlet servlet;
  @Mock private HttpServletRequest request;
  @Mock private HttpServletResponse response;

  private DatastoreService datastore;
  private StringWriter stringWriter;
  private PrintWriter writer;

  @Before
  public void setUp() throws IOException {
    MockitoAnnotations.initMocks(this);
    helper.setUp();
    datastore = DatastoreServiceFactory.getDatastoreService();

    servlet = new SpendingAnalyticsServlet(datastore);

    stringWriter = new StringWriter();
    writer = new PrintWriter(stringWriter);
    when(response.getWriter()).thenReturn(writer);
  }

  @After
  public void tearDown() {
    helper.tearDown();
  }

  @Test
  public void doGetWithReceiptsInDatastore() throws IOException {
    // Receipts in datastore:
    // Walmart: $26.12, Contoso: $14.51, Main Street Restaurant: $29.01
    TestUtils.addTestReceipts(datastore);

    servlet.doGet(request, response);
    writer.flush();

    HashMap<String, Double> storeAnalytics = TestUtils.parseStoreAnalytics(stringWriter.toString());
    HashMap<String, Double> categoryAnalytics =
        TestUtils.parseCategoryAnalytics(stringWriter.toString());

    Assert.assertTrue(EXPECTED_STORES_ANALYTICS.equals(storeAnalytics));
    Assert.assertTrue(EXPECTED_CATEGORY_ANALYTICS.equals(categoryAnalytics));
  }

  @Test
  public void doGetWithNoReceiptsInDatastore() throws IOException {
    servlet.doGet(request, response);
    writer.flush();

    // Make sure empty HashMaps are returned.
    HashMap<String, Double> storeAnalytics = TestUtils.parseStoreAnalytics(stringWriter.toString());
    HashMap<String, Double> categoryAnalytics =
        TestUtils.parseCategoryAnalytics(stringWriter.toString());

    Assert.assertTrue(storeAnalytics.isEmpty());
    Assert.assertTrue(categoryAnalytics.isEmpty());
  }
}
@@ -1112,30 +1112,8 @@
 ap;%0A
-import java.util.Map;%0A
 impo
@@ -1150,24 +1150,24 @@
 letRequest;%0A
+
 import javax
@@ -1428,32 +1428,36 @@
 te static final 
+Hash
 Map%3CString, Doub
@@ -1466,33 +1466,32 @@
 %3E EXPECTED_STORE
-S
 _ANALYTICS =%0A   
@@ -1490,89 +1490,23 @@
 CS =
-%0A      Map.of(%22walmart%22, 26.12, %22contoso%22, 14.51, %22main street restaurant%22, 29.01
+ new HashMap%3C%3E(
 );%0A 
@@ -1527,16 +1527,20 @@
 c final 
+Hash
 Map%3CStri
@@ -1584,88 +1584,23 @@
 CS =
-%0A      Map.of(%22candy%22, 26.12, %22drink%22, 26.12, %22cappuccino%22, 14.51, %22food%22, 43.52
+ new HashMap%3C%3E(
 );%0A%0A
@@ -2378,16 +2378,405 @@
 writer);
+%0A%0A    EXPECTED_STORE_ANALYTICS.put(%22walmart%22, 26.12);%0A    EXPECTED_STORE_ANALYTICS.put(%22contoso%22, 14.51);%0A    EXPECTED_STORE_ANALYTICS.put(%22main street restaurant%22, 29.01);%0A%0A    EXPECTED_CATEGORY_ANALYTICS.put(%22candy%22, 26.12);%0A    EXPECTED_CATEGORY_ANALYTICS.put(%22drink%22, 26.12);%0A    EXPECTED_CATEGORY_ANALYTICS.put(%22cappuccino%22, 14.51);%0A    EXPECTED_CATEGORY_ANALYTICS.put(%22food%22, 43.52);
 %0A  %7D%0A%0A  
@@ -3324,32 +3324,32 @@
 r.toString());%0A%0A
+
     Assert.asser
@@ -3368,17 +3368,16 @@
 ED_STORE
-S
 _ANALYTI

Move expected hashmap setup to setup function