// 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.Feature;
import com.google.cloud.vision.v1.Image;
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.Answers;
import org.mockito.Mock;
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, AnnotateImageRequest.class,
    BatchAnnotateImagesResponse.class, EntityAnnotation.class, Feature.class, Image.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";

  @Mock private AnnotateImageRequest request;
  @Mock private Feature feature;
  @Mock private Image image;

  @Mock(answer = Answers.RETURNS_SELF) private AnnotateImageRequest.Builder requestBuilder;
  @Mock(answer = Answers.RETURNS_SELF) private Feature.Builder featureBuilder;
  @Mock(answer = Answers.RETURNS_SELF) private Image.Builder imageBuilder;

  @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);

    when(imageBuilder.build()).thenReturn(image);
    mockStatic(Image.class);
    when(Image.newBuilder()).thenReturn(imageBuilder);

    when(featureBuilder.build()).thenReturn(feature);
    mockStatic(Feature.class);
    when(Feature.newBuilder()).thenReturn(featureBuilder);

    when(requestBuilder.build()).thenReturn(request);
    mockStatic(AnnotateImageRequest.class);
    when(AnnotateImageRequest.newBuilder()).thenReturn(requestBuilder);

    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);
  }
}

@@ -1004,92 +1004,8 @@
 on;%0A
-import com.google.cloud.vision.v1.Feature;%0Aimport com.google.cloud.vision.v1.Image;%0A
 impo
@@ -1459,61 +1459,8 @@
 th;%0A
-import org.mockito.Answers;%0Aimport org.mockito.Mock;%0A
 impo
@@ -1723,40 +1723,8 @@
 ass,
- AnnotateImageRequest.class,%0A   
  Bat
@@ -1754,16 +1754,20 @@
 e.class,
+%0A   
  EntityA
@@ -1786,40 +1786,8 @@
 ass,
- Feature.class, Image.class,%0A   
  Ima
@@ -2036,1139 +2036,332 @@
 %0A  @
-Mock private AnnotateImageRequest request;%0A  @Mock private Feature feature;%0A  @Mock private Image image;%0A%0A  @Mock(answer = Answers.RETURNS_SELF) private AnnotateImageRequest.Builder requestBuilder;%0A  @Mock(answer = Answers.RETURNS_SELF) private Feature.Builder featureBuilder;%0A  @Mock(answer = Answers.RETURNS_SELF) private Image.Builder imageBuilder;%0A%0A  @Before%0A  public void setUp() %7B%0A    MockitoAnnotations.initMocks(this);%0A  %7D%0A%0A  @Test%0A  public void serveImageTextUrlReturnsAnalysisResults() throws IOException %7B%0A    URL url = mock(URL.class);%0A    InputStream inputStream = new ByteArrayInputStream(IMAGE_BYTES.toByteArray());%0A    when(url.openStream()).thenReturn(inputStream);%0A%0A    when(imageBuilder.build()).thenReturn(image);%0A    mockStatic(Image.class);%0A    when(Image.newBuilder()).thenReturn(imageBuilder);%0A%0A    when(featureBuilder.build()).thenReturn(feature);%0A    mockStatic(Feature.class);%0A    when(Feature.newBuilder()).thenReturn(featureBuilder);%0A%0A    when(requestBuilder.build()).thenReturn(request);%0A    mockStatic(AnnotateImageRequest.class);%0A    when(AnnotateImageRequest.newBuilder()).thenReturn(requestBuilder
+Before%0A  public void setUp() %7B%0A    MockitoAnnotations.initMocks(this);%0A  %7D%0A%0A  @Test%0A  public void serveImageTextUrlReturnsAnalysisResults() throws IOException %7B%0A    URL url = mock(URL.class);%0A    InputStream inputStream = new ByteArrayInputStream(IMAGE_BYTES.toByteArray());%0A    when(url.openStream()).thenReturn(inputStream
 );%0A%0A

Remove unnecessary request mocks