1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.videointelligence.v1beta2; 18 19 import com.google.api.gax.core.NoCredentialsProvider; 20 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 21 import com.google.api.gax.httpjson.testing.MockHttpService; 22 import com.google.api.gax.rpc.ApiClientHeaderProvider; 23 import com.google.api.gax.rpc.ApiException; 24 import com.google.api.gax.rpc.ApiExceptionFactory; 25 import com.google.api.gax.rpc.StatusCode; 26 import com.google.api.gax.rpc.testing.FakeStatusCode; 27 import com.google.cloud.videointelligence.v1beta2.stub.HttpJsonVideoIntelligenceServiceStub; 28 import com.google.longrunning.Operation; 29 import com.google.protobuf.Any; 30 import java.io.IOException; 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.concurrent.ExecutionException; 34 import javax.annotation.Generated; 35 import org.junit.After; 36 import org.junit.AfterClass; 37 import org.junit.Assert; 38 import org.junit.Before; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 42 @Generated("by gapic-generator-java") 43 public class VideoIntelligenceServiceClientHttpJsonTest { 44 private static MockHttpService mockService; 45 private static VideoIntelligenceServiceClient client; 46 47 @BeforeClass startStaticServer()48 public static void startStaticServer() throws IOException { 49 mockService = 50 new MockHttpService( 51 HttpJsonVideoIntelligenceServiceStub.getMethodDescriptors(), 52 VideoIntelligenceServiceSettings.getDefaultEndpoint()); 53 VideoIntelligenceServiceSettings settings = 54 VideoIntelligenceServiceSettings.newHttpJsonBuilder() 55 .setTransportChannelProvider( 56 VideoIntelligenceServiceSettings.defaultHttpJsonTransportProviderBuilder() 57 .setHttpTransport(mockService) 58 .build()) 59 .setCredentialsProvider(NoCredentialsProvider.create()) 60 .build(); 61 client = VideoIntelligenceServiceClient.create(settings); 62 } 63 64 @AfterClass stopServer()65 public static void stopServer() { 66 client.close(); 67 } 68 69 @Before setUp()70 public void setUp() {} 71 72 @After tearDown()73 public void tearDown() throws Exception { 74 mockService.reset(); 75 } 76 77 @Test annotateVideoTest()78 public void annotateVideoTest() throws Exception { 79 AnnotateVideoResponse expectedResponse = 80 AnnotateVideoResponse.newBuilder() 81 .addAllAnnotationResults(new ArrayList<VideoAnnotationResults>()) 82 .build(); 83 Operation resultOperation = 84 Operation.newBuilder() 85 .setName("annotateVideoTest") 86 .setDone(true) 87 .setResponse(Any.pack(expectedResponse)) 88 .build(); 89 mockService.addResponse(resultOperation); 90 91 String inputUri = "inputUri470706498"; 92 List<Feature> features = new ArrayList<>(); 93 94 AnnotateVideoResponse actualResponse = client.annotateVideoAsync(inputUri, features).get(); 95 Assert.assertEquals(expectedResponse, actualResponse); 96 97 List<String> actualRequests = mockService.getRequestPaths(); 98 Assert.assertEquals(1, actualRequests.size()); 99 100 String apiClientHeaderKey = 101 mockService 102 .getRequestHeaders() 103 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 104 .iterator() 105 .next(); 106 Assert.assertTrue( 107 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 108 .matcher(apiClientHeaderKey) 109 .matches()); 110 } 111 112 @Test annotateVideoExceptionTest()113 public void annotateVideoExceptionTest() throws Exception { 114 ApiException exception = 115 ApiExceptionFactory.createException( 116 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 117 mockService.addException(exception); 118 119 try { 120 String inputUri = "inputUri470706498"; 121 List<Feature> features = new ArrayList<>(); 122 client.annotateVideoAsync(inputUri, features).get(); 123 Assert.fail("No exception raised"); 124 } catch (ExecutionException e) { 125 } 126 } 127 } 128