Skip to content

Commit

Permalink
feat(impl): [#528] add and use PolicyStoreTestUtil
Browse files Browse the repository at this point in the history
in order to deduplicate common test code
  • Loading branch information
dsmf committed May 7, 2024
1 parent 1240f77 commit d3e1dcf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.StringReader;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.servlet.http.HttpServletRequest;
import org.eclipse.tractusx.irs.edc.client.policy.Constraint;
import org.eclipse.tractusx.irs.edc.client.policy.Constraints;
Expand All @@ -51,6 +48,7 @@
import org.eclipse.tractusx.irs.policystore.models.PolicyResponse;
import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.services.PolicyStoreService;
import org.eclipse.tractusx.irs.policystore.testutil.PolicyStoreTestUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -112,10 +110,7 @@ class RegisterPolicyTests {
void registerAllowedPolicy() {
// arrange
final OffsetDateTime now = OffsetDateTime.now();
final JsonObject jsonObject;
try (JsonReader jsonReader = Json.createReader(new StringReader(REGISTER_POLICY_EXAMPLE_PAYLOAD))) {
jsonObject = jsonReader.readObject();
}
final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject(REGISTER_POLICY_EXAMPLE_PAYLOAD);

// act
final CreatePolicyRequest request = new CreatePolicyRequest(now.plusMinutes(1), null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.StringReader;
import java.time.Clock;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.ThrowableAssert;
import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl;
Expand All @@ -64,6 +61,7 @@
import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.persistence.PolicyPersistence;
import org.eclipse.tractusx.irs.policystore.testutil.PolicyStoreTestUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -115,10 +113,7 @@ void registerPolicy_withBpnNull_shouldStoreAsDefault() {

// ARRANGE
final OffsetDateTime now = OffsetDateTime.now();
final JsonObject jsonObject;
try (JsonReader jsonReader = Json.createReader(new StringReader(REGISTER_POLICY_EXAMPLE_PAYLOAD))) {
jsonObject = jsonReader.readObject();
}
final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject(REGISTER_POLICY_EXAMPLE_PAYLOAD);

// ACT
testee.registerPolicy(new CreatePolicyRequest(now, null, jsonObject));
Expand All @@ -135,10 +130,7 @@ void registerPolicy_success() {

// ARRANGE
final OffsetDateTime now = OffsetDateTime.now();
final JsonObject jsonObject;
try (JsonReader jsonReader = Json.createReader(new StringReader(REGISTER_POLICY_EXAMPLE_PAYLOAD))) {
jsonObject = jsonReader.readObject();
}
final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject(REGISTER_POLICY_EXAMPLE_PAYLOAD);

// ACT
final OffsetDateTime validUntil = now.plusMonths(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,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 Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs.policystore.testutil;

import java.io.StringReader;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;

public class PolicyStoreTestUtil {

public static JsonObject toJsonObject(final String jsonString) {
final JsonObject jsonObject;
try (JsonReader jsonReader = Json.createReader(new StringReader(jsonString))) {
jsonObject = jsonReader.readObject();
}
return jsonObject;
}
}

0 comments on commit d3e1dcf

Please sign in to comment.