-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
537 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ditto/gateway/service/endpoints/routes/checkpermissions/CheckPermissionsResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2024 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.checkpermissions; | ||
|
||
|
||
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Map; | ||
|
||
import org.eclipse.ditto.base.model.headers.DittoHeaders; | ||
import org.eclipse.ditto.json.JsonFactory; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.base.model.common.HttpStatus; | ||
import org.junit.Test; | ||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
|
||
public final class CheckPermissionsResponseTest { | ||
|
||
private static final DittoHeaders DITTO_HEADERS = DittoHeaders.newBuilder() | ||
.correlationId("test-correlation-id") | ||
.responseRequired(false) | ||
.build(); | ||
|
||
private static final Map<String, Boolean> PERMISSION_RESULTS = Map.of("check1", true, "check2", false); | ||
private static final JsonObject RESULTS_JSON = JsonObject.newBuilder().set("check1", true).set("check2", false).build(); | ||
|
||
private static final JsonObject KNOWN_JSON = JsonFactory.newObjectBuilder() | ||
.set("type", CheckPermissionsResponse.TYPE) | ||
.set("status", HttpStatus.OK.getCode()) | ||
.set("permissionResults", RESULTS_JSON) | ||
.build(); | ||
private static CheckPermissionsResponse KNOWN_RESPONSE = CheckPermissionsResponse.of(PERMISSION_RESULTS, DITTO_HEADERS); | ||
@Test | ||
public void of() { | ||
final CheckPermissionsResponse response = CheckPermissionsResponse.of(PERMISSION_RESULTS, DITTO_HEADERS); | ||
assertEquals(DITTO_HEADERS, response.getDittoHeaders()); | ||
assertThat(response.getEntity()).isEqualTo(RESULTS_JSON); | ||
} | ||
|
||
@Test | ||
public void toJsonReturnsExpected() { | ||
final CheckPermissionsResponse response = CheckPermissionsResponse.of(PERMISSION_RESULTS, DITTO_HEADERS); | ||
final JsonObject actualJson = response.toJson(); | ||
assertThat(actualJson).isEqualTo(KNOWN_JSON); | ||
} | ||
|
||
@Test | ||
public void createInstanceFromValidJson() { | ||
final CheckPermissionsResponse response = CheckPermissionsResponse.fromJson(KNOWN_RESPONSE.toJson(), DITTO_HEADERS); | ||
assertEquals(DITTO_HEADERS, response.getDittoHeaders()); | ||
assertThat(response.getEntity()).isEqualTo(KNOWN_RESPONSE.getEntity().asObject()); | ||
} | ||
|
||
@Test | ||
public void testEqualsAndHashcode() { | ||
EqualsVerifier.forClass(CheckPermissionsResponse.class) | ||
.withRedefinedSuperclass() | ||
.verify(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...se/ditto/gateway/service/endpoints/routes/checkpermissions/CheckPermissionsRouteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2024 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.checkpermissions; | ||
|
||
import org.apache.pekko.http.javadsl.model.ContentTypes; | ||
import org.apache.pekko.http.javadsl.model.HttpEntities; | ||
import org.apache.pekko.http.javadsl.model.HttpRequest; | ||
import org.apache.pekko.http.javadsl.model.StatusCodes; | ||
import org.apache.pekko.http.javadsl.server.Route; | ||
import org.apache.pekko.http.javadsl.testkit.TestRoute; | ||
import org.eclipse.ditto.gateway.service.endpoints.EndpointTestBase; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.policies.model.ResourceKey; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat; | ||
|
||
public final class CheckPermissionsRouteTest extends EndpointTestBase { | ||
|
||
private CheckPermissionsRoute checkPermissionsRoute; | ||
private TestRoute routeUnderTest; | ||
|
||
@Before | ||
public void setUp() { | ||
checkPermissionsRoute = new CheckPermissionsRoute(routeBaseProperties); | ||
final Route route = extractRequestContext(ctx -> checkPermissionsRoute.buildCheckPermissionsRoute(ctx, dittoHeaders)); | ||
routeUnderTest = testRoute(handleExceptions(() -> route)); | ||
} | ||
|
||
@Test | ||
public void postCheckPermissionsForwardCommand() { | ||
final var permissionResource = | ||
ImmutablePermissionCheck.of(ResourceKey.newInstance("policy:/"), | ||
"org.eclipse.ditto:some-policy-1", | ||
List.of("WRITE")); | ||
final var jsonPayload = JsonObject.newBuilder().set("check1", permissionResource.toJson()).build(); | ||
final var request = HttpRequest.POST("/checkPermissions") | ||
.withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, jsonPayload.toString())); | ||
|
||
final var result = routeUnderTest.run(request); | ||
result.assertStatusCode(StatusCodes.OK); | ||
|
||
assertThat(JsonObject.of(result.entityString())) | ||
.isEqualTo(JsonObject.newBuilder().set("check1", false).build()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...eclipse/ditto/gateway/service/endpoints/routes/checkpermissions/CheckPermissionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2024 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.checkpermissions; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.eclipse.ditto.base.model.headers.DittoHeaders; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.policies.model.ResourceKey; | ||
import org.junit.Test; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Unit tests for {@link CheckPermissions}. | ||
*/ | ||
public final class CheckPermissionsTest { | ||
|
||
@Test | ||
public void fromJsonToJson() { | ||
final DittoHeaders headers = DittoHeaders.newBuilder() | ||
.correlationId("test-correlation-id") | ||
.build(); | ||
var permissionResource = | ||
ImmutablePermissionCheck.of(ResourceKey.newInstance("thing:/features/lamp/properties/on"), | ||
"org.eclipse.ditto:some-thing-1", | ||
List.of("WRITE")); | ||
Map<String, ImmutablePermissionCheck> permissionChecks = new LinkedHashMap<>(); | ||
permissionChecks.put("check1", permissionResource); | ||
final CheckPermissions command = CheckPermissions.of(permissionChecks, headers); | ||
|
||
final CheckPermissions deserialized = | ||
CheckPermissions.fromJson(JsonObject.newBuilder().set("check1", permissionResource.toJson()).build(), | ||
headers); | ||
|
||
assertThat(deserialized).isEqualTo(command); | ||
} | ||
|
||
@Test | ||
public void testEqualsAndHashcode() { | ||
EqualsVerifier.forClass(CheckPermissions.class) | ||
.usingGetClass() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void testEmptyPermissionChecks() { | ||
final DittoHeaders headers = DittoHeaders.newBuilder().build(); | ||
final Map<String, ImmutablePermissionCheck> emptyPermissionChecks = new LinkedHashMap<>(); | ||
|
||
final CheckPermissions command = CheckPermissions.of(emptyPermissionChecks, headers); | ||
|
||
assertThat(command.getPermissionChecks()).isEmpty(); | ||
assertThat(command.getDittoHeaders()).isEqualTo(headers); | ||
} | ||
|
||
} |
Oops, something went wrong.