|
| 1 | +/* |
| 2 | + * Copyright 2020 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 com.example.asset; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.cloud.bigquery.BigQuery; |
| 22 | +import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; |
| 23 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 24 | +import com.google.cloud.bigquery.DatasetId; |
| 25 | +import com.google.cloud.bigquery.DatasetInfo; |
| 26 | +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.PrintStream; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.junit.runners.JUnit4; |
| 34 | + |
| 35 | +/** Tests for search samples. */ |
| 36 | +@RunWith(JUnit4.class) |
| 37 | +@SuppressWarnings("checkstyle:abbreviationaswordinname") |
| 38 | +public class Search { |
| 39 | + |
| 40 | + private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 41 | + private static final String datasetName = RemoteBigQueryHelper.generateDatasetName(); |
| 42 | + private ByteArrayOutputStream bout; |
| 43 | + private PrintStream out; |
| 44 | + private BigQuery bigquery; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() { |
| 48 | + bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 49 | + if (bigquery.getDataset(datasetName) == null) { |
| 50 | + bigquery.create(DatasetInfo.newBuilder(datasetName).build()); |
| 51 | + } |
| 52 | + bout = new ByteArrayOutputStream(); |
| 53 | + out = new PrintStream(bout); |
| 54 | + System.setOut(out); |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void tearDown() { |
| 59 | + System.setOut(null); |
| 60 | + bout.reset(); |
| 61 | + DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName); |
| 62 | + bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSearchAllResourcesExample() throws Exception { |
| 67 | + // Wait 10 seconds to let dataset creation event go to CAI |
| 68 | + Thread.sleep(10000); |
| 69 | + String scope = "projects/" + projectId; |
| 70 | + String query = "name:" + datasetName; |
| 71 | + SearchAllResourcesExample.searchAllResources(scope, query); |
| 72 | + String got = bout.toString(); |
| 73 | + assertThat(got).contains(datasetName); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSearchAllIamPoliciesExample() throws Exception { |
| 78 | + String scope = "projects/" + projectId; |
| 79 | + String query = "policy:roles/owner"; |
| 80 | + SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query); |
| 81 | + String got = bout.toString(); |
| 82 | + assertThat(got).contains("roles/owner"); |
| 83 | + } |
| 84 | +} |
0 commit comments