-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.java
208 lines (186 loc) · 5.05 KB
/
Application.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package software.xdev;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.xdev.sched.api.RoleApi;
import software.xdev.sched.api.SessionApi;
import software.xdev.sched.api.UserApi;
import software.xdev.sched.client.ApiClient;
import software.xdev.sched.model.ExportSession;
import software.xdev.sched.model.Session;
import software.xdev.sched.model.User;
import software.xdev.sched.model.UserAdded;
public final class Application
{
public static final Logger LOG = LoggerFactory.getLogger(Application.class);
private static final String LOG_RESPONSE = "Response: {}";
private static String testUser;
public static final String SESSION_KEY = "TEST-123" + System.currentTimeMillis() + "_DELETE_ME";
public static void main(final String[] args)
{
String apiKey = Optional.ofNullable(System.getenv("API-KEY"))
.orElseGet(() -> System.getProperty("API-KEY"));
if(apiKey == null)
{
LOG.error("Required API-KEY not set in environment variables or system properties");
LOG.info("Please provide API Key over console:");
try(final Scanner scanner = new Scanner(System.in))
{
apiKey = scanner.nextLine();
}
if(apiKey == null || apiKey.isBlank())
{
LOG.error("Invalid key; Aborting");
System.exit(1);
}
}
try
{
final CustomApiClient apiClient = new CustomApiClient(apiKey);
checkUserAPI(apiClient);
checkSessionAPI(apiClient);
checkRoleAPI(apiClient);
}
catch(final Exception ex)
{
LOG.error("Unexpected problem", ex);
}
finally
{
LOG.warn("Manual cleanup may be required for user and session (as APIs for complete deletion do not "
+ "exist)");
}
}
private static void checkUserAPI(final CustomApiClient apiClient)
{
final UserApi userApi = new UserApi(apiClient);
LOG.info("=== USER: ADD ===");
final UserAdded userAdded = userApi.addUser(
"TestUser" + System.currentTimeMillis() + "_DELETE_ME",
// Temporary E-Mail for test may be acquired by e.g. https://temp-mail.org
null,
"+49 123 456",
null,
"attendee",
null,
"The TestUser",
"Does some integration testing with the API",
URI.create("https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png"),
"The internet",
"IANA",
"Tester",
null,
"0",
null,
"Y"
);
testUser = userAdded.getUsername();
LOG.info("Added user: {}", userAdded);
userApi.modifyUser(
userAdded.getId(),
userAdded.getUsername(),
null,
null,
null,
null,
null,
null,
null,
"Does some integration testing with the API - Also the user/mod endpoint works",
null,
null,
null,
null,
null,
null,
null
);
LOG.info("Modified user");
LOG.info("=== USER: LIST ===");
final List<User> users = userApi.listUsers("json", null);
LOG.info("Users: {}", users);
}
private static void checkSessionAPI(final CustomApiClient apiClient)
{
final SessionApi sessionApi = new SessionApi(apiClient);
LOG.info("=== SESSION: ADD ===");
final String addSessionResponse = sessionApi.addSession(
SESSION_KEY,
"TestSession",
"2023-01-01 14:00",
"2023-01-01 16:00",
"session",
null,
"Test session",
URI.create("https://sched.com/api"),
"First room",
null,
null,
null,
null,
null,
"N",
null,
Map.of("extra", "Joppiesauce")
);
LOG.info(LOG_RESPONSE, addSessionResponse);
LOG.info("=== SESSION: MODIFY ===");
final String modifySessionResponse = sessionApi.modifySession(
SESSION_KEY,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
"Y",
null
);
LOG.info(LOG_RESPONSE, modifySessionResponse);
LOG.info("=== SESSION: LIST ===");
final List<Session> sessions = sessionApi.listSessions(null, "json", null, null);
LOG.info("Sessions: {}", sessions);
LOG.info("=== SESSION: EXPORT ===");
final List<ExportSession> exportSessions =
sessionApi.exportSessions(null, "json", null, null, null, null, null);
LOG.info("Sessions: {}", exportSessions);
LOG.info("=== SESSION: \"DELETE\"/DEACTIVATE ===");
final String deleteResponse = sessionApi.deleteSession(SESSION_KEY);
LOG.info(LOG_RESPONSE, deleteResponse);
}
private static void checkRoleAPI(final CustomApiClient apiClient)
{
final RoleApi roleApi = new RoleApi(apiClient);
LOG.info("=== ROLE: ADD ===");
final String roleAddResponse = roleApi.addRole(testUser, "speaker", null, null);
LOG.info(LOG_RESPONSE, roleAddResponse);
LOG.info("=== ROLE: \"DELETE\"/REMOVE ===");
final String roleDeleteResponse = roleApi.deleteRole(testUser, "speaker", null);
LOG.info(LOG_RESPONSE, roleDeleteResponse);
}
public static class CustomApiClient extends ApiClient
{
@SuppressWarnings("checkstyle:MagicNumber") // timeout
public CustomApiClient(final String apiKey)
{
this.setApiKey(apiKey);
this.setConnectTimeout(30_000);
this.setUserAgent("Sched-Java-Client-Demo");
}
}
private Application()
{
}
}