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

import static org.mockito.Mockito.when;

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.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
import com.google.api.client.util.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Message;
import com.google.sps.data.Email;
import com.google.sps.data.Post;
import com.google.sps.servlets.PostDataServlet;
import com.google.sps.api.GmailConfiguration;
import com.google.sps.api.GmailAPI;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.internet.MimeMessage;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/** Tests querying users and sending emails from an authorized account.*/
@RunWith(MockitoJUnitRunner.class)
public final class GmailConfigurationTest {

  private static final String FROM = "me";
  private static final String TO = "test@google.com";
  private static final String SUBJECT = "test";
  private static final String BODYTEXT = "<h1>test</h1>";

  private static GmailConfiguration mGmailConfiguration;
  private static Post mPost;
  private static Gmail mGmail;

  private static LocalServiceTestHelper helper =
    new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  private static DatastoreService datastore;

  @Before
  public void setUp() throws Exception {

    helper.setUp();

    datastore = DatastoreServiceFactory.getDatastoreService();
    mGmailConfiguration= mock(GmailConfiguration.class);
    mPost = mock(Post.class);
    mGmail = mock(Gmail.class);
  }

  @Override
  @After
  public void tearDown() {

    helper.tearDown();
  }

  @Test
  public void noUsersAttendingCollege() {

  }

  @Test
  public void usersAttendingCollege() {
  }
}

@@ -2425,26 +2425,14 @@
 ith(
-MockitoJUnitRunner
+JUnit4
 .cla
@@ -2690,64 +2690,13 @@
 %0A%0A  
-private static GmailConfiguration mGmailConfiguration;%0A 
+@Mock
  pri
@@ -2717,24 +2717,30 @@
 st mPost;%0A  
+@Mock 
 private stat
@@ -2921,16 +2921,72 @@
 tastore;
+%0A  private static GmailConfiguration gmailConfiguration;
 %0A%0A  @Bef
@@ -3051,17 +3051,16 @@
 etUp();%0A
-%0A
     data
@@ -3114,26 +3114,25 @@
 vice();%0A    
-mG
+g
 mailConfigur
@@ -3136,23 +3136,23 @@
 guration
+ 
 = 
-mock(
+new 
 GmailCon
@@ -3165,95 +3165,56 @@
 tion
-.class
+(
 );%0A    
-mPost = mock(Post.class);%0A    mGmail = mock(Gmail.class);%0A  %7D%0A%0A  @Override
+MockitoAnnotations.initMocks(this);%0A  %7D%0A
 %0A  @
@@ -3301,15 +3301,12 @@
 oid 
-noU
+u
 ser
-s
 Atte
@@ -3350,21 +3350,22 @@
 ic void 
-u
+noU
 ser
-s
 Attendin
@@ -3377,14 +3377,152 @@
 ege() %7B%0A
+%0A
   %7D
+%0A%0A  @Test%0A  public void sendEmailWithAuthenticatedService() %7B%0A%0A  %7D%0A%0A  @Test%0A  public void sendEmailWithNotAuthenticatedService() %7B%0A%0A  %7D%0A%0A
 %0A%7D%0A

set up four tests for gmail config