-
Notifications
You must be signed in to change notification settings - Fork 215
/
WhoamiResponseTest.java
107 lines (90 loc) · 4.43 KB
/
WhoamiResponseTest.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
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.routes.whoami;
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import org.eclipse.ditto.base.api.common.CommonCommandResponse;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.junit.Test;
import nl.jqno.equalsverifier.EqualsVerifier;
/**
* Unit test for {@link WhoamiResponse}.
*/
public final class WhoamiResponseTest {
private static final String DEFAULT_SUBJECT = "eclipse:subject";
private static final String OTHER_SUBJECT = "other:sub";
private static final AuthorizationContext AUTHORIZATION_CONTEXT =
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance(DEFAULT_SUBJECT),
AuthorizationSubject.newInstance(OTHER_SUBJECT));
private static final UserInformation USER_INFORMATION =
DefaultUserInformation.fromAuthorizationContext(AUTHORIZATION_CONTEXT);
private static final DittoHeaders DITTO_HEADERS = DittoHeaders.newBuilder()
.correlationId("any")
.responseRequired(false)
.build();
private static final JsonObject KNOWN_JSON = JsonFactory.newObjectBuilder()
.set(CommonCommandResponse.JsonFields.TYPE, WhoamiResponse.TYPE)
.set(CommonCommandResponse.JsonFields.STATUS, HttpStatus.OK.getCode())
.set("userInformation", USER_INFORMATION.toJson())
.build();
@Test
public void of() {
final WhoamiResponse response = WhoamiResponse.of(USER_INFORMATION, DITTO_HEADERS);
assertThat(response.getDittoHeaders()).isEqualTo(DITTO_HEADERS);
assertThat(response.getEntity()).isEqualTo(USER_INFORMATION.toJson());
}
@Test
public void toJsonReturnsExpected() {
final WhoamiResponse response = WhoamiResponse.of(USER_INFORMATION, DITTO_HEADERS);
final JsonObject actualJson = response.toJson();
assertThat(actualJson).isEqualTo(KNOWN_JSON);
}
@Test
public void createInstanceFromValidJson() {
final WhoamiResponse response = WhoamiResponse.fromJson(KNOWN_JSON, DITTO_HEADERS);
assertThat(response.getDittoHeaders()).isEqualTo(DITTO_HEADERS);
assertThat(response.getEntity()).isEqualTo(USER_INFORMATION.toJson());
}
@Test
public void setEntity() {
final UserInformation otherUserInformation = DefaultUserInformation.fromAuthorizationContext(
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance(OTHER_SUBJECT)));
final JsonValue expectedEntity = otherUserInformation.toJson();
final WhoamiResponse originalResponse = WhoamiResponse.of(USER_INFORMATION, DITTO_HEADERS);
final WhoamiResponse responseWithNewEntity = originalResponse.setEntity(expectedEntity);
final JsonValue newEntity = responseWithNewEntity.getEntity(JsonSchemaVersion.LATEST);
assertThat(newEntity).isEqualTo(expectedEntity);
}
@Test
public void testEqualsAndHashcode() {
EqualsVerifier.forClass(WhoamiResponse.class)
.withRedefinedSuperclass()
.verify();
}
@Test
public void testImmutability() {
assertInstancesOf(WhoamiResponse.class, areImmutable(), provided(JsonObject.class).isAlsoImmutable());
}
}