package com.google.sps.servlets;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.tools.development.testing.LocalBlobstoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

@RunWith(JUnit4.class)
public final class BlobServletTest {

    private static final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(
                    new LocalDatastoreServiceTestConfig(),
                    new LocalBlobstoreServiceTestConfig());
    private static final String KEY_STRING = "keyString";

    @Before
    public void setUp() {
        helper.setUp();
    }

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

    /*@Test
    public void doGetBlob() throws IOException {
        BlobKey mockKey = mock(BlobKey.class);
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getParameter("blobAction")).thenReturn(BlobAction.valueOf("KEY_TO_BLOB").toString());
        when(request.getParameter("blob-key")).thenReturn(mockKey);
        HttpServletResponse response = mock(HttpServletResponse.class);
        BlobstoreService mockBlobService = mock(BlobstoreService.class);

        when(mockBlobService.serve())


        BlobServlet spiedServlet = spy(BlobServlet.class);
        when(spiedServlet.getBlobService()).thenReturn(mockBlobService);
    }*/


    @Test
    // Post a request containing blobs
    public void doPostWithBlob() throws IOException {

        final HttpServletRequest request = mock(HttpServletRequest.class);
        final HttpServletResponse response = mock(HttpServletResponse.class);

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


        BlobKey blobKey = new BlobKey(KEY_STRING);
        Map<String, List<BlobKey>> fakeKeys = new HashMap<>();
        fakeKeys.put("image", Arrays.asList(blobKey));

        BlobstoreService spiedBlobService = mock(BlobstoreService.class);

        when(spiedBlobService.getUploads(Mockito.any(HttpServletRequest.class))).thenReturn(fakeKeys);

        new BlobServlet().doPost(request, response, spiedBlobService);

        assertTrue(stringWriter.toString().contains(KEY_STRING));
    }
}
@@ -1263,16 +1263,89 @@
 String%22;
+%0A    private static final String EMPTY_RESPONSE = %22%7B%5C%22blobKey%5C%22:%5C%22%5C%22%7D%5Cn%22;
 %0A%0A    @B
@@ -2687,17 +2687,19 @@
  fakeKey
-s
+Map
  = new H
@@ -2725,17 +2725,19 @@
  fakeKey
-s
+Map
 .put(%22im
@@ -2816,139 +2816,1927 @@
 e = 
-mock(BlobstoreService.class);%0A%0A        when(spiedBlobService.getUploads(Mockito.any(HttpServletRequest.class))).thenReturn(fakeKeys
+spy(BlobstoreService.class);%0A%0A        when(spiedBlobService.getUploads(Mockito.any(HttpServletRequest.class))).thenReturn(fakeKeyMap);%0A%0A        new BlobServlet().doPost(request, response, spiedBlobService);%0A%0A        assertTrue(stringWriter.toString().contains(KEY_STRING));%0A    %7D%0A%0A    @Test%0A    // Post a request containing a blob that wasn't defined as 'image'%0A    public void doPostNullBlob() throws IOException %7B%0A%0A        final HttpServletRequest request = mock(HttpServletRequest.class);%0A        final HttpServletResponse response = mock(HttpServletResponse.class);%0A%0A        StringWriter stringWriter = new StringWriter();%0A        PrintWriter writer = new PrintWriter(stringWriter);%0A        when(response.getWriter()).thenReturn(writer);%0A%0A        BlobstoreService spiedBlobService = spy(BlobstoreService.class);%0A        Map%3CString, List%3CBlobKey%3E%3E fakeKeyMap = new HashMap%3C%3E();%0A        BlobKey blobKey = new BlobKey(KEY_STRING);%0A        fakeKeyMap.put(%22file%22, Arrays.asList(blobKey));%0A        when(spiedBlobService.getUploads(Mockito.any(HttpServletRequest.class))).thenReturn(fakeKeyMap);%0A%0A        new BlobServlet().doPost(request, response, spiedBlobService);%0A%0A        assertTrue(stringWriter.toString().equals(EMPTY_RESPONSE));%0A    %7D%0A%0A    @Test%0A    // Post a request not containing any blobs%0A    public void doPostNoBlob() throws IOException %7B%0A%0A        final HttpServletRequest request = mock(HttpServletRequest.class);%0A        final HttpServletResponse response = mock(HttpServletResponse.class);%0A%0A        StringWriter stringWriter = new StringWriter();%0A        PrintWriter writer = new PrintWriter(stringWriter);%0A        when(response.getWriter()).thenReturn(writer);%0A%0A        BlobstoreService spiedBlobService = spy(BlobstoreService.class);%0A        Map%3CString, List%3CBlobKey%3E%3E emptyKeyMap = new HashMap%3C%3E();%0A        when(spiedBlobService.getUploads(Mockito.any(HttpServletRequest.class))).thenReturn(emptyKeyMap
 );%0A%0A
@@ -4850,35 +4850,37 @@
 tring().
-contains(KEY_STRING
+equals(EMPTY_RESPONSE
 ));%0A    

Add test cases for requests without blobs / requests without images