|
| 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 | + * http://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 aiplatform; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.api.gax.longrunning.OperationFuture; |
| 23 | +import com.google.cloud.aiplatform.v1.DeleteFeaturestoreRequest; |
| 24 | +import com.google.cloud.aiplatform.v1.DeleteOperationMetadata; |
| 25 | +import com.google.cloud.aiplatform.v1.FeaturestoreName; |
| 26 | +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; |
| 27 | +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; |
| 28 | +import com.google.protobuf.Empty; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.PrintStream; |
| 32 | +import java.util.UUID; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.concurrent.TimeoutException; |
| 36 | +import org.junit.After; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | +import org.junit.runners.JUnit4; |
| 42 | + |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +public class CreateFeaturestoreSampleTest { |
| 45 | + |
| 46 | + private static final String PROJECT_ID = System.getenv("UCAIP_PROJECT_ID"); |
| 47 | + private static final int MIN_NODE_COUNT = 1; |
| 48 | + private static final int MAX_NODE_COUNT = 5; |
| 49 | + private static final boolean USE_FORCE = true; |
| 50 | + private static final String LOCATION = "us-central1"; |
| 51 | + private static final String ENDPOINT = "us-central1-aiplatform.googleapis.com:443"; |
| 52 | + private static final int TIMEOUT = 900; |
| 53 | + private ByteArrayOutputStream bout; |
| 54 | + private PrintStream out; |
| 55 | + private PrintStream originalPrintStream; |
| 56 | + private String featurestoreId; |
| 57 | + |
| 58 | + private static void requireEnvVar(String varName) { |
| 59 | + String errorMessage = |
| 60 | + String.format("Environment variable '%s' is required to perform these tests.", varName); |
| 61 | + assertNotNull(errorMessage, System.getenv(varName)); |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void checkRequirements() { |
| 66 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 67 | + requireEnvVar("UCAIP_PROJECT_ID"); |
| 68 | + } |
| 69 | + |
| 70 | + @Before |
| 71 | + public void setUp() { |
| 72 | + bout = new ByteArrayOutputStream(); |
| 73 | + out = new PrintStream(bout); |
| 74 | + originalPrintStream = System.out; |
| 75 | + System.setOut(out); |
| 76 | + } |
| 77 | + |
| 78 | + static void deleteFeaturestoreSample( |
| 79 | + String project, |
| 80 | + String featurestoreId, |
| 81 | + boolean useForce, |
| 82 | + String location, |
| 83 | + String endpoint, |
| 84 | + int timeout) |
| 85 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 86 | + |
| 87 | + FeaturestoreServiceSettings featurestoreServiceSettings = |
| 88 | + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 89 | + |
| 90 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 91 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 92 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 93 | + try (FeaturestoreServiceClient featurestoreServiceClient = |
| 94 | + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { |
| 95 | + |
| 96 | + DeleteFeaturestoreRequest deleteFeaturestoreRequest = |
| 97 | + DeleteFeaturestoreRequest.newBuilder() |
| 98 | + .setName(FeaturestoreName.of(project, location, featurestoreId).toString()) |
| 99 | + .setForce(useForce) |
| 100 | + .build(); |
| 101 | + |
| 102 | + OperationFuture<Empty, DeleteOperationMetadata> operationFuture = |
| 103 | + featurestoreServiceClient.deleteFeaturestoreAsync(deleteFeaturestoreRequest); |
| 104 | + System.out.format("Operation name: %s%n", operationFuture.getInitialFuture().get().getName()); |
| 105 | + System.out.println("Waiting for operation to finish..."); |
| 106 | + operationFuture.get(timeout, TimeUnit.SECONDS); |
| 107 | + |
| 108 | + System.out.format("Deleted Featurestore."); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @After |
| 113 | + public void tearDown() |
| 114 | + throws InterruptedException, ExecutionException, IOException, TimeoutException { |
| 115 | + |
| 116 | + // Delete the featurestore |
| 117 | + deleteFeaturestoreSample(PROJECT_ID, featurestoreId, USE_FORCE, LOCATION, ENDPOINT, 60); |
| 118 | + |
| 119 | + // Assert |
| 120 | + String deleteFeaturestoreResponse = bout.toString(); |
| 121 | + assertThat(deleteFeaturestoreResponse).contains("Deleted Featurestore"); |
| 122 | + System.out.flush(); |
| 123 | + System.setOut(originalPrintStream); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testCreateFeaturestoreSample() |
| 128 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 129 | + // Create the featurestore |
| 130 | + String tempUuid = UUID.randomUUID().toString().replaceAll("-", "_").substring(0, 26); |
| 131 | + String id = String.format("temp_create_featurestore_test_%s", tempUuid); |
| 132 | + CreateFeaturestoreSample.createFeaturestoreSample( |
| 133 | + PROJECT_ID, id, MIN_NODE_COUNT, MAX_NODE_COUNT, LOCATION, ENDPOINT, TIMEOUT); |
| 134 | + |
| 135 | + // Assert |
| 136 | + String createFeaturestoreResponse = bout.toString(); |
| 137 | + assertThat(createFeaturestoreResponse).contains("Create Featurestore Response"); |
| 138 | + featurestoreId = |
| 139 | + createFeaturestoreResponse.split("Name: ")[1].split("featurestores/")[1].split("\n")[0] |
| 140 | + .trim(); |
| 141 | + } |
| 142 | +} |
0 commit comments