|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
20 | 20 |
|
| 21 | +import com.google.cloud.storage.Blob; |
| 22 | +import com.google.cloud.storage.BlobId; |
| 23 | +import com.google.cloud.storage.BlobInfo; |
| 24 | +import com.google.cloud.storage.Storage; |
| 25 | +import com.google.cloud.storage.StorageOptions; |
| 26 | + |
21 | 27 | import java.io.ByteArrayOutputStream;
|
22 | 28 | import java.io.IOException;
|
23 | 29 | import java.io.PrintStream;
|
| 30 | +import java.util.UUID; |
| 31 | + |
24 | 32 | import org.junit.After;
|
25 | 33 | import org.junit.Before;
|
26 | 34 | import org.junit.Test;
|
|
33 | 41 | public class ImportProductSetsIT {
|
34 | 42 | private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
|
35 | 43 | private static final String COMPUTE_REGION = "us-west1";
|
36 |
| - private static final String GCS_URI = |
37 |
| - "gs://cloud-samples-data/vision/product_search/product_sets.csv"; |
38 |
| - private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing"; |
39 |
| - private static final String PRODUCT_ID_1 = "fake_product_id_for_testing_1"; |
40 |
| - private static final String PRODUCT_ID_2 = "fake_product_id_for_testing_2"; |
| 44 | + private static final String PRODUCT_SET_ID = |
| 45 | + String.format("test_%s", UUID.randomUUID().toString()); |
| 46 | + private static final String PRODUCT_ID_1 = String.format("test_%s", UUID.randomUUID().toString()); |
41 | 47 | private static final String IMAGE_URI_1 = "shoes_1.jpg";
|
42 |
| - private static final String IMAGE_URI_2 = "shoes_2.jpg"; |
| 48 | + private static final String FILEPATH = |
| 49 | + String.format("vision/%s.csv", UUID.randomUUID().toString()); |
| 50 | + private static final String GCS_URI = String.format("gs://%s/%s", PROJECT_ID, FILEPATH); |
| 51 | + private Blob blob; |
43 | 52 | private ByteArrayOutputStream bout;
|
44 | 53 | private PrintStream out;
|
45 | 54 |
|
46 | 55 | @Before
|
47 | 56 | public void setUp() {
|
| 57 | + // Create the product set csv file locally and upload it to GCS |
| 58 | + // This is so that there is a unique product set ID for all python version tests. |
| 59 | + Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService(); |
| 60 | + BlobId blobId = BlobId.of(PROJECT_ID, FILEPATH); |
| 61 | + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); |
| 62 | + String csvContents = |
| 63 | + "\"gs://cloud-samples-data/vision/product_search/shoes_1.jpg\"," |
| 64 | + + String.format("\"%s\",", IMAGE_URI_1) |
| 65 | + + String.format("\"%s\",", PRODUCT_SET_ID) |
| 66 | + + String.format("\"%s\",", PRODUCT_ID_1) |
| 67 | + + "\"apparel\",,\"style=womens\",\"0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9\""; |
| 68 | + blob = storage.create(blobInfo, csvContents.getBytes()); |
| 69 | + |
48 | 70 | bout = new ByteArrayOutputStream();
|
49 | 71 | out = new PrintStream(bout);
|
50 | 72 | System.setOut(out);
|
51 | 73 | }
|
52 | 74 |
|
53 | 75 | @After
|
54 | 76 | public void tearDown() throws IOException {
|
55 |
| - ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_1); |
56 |
| - ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_2); |
| 77 | + ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1); |
57 | 78 | ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
|
| 79 | + Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService(); |
| 80 | + // Delete the created blob |
| 81 | + storage.delete(blob.getBlobId()); |
58 | 82 | System.setOut(null);
|
59 | 83 | }
|
60 | 84 |
|
61 | 85 | @Test
|
62 | 86 | public void testImportProductSets() throws Exception {
|
63 |
| - // Act |
64 |
| - ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION); |
65 |
| - |
66 |
| - // Assert |
67 |
| - String got = bout.toString(); |
68 |
| - System.out.println(got); |
69 |
| - assertThat(got).doesNotContain(PRODUCT_SET_ID); |
70 |
| - |
71 |
| - // Act |
72 |
| - ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); |
73 |
| - |
74 |
| - // Assert |
75 |
| - assertThat(got).doesNotContain(PRODUCT_ID_1); |
76 |
| - assertThat(got).doesNotContain(PRODUCT_ID_2); |
77 |
| - |
78 |
| - // Act |
79 |
| - ProductInProductSetManagement.listProductsInProductSet( |
80 |
| - PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); |
81 |
| - |
82 |
| - // Assert |
83 |
| - assertThat(got).doesNotContain(PRODUCT_ID_1); |
84 |
| - assertThat(got).doesNotContain(PRODUCT_ID_2); |
85 |
| - |
86 |
| - // Act |
87 |
| - ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1); |
88 |
| - |
89 |
| - // Assert |
90 |
| - assertThat(got).doesNotContain(IMAGE_URI_1); |
91 |
| - |
92 |
| - // Act |
93 |
| - ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_2); |
94 |
| - |
95 |
| - // Assert |
96 |
| - assertThat(got).doesNotContain(IMAGE_URI_2); |
97 |
| - |
98 | 87 | // Act
|
99 | 88 | ImportProductSets.importProductSets(PROJECT_ID, COMPUTE_REGION, GCS_URI);
|
100 | 89 | ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION);
|
101 | 90 |
|
102 | 91 | // Assert
|
103 |
| - got = bout.toString(); |
| 92 | + String got = bout.toString(); |
104 | 93 | assertThat(got).contains(PRODUCT_SET_ID);
|
105 | 94 |
|
106 | 95 | // Act
|
107 | 96 | ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);
|
108 | 97 |
|
109 | 98 | // Assert
|
110 | 99 | assertThat(got).contains(PRODUCT_ID_1);
|
111 |
| - assertThat(got).contains(PRODUCT_ID_2); |
112 | 100 |
|
113 | 101 | // Act
|
114 | 102 | ProductInProductSetManagement.listProductsInProductSet(
|
115 | 103 | PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
|
116 | 104 |
|
117 | 105 | // Assert
|
118 | 106 | assertThat(got).contains(PRODUCT_ID_1);
|
119 |
| - assertThat(got).contains(PRODUCT_ID_2); |
120 | 107 |
|
121 | 108 | // Act
|
122 | 109 | ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1);
|
123 | 110 |
|
124 | 111 | // Assert
|
125 | 112 | assertThat(got).contains(IMAGE_URI_1);
|
126 |
| - |
127 |
| - // Act |
128 |
| - ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_2); |
129 |
| - |
130 |
| - // Assert |
131 |
| - assertThat(got).contains(IMAGE_URI_2); |
132 | 113 | }
|
133 | 114 | }
|
0 commit comments