@@ -0,0 +1,3428 @@
+// Copyright 2019 Google LLC%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     https://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0Apackage com.google.sps;%0A%0Aimport static org.mockito.Mockito.when;%0A%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.google.appengine.api.datastore.DatastoreService;%0Aimport com.google.appengine.api.datastore.DatastoreServiceFactory;%0Aimport com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;%0Aimport com.google.appengine.tools.development.testing.LocalServiceTestHelper;%0Aimport com.google.sps.servlets.SpendingAnalyticsServlet;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0Aimport java.util.HashMap;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.mockito.Mock;%0Aimport org.mockito.MockitoAnnotations;%0A%0Apublic final class SpendingAnalyticsServletTest %7B%0A  // Local Datastore%0A  private final LocalServiceTestHelper helper =%0A      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()).setEnvIsLoggedIn(true);%0A%0A  @Mock private SpendingAnalyticsServlet servlet;%0A  @Mock private HttpServletRequest request;%0A  @Mock private HttpServletResponse response;%0A%0A  private DatastoreService datastore;%0A  private StringWriter stringWriter;%0A  private PrintWriter writer;%0A%0A  @Before%0A  public void setUp() throws IOException %7B%0A    MockitoAnnotations.initMocks(this);%0A    helper.setUp();%0A    datastore = DatastoreServiceFactory.getDatastoreService();%0A%0A    servlet = new SpendingAnalyticsServlet(datastore);%0A%0A    stringWriter = new StringWriter();%0A    writer = new PrintWriter(stringWriter);%0A    when(response.getWriter()).thenReturn(writer);%0A  %7D%0A%0A  @After%0A  public void tearDown() %7B%0A    helper.tearDown();%0A  %7D%0A%0A  @Test%0A  public void doGetWithReceiptsInDatastore() throws IOException %7B%0A    // Receipts in datastore:%0A    // Walmart: $26.12, Contoso: $14.51, Main Street Restaurant: $29.01%0A    TestUtils.addTestReceipts(datastore);%0A%0A    // Perform doGet.%0A    servlet.doGet(request, response);%0A    writer.flush();%0A%0A    // Make sure all stores returned in HashMap.%0A    HashMap%3CString, Double%3E storeAnalytics =%0A        new ObjectMapper().readValue(stringWriter.toString(), HashMap.class);%0A    Assert.assertEquals(3, storeAnalytics.size());%0A    Assert.assertTrue(storeAnalytics.containsKey(%22Walmart%22));%0A    Assert.assertTrue(storeAnalytics.containsKey(%22Contoso%22));%0A    Assert.assertTrue(storeAnalytics.containsKey(%22Main Street Restaurant%22));%0A  %7D%0A%0A  @Test%0A  public void doGetWithNoReceiptsInDatastore() throws IOException %7B%0A    // Perform doGet.%0A    servlet.doGet(request, response);%0A    writer.flush();%0A%0A    // Make sure empty HashMap is returned.%0A    HashMap%3CString, Double%3E storeAnalytics =%0A        new ObjectMapper().readValue(stringWriter.toString(), HashMap.class);%0A    Assert.assertTrue(storeAnalytics.isEmpty());%0A  %7D%0A%7D

Add spending analytics servlet tests