// 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 static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;
import com.google.sps.data.AnalysisResults;
import com.google.sps.servlets.ReceiptAnalysis;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({AnnotateImageResponse.class, BatchAnnotateImagesResponse.class,
    EntityAnnotation.class, ImageAnnotatorClient.class, ReceiptAnalysis.class, URL.class})
public final class ReceiptAnalysisTest {
  private static final ByteString IMAGE_BYTES = ByteString.copyFromUtf8("byte string");
  private static final String RAW_TEXT = "raw text";

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void serveImageTextUrlReturnsAnalysisResults() throws IOException {
    URL url = mock(URL.class);
    InputStream inputStream = new ByteArrayInputStream(IMAGE_BYTES.toByteArray());
    when(url.openStream()).thenReturn(inputStream);

    ImageAnnotatorClient client = mock(ImageAnnotatorClient.class);
    mockStatic(ImageAnnotatorClient.class);
    when(ImageAnnotatorClient.create()).thenReturn(client);

    BatchAnnotateImagesResponse batchResponse = mock(BatchAnnotateImagesResponse.class);
    when(client.batchAnnotateImages(Mockito.<AnnotateImageRequest>anyList()))
        .thenReturn(batchResponse);

    AnnotateImageResponse response = mock(AnnotateImageResponse.class);
    ImmutableList<AnnotateImageResponse> responses = ImmutableList.of(response);
    when(batchResponse.getResponsesList()).thenReturn(responses);

    EntityAnnotation annotation = mock(EntityAnnotation.class);
    ImmutableList<EntityAnnotation> annotations = ImmutableList.of(annotation);
    when(response.getTextAnnotationsList()).thenReturn(annotations);

    when(annotation.getDescription()).thenReturn(RAW_TEXT);

    AnalysisResults results = ReceiptAnalysis.serveImageText(url);

    Assert.assertEquals(results.getRawText(), RAW_TEXT);
  }
}

@@ -1060,56 +1060,8 @@
 nt;%0A
-import com.google.common.collect.ImmutableList;%0A
 impo
@@ -1647,100 +1647,8 @@
 st(%7B
-AnnotateImageResponse.class, BatchAnnotateImagesResponse.class,%0A    EntityAnnotation.class, 
 Imag
@@ -2401,208 +2401,102 @@
     
-Batch
+Entity
 Annotat
-eImagesResponse batchResponse = mock(BatchAnnotateImagesResponse.class);%0A    when(client.batchAnnotateImages(Mockito.%3CAnnotateImageRequest%3EanyList()))%0A        .thenReturn(batchResponse
+ion annotation = EntityAnnotation.newBuilder().setDescription(RAW_TEXT).build(
 );%0A
-%0A
     
@@ -2527,22 +2527,25 @@
 sponse =
- mock(
+%0A        
 Annotate
@@ -2562,34 +2562,70 @@
 nse.
-class);%0A    ImmutableList%3C
+newBuilder().addTextAnnotations(annotation).build();%0A    Batch
 Anno
@@ -2633,24 +2633,25 @@
 ateImage
+s
 Response
 %3E respon
@@ -2646,103 +2646,86 @@
 onse
-%3E r
+ batchR
 esponse
-s
  =
- ImmutableList.of(response);%0A    when(batchResponse.getResponsesList()).thenReturn
+%0A        BatchAnnotateImagesResponse.newBuilder().addResponses
 (res
@@ -2733,284 +2733,130 @@
 onse
-s
+).build(
 );%0A
-%0A
     
-EntityAnnotation annotation = mock(EntityAnnotation.class);%0A    ImmutableList%3CEntityAnnotation%3E annotations = ImmutableList.of(annotation);%0A    when(response.getTextAnnotationsList()).thenReturn(annotations);%0A%0A    when(annotation.getDescription()).thenReturn(RAW_TEXT
+when(client.batchAnnotateImages(Mockito.%3CAnnotateImageRequest%3EanyList()))%0A        .thenReturn(batchResponse
 );%0A%0A

Remove unnecessary response mocks