This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
/
ITSystemTest.java
96 lines (84 loc) · 3.46 KB
/
ITSystemTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright 2020 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
*
* http://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.cloud.redis.v1.it;
import static org.junit.Assert.assertEquals;
import com.google.cloud.ServiceOptions;
import com.google.cloud.redis.v1.CloudRedisClient;
import com.google.cloud.redis.v1.Instance;
import com.google.cloud.redis.v1.InstanceName;
import com.google.cloud.redis.v1.LocationName;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ITSystemTest {
private static CloudRedisClient client;
private static String projectId;
private static final Logger log = Logger.getLogger(ITSystemTest.class.getName());
private static final Instance.Tier TIER = Instance.Tier.BASIC;
private static final String INSTANCE_NAME_PREFIX = "test-instance";
private static final String INSTANCE =
INSTANCE_NAME_PREFIX + "-" + UUID.randomUUID().toString().substring(0, 8);
private static final String LOCATION = "us-central1";
private static final int MEMORY_SIZE_GB = 1;
private static final String AUTHORIZED_NETWORK = System.getProperty("redis.network", "default");
@BeforeClass
public static void setUp() throws Exception {
client = CloudRedisClient.create();
projectId = ServiceOptions.getDefaultProjectId();
/** Creates a Redis instance based on the specified tier and memory size. */
LocationName parent = LocationName.of(projectId, LOCATION);
String authorizedNetwork = "projects/" + projectId + "/global/networks/" + AUTHORIZED_NETWORK;
Instance instance =
Instance.newBuilder()
.setTier(TIER)
.setMemorySizeGb(MEMORY_SIZE_GB)
.setAuthorizedNetwork(authorizedNetwork)
.build();
client.createInstanceAsync(parent, INSTANCE, instance).get();
log.info("redis instance created successfully.");
}
@AfterClass
public static void tearDown() {
/** Deletes a specific Redis instance. Instance stops serving and data is deleted. */
InstanceName name = InstanceName.of(projectId, LOCATION, INSTANCE);
client.deleteInstanceAsync(name);
log.info("redis instance deleted successfully.");
client.close();
}
@Test
public void testGetInstance() {
InstanceName name = InstanceName.of(projectId, LOCATION, INSTANCE);
Instance response = client.getInstance(name);
assertEquals(TIER, response.getTier());
assertEquals(MEMORY_SIZE_GB, response.getMemorySizeGb());
}
@Test
public void testListInstances() {
LocationName parent = LocationName.of(projectId, LOCATION);
CloudRedisClient.ListInstancesPagedResponse pagedListResponse = client.listInstances(parent);
List<Instance> resources = Lists.newArrayList(pagedListResponse.iterateAll());
int instance = 0, count = 0;
while (instance < resources.size()) {
count++;
instance++;
}
assertEquals(count, resources.size());
}
}