-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMocClientTest.java
More file actions
149 lines (122 loc) · 6.53 KB
/
MocClientTest.java
File metadata and controls
149 lines (122 loc) · 6.53 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package city.makeour.moc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables;
import city.makeour.ngsi.v2.api.EntitiesApi;
import city.makeour.ngsi.v2.model.CreateEntityRequest;
import city.makeour.ngsi.v2.model.ListEntitiesResponse;
import city.makeour.ngsi.v2.model.RetrieveEntityResponse;
class MocClientTest {
@Test
@DisplayName("デフォルトコンストラクタで正しいベースパスが設定されることを確認")
void defaultConstructorShouldSetCorrectBasePath() {
MocClient client = new MocClient();
assertEquals("https://orion.sandbox.makeour.city", client.client.getApiClient().getBasePath());
}
@Test
@DisplayName("カスタムベースパスが正しく設定されることを確認")
void constructorWithBasePathShouldSetCustomBasePath() {
String customBasePath = "https://custom.orion.example.com";
MocClient client = new MocClient(customBasePath);
assertEquals(customBasePath, client.client.getApiClient().getBasePath());
}
@Test
@DisplayName("entities()メソッドが正しいEntitiesApiインスタンスを返すことを確認")
void entitiesMethodShouldReturnEntitiesApiInstance() {
MocClient client = new MocClient();
EntitiesApi entitiesApi = client.entities();
assertNotNull(entitiesApi);
assertEquals(client.client.getEntitiesApi(), entitiesApi);
}
@Test
@DisplayName("entities apiで、entityの一覧を取得するテスト")
void testEntitiesApi() {
MocClient client = new MocClient();
EntitiesApi entitiesApi = client.entities();
List<ListEntitiesResponse> list = entitiesApi.listEntities(null, null, null, null, null, null, null, null, null,
null, null, null,
null,
null,
null);
assertNotNull(list);
System.out.println("Entities: " + list);
}
@Test
@DisplayName("ログインしてデータ作成できるかのテスト")
@EnabledIfEnvironmentVariables({
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*")
})
void testSetMocAuthInfo() throws GeneralSecurityException, NoSuchAlgorithmException {
String cognitoUserPoolId = System.getenv("TEST_COGNITO_USER_POOL_ID");
String cognitoClientId = System.getenv("TEST_COGNITO_CLIENT_ID");
String username = System.getenv("TEST_COGNITO_USERNAME");
String password = System.getenv("TEST_COGNITO_PASSWORD");
MocClient client = new MocClient();
client.setMocAuthInfo(cognitoUserPoolId, cognitoClientId);
client.login(username, password);
String uuid = UUID.randomUUID().toString();
CreateEntityRequest entity = new CreateEntityRequest();
entity.setType("TestEntity");
entity.setId("urn:ngsi-ld:TestEntity:" + uuid);
client.entities().createEntity("application/json", entity, "keyValues");
}
@Test
@DisplayName("トークンリフレッシュなど行えるかのテスト")
@EnabledIfEnvironmentVariables({
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*")
})
void testAuth() throws GeneralSecurityException, NoSuchAlgorithmException {
String cognitoUserPoolId = System.getenv("TEST_COGNITO_USER_POOL_ID");
String cognitoClientId = System.getenv("TEST_COGNITO_CLIENT_ID");
String username = System.getenv("TEST_COGNITO_USERNAME");
String password = System.getenv("TEST_COGNITO_PASSWORD");
MocClient client = new MocClient();
client.setMocAuthInfo(cognitoUserPoolId, cognitoClientId);
client.auth(username, password);
client.auth(username, password); // トークンリフレッシュを行う
String uuid = UUID.randomUUID().toString();
CreateEntityRequest entity = new CreateEntityRequest();
entity.setType("TestEntity");
entity.setId("urn:ngsi-ld:TestEntity:" + uuid);
client.entities().createEntity("application/json", entity, "keyValues");
}
@Test
@DisplayName("エンティティを作成・取得できるかのテスト(最小版)")
@EnabledIfEnvironmentVariables({
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"),
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*")
})
void testCreateAndGetEntity_Minimal() throws GeneralSecurityException, NoSuchAlgorithmException {
MocClient client = new MocClient();
client.setMocAuthInfo(System.getenv("TEST_COGNITO_USER_POOL_ID"), System.getenv("TEST_COGNITO_CLIENT_ID"));
client.login(System.getenv("TEST_COGNITO_USERNAME"), System.getenv("TEST_COGNITO_PASSWORD"));
// 作成&取得
String entityId = "urn:ngsi-ld:TestEntity:" + UUID.randomUUID().toString();
CreateEntityRequest entity = new CreateEntityRequest();
entity.setType("TestEntity");
entity.setId(entityId);
// 作成を実行
client.entities().createEntity("application/json", entity, "keyValues");
// getEntityの呼び出し、レスポンスの変換
RetrieveEntityResponse retrievedEntity = client
.getEntity(entityId, "TestEntity", null, null, null)
.body(RetrieveEntityResponse.class);
assertNotNull(retrievedEntity);
assertEquals(entityId, retrievedEntity.getId());
}
}