// 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 com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import com.google.sps.servlets.EventServlet;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.Mock;

/** */
@RunWith(JUnit4.class)
public final class EventServletTest {
  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  private HttpServletRequest mockedRequest;
  private HttpServletResponse mockedResponse;
  private EventServlet testEventServlet;

  @Before
  public void setUp() {
    helper.setUp();
    testEventServlet = new EventServlet();
  }

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

  @Test
  public void postOneEventToDatastore() throws IOException {
    HttpServletRequest request = mock(HttpServletRequest.class);       
    HttpServletResponse response = mock(HttpServletResponse.class);    

    when(request.getParameter("event-name")).thenReturn("Lake Clean Up");
    when(request.getParameter("event-description")).thenReturn("We're cleaning up the lake");
    when(request.getParameter("street-address")).thenReturn("678 Lakeview Way");
    when(request.getParameter("city")).thenReturn("Lakeside");
    when(request.getParameter("state")).thenReturn("Michigan");
    when(request.getParameter("date")).thenReturn("2020-17-05");
    when(request.getParameter("start-time")).thenReturn("14:00");
    when(request.getParameter("end-time")).thenReturn("15:00");
    when(request.getParameter("cover-photo")).thenReturn("/img-2030121");
    when(request.getParameter("all-tags")).thenReturn("[environment]");

    testEventServlet.doPost(request, response);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    assertEquals(1, ds.prepare(new Query("Event")).countEntities(withLimit(10)));
  }

  @Test
  public void postMultipleEventsToDatastore() throws IOException {
    HttpServletRequest request = mock(HttpServletRequest.class);       
    HttpServletResponse response = mock(HttpServletResponse.class);    

    when(request.getParameter("event-name")).thenReturn("Lake Clean Up");
    when(request.getParameter("event-description")).thenReturn("We're cleaning up the lake");
    when(request.getParameter("street-address")).thenReturn("678 Lakeview Way");
    when(request.getParameter("city")).thenReturn("Lakeside");
    when(request.getParameter("state")).thenReturn("Michigan");
    when(request.getParameter("date")).thenReturn("2020-17-05");
    when(request.getParameter("start-time")).thenReturn("14:00");
    when(request.getParameter("end-time")).thenReturn("15:00");
    when(request.getParameter("cover-photo")).thenReturn("/img-2030121");
    when(request.getParameter("all-tags")).thenReturn("['environment']");

    testEventServlet.doPost(request, response);
    testEventServlet.doPost(request, response);
    testEventServlet.doPost(request, response);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    assertEquals(3, ds.prepare(new Query("Event")).countEntities(withLimit(10)));
  }
}
@@ -1559,16 +1559,123 @@
 .Mock;%0A%0A
+import com.google.appengine.api.datastore.Cursor;%0Aimport com.google.appengine.api.datastore.FetchOptions;%0A%0A
 /** */%0A@
@@ -4537,10 +4537,1214 @@
 )));%0A  %7D
+%0A%0A  @Test%0A  public void postEventWithEmptyFields() throws IOException %7B%0A    HttpServletRequest request = mock(HttpServletRequest.class);       %0A    HttpServletResponse response = mock(HttpServletResponse.class);    %0A%0A    // This request does not include optional fields end-time and cover-photo.%0A    when(request.getParameter(%22event-name%22)).thenReturn(%22Lake Clean Up%22);%0A    when(request.getParameter(%22event-description%22)).thenReturn(%22We're cleaning up the lake%22);%0A    when(request.getParameter(%22street-address%22)).thenReturn(%22678 Lakeview Way%22);%0A    when(request.getParameter(%22city%22)).thenReturn(%22Lakeside%22);%0A    when(request.getParameter(%22state%22)).thenReturn(%22Michigan%22);%0A    when(request.getParameter(%22date%22)).thenReturn(%222020-17-05%22);%0A    when(request.getParameter(%22start-time%22)).thenReturn(%2214:00%22);%0A    when(request.getParameter(%22all-tags%22)).thenReturn(%22%5B'environment'%5D%22);%0A%0A    testEventServlet.doPost(request, response);%0A%0A    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();%0A    Entity postedEntity = ds.prepare(new Query(%22Event%22)).asSingleEntity();%0A    %0A    assertEquals(%22%22, postedEntity.getProperty(%22endTime%22));%0A    assertEquals(%22%22, postedEntity.getProperty(%22coverPhoto%22));%0A  %7D
 %0A%7D

Create test to ensure entity still includes empty property when not supplied