Remove lambda code smells from policyagent/clients
Change-Id: I9f4ef0041989d6565b0982e35e3521968e7d59fc
Issue-ID: NONRTRIC-142
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
diff --git a/policy-agent/pom.xml b/policy-agent/pom.xml
index 78ff193..b8dcbc4 100644
--- a/policy-agent/pom.xml
+++ b/policy-agent/pom.xml
@@ -48,7 +48,7 @@
<properties>
<java.version>11</java.version>
<springfox.version>2.9.2</springfox.version>
- <immutable.version>2.7.1</immutable.version>
+ <immutable.version>2.7.4</immutable.version>
<sdk.version>1.1.6</sdk.version>
<swagger.version>2.0.0</swagger.version>
<json.version>20180130</json.version>
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java
index d03dbb4..0efe14d 100644
--- a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java
+++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java
@@ -23,7 +23,6 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
-
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -38,7 +37,7 @@
public class OscA1Client implements A1Client {
private static final String URL_PREFIX = "/a1-p";
- private static final String POLICYTYPES = "/policytypes";
+ private static final String POLICY_TYPES = "/policytypes";
private static final String CREATE_SCHEMA = "create_schema";
private static final String TITLE = "title";
@@ -74,16 +73,14 @@
@Override
public Mono<List<String>> getPolicyTypeIdentities() {
- return restClient.get(POLICYTYPES) //
- .flatMap(this::parseJsonArrayOfString);
+ return getPolicyTypeIds() //
+ .collectList();
}
@Override
public Mono<List<String>> getPolicyIdentities() {
- return getPolicyTypeIdentities() //
- .flatMapMany(types -> Flux.fromIterable(types)) //
- .flatMap(type -> getPolicyIdentitiesById(type)) //
- .flatMap(policyIds -> Flux.fromIterable(policyIds)) //
+ return getPolicyTypeIds() //
+ .flatMap(this::getPolicyIdentitiesByType) //
.collectList();
}
@@ -108,14 +105,13 @@
@Override
public Mono<A1ProtocolType> getProtocolVersion() {
return restClient.get(HEALTHCHECK) //
- .flatMap(resp -> Mono.just(A1ProtocolType.OSC_V1));
+ .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
}
@Override
public Flux<String> deleteAllPolicies() {
- return getPolicyTypeIdentities() //
- .flatMapMany(types -> Flux.fromIterable(types)) //
- .flatMap(typeId -> deletePoliciesForType(typeId)); //
+ return getPolicyTypeIds() //
+ .flatMap(this::deletePoliciesForType);
}
@Override
@@ -125,10 +121,14 @@
}
- private Mono<List<String>> getPolicyIdentitiesById(String typeId) {
- String uri = POLICY_IDS_URI.buildAndExpand(typeId).toUriString();
- return restClient.get(uri) //
- .flatMap(this::parseJsonArrayOfString);
+ private Flux<String> getPolicyTypeIds() {
+ return restClient.get(POLICY_TYPES) //
+ .flatMapMany(this::parseJsonArrayOfString);
+ }
+
+ private Flux<String> getPolicyIdentitiesByType(String typeId) {
+ return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) //
+ .flatMapMany(this::parseJsonArrayOfString);
}
private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
@@ -149,12 +149,11 @@
}
private Flux<String> deletePoliciesForType(String typeId) {
- return getPolicyIdentitiesById(typeId) //
- .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
- .flatMap(policyId -> deletePolicyById(typeId, policyId)); //
+ return getPolicyIdentitiesByType(typeId) //
+ .flatMap(policyId -> deletePolicyById(typeId, policyId));
}
- private Mono<List<String>> parseJsonArrayOfString(String inputString) {
+ private Flux<String> parseJsonArrayOfString(String inputString) {
try {
List<String> arrayList = new ArrayList<>();
JSONArray jsonArray = new JSONArray(inputString);
@@ -162,9 +161,9 @@
arrayList.add(jsonArray.getString(i));
}
logger.debug("A1 client: received list = {}", arrayList);
- return Mono.just(arrayList);
+ return Flux.fromIterable(arrayList);
} catch (JSONException ex) { // invalid json
- return Mono.error(ex);
+ return Flux.error(ex);
}
}
}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java
index d39c211..44799c8 100644
--- a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java
+++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOnapA1Client.java
@@ -27,7 +27,6 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
-
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -35,7 +34,6 @@
import org.oransc.policyagent.repository.Policy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -51,7 +49,7 @@
private static Gson gson = new GsonBuilder() //
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
- .create(); //
+ .create();
public SdncOnapA1Client(RicConfig ricConfig, String baseUrl, String username, String password) {
this(ricConfig, username, password, new AsyncRestClient(baseUrl + "/restconf/operations"));
@@ -69,43 +67,17 @@
@Override
public Mono<List<String>> getPolicyTypeIdentities() {
- SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
- .nearRtRicId(ricConfig.baseUrl()) //
- .build();
- String inputJsonString = createInputJsonString(inputParams);
- logger.debug("POST getPolicyTypeIdentities inputJsonString = {}", inputJsonString);
-
- return restClient
- .postWithAuthHeader(URL_PREFIX + "getPolicyTypes", inputJsonString, a1ControllerUsername,
- a1ControllerPassword) //
- .flatMap(response -> getValueFromResponse(response, "policy-type-id-list")) //
- .flatMap(this::parseJsonArrayOfString);
+ return getPolicyTypeIds() //
+ .collectList();
}
@Override
public Mono<List<String>> getPolicyIdentities() {
- return getPolicyTypeIdentities() //
- .flatMapMany(types -> Flux.fromIterable(types)) //
- .flatMap(type -> getPolicyIdentities(type)) //
- .flatMap(policyIds -> Flux.fromIterable(policyIds)) //
+ return getPolicyTypeIds() //
+ .flatMap(this::getPolicyIdentitiesByType) //
.collectList();
}
- public Mono<List<String>> getPolicyIdentities(String policyTypeId) {
- SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
- .nearRtRicId(ricConfig.baseUrl()) //
- .policyTypeId(policyTypeId) //
- .build();
- String inputJsonString = createInputJsonString(inputParams);
- logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString);
-
- return restClient
- .postWithAuthHeader(URL_PREFIX + "getPolicyInstances", inputJsonString, a1ControllerUsername,
- a1ControllerPassword) //
- .flatMap(response -> getValueFromResponse(response, "policy-instance-id-list")) //
- .flatMap(this::parseJsonArrayOfString);
- }
-
@Override
public Mono<String> getPolicyTypeSchema(String policyTypeId) {
SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
@@ -131,6 +103,7 @@
.policyInstance(policy.json()) //
.properties(new ArrayList<String>()) //
.build();
+
String inputJsonString = createInputJsonString(inputParams);
logger.debug("POST putPolicy inputJsonString = {}", inputJsonString);
@@ -138,35 +111,21 @@
a1ControllerUsername, a1ControllerPassword);
}
- public Mono<String> deletePolicyByIds(String policyTypeId, String policyId) {
- SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
- .nearRtRicId(ricConfig.baseUrl()) //
- .policyTypeId(policyTypeId) //
- .policyInstanceId(policyId) //
- .build();
- String inputJsonString = createInputJsonString(inputParams);
- logger.debug("POST deletePolicy inputJsonString = {}", inputJsonString);
-
- return restClient.postWithAuthHeader(URL_PREFIX + "deletePolicyInstance", inputJsonString,
- a1ControllerUsername, a1ControllerPassword);
- }
-
@Override
public Mono<String> deletePolicy(Policy policy) {
- return deletePolicyByIds(policy.type().name(), policy.id());
+ return deletePolicyByTypeId(policy.type().name(), policy.id());
}
@Override
public Flux<String> deleteAllPolicies() {
- return getPolicyTypeIdentities() //
- .flatMapMany(types -> Flux.fromIterable(types)) //
- .flatMap(typeId -> deletePoliciesForType(typeId)); //
+ return getPolicyTypeIds() //
+ .flatMap(this::deletePoliciesForType); //
}
@Override
public Mono<A1ProtocolType> getProtocolVersion() {
return getPolicyTypeIdentities() //
- .flatMap(x -> Mono.just(A1ProtocolType.SDNC_ONAP));
+ .flatMap(notUsed -> Mono.just(A1ProtocolType.SDNC_ONAP));
}
@Override
@@ -174,6 +133,35 @@
return Mono.error(new Exception("Status not implemented in the controller"));
}
+ private Flux<String> getPolicyTypeIds() {
+ SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
+ .nearRtRicId(ricConfig.baseUrl()) //
+ .build();
+ String inputJsonString = createInputJsonString(inputParams);
+ logger.debug("POST getPolicyTypeIdentities inputJsonString = {}", inputJsonString);
+
+ return restClient
+ .postWithAuthHeader("/A1-ADAPTER-API:getPolicyTypes", inputJsonString, a1ControllerUsername,
+ a1ControllerPassword) //
+ .flatMap(response -> getValueFromResponse(response, "policy-type-id-list")) //
+ .flatMapMany(this::parseJsonArrayOfString);
+ }
+
+ private Flux<String> getPolicyIdentitiesByType(String policyTypeId) {
+ SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
+ .nearRtRicId(ricConfig.baseUrl()) //
+ .policyTypeId(policyTypeId) //
+ .build();
+ String inputJsonString = createInputJsonString(inputParams);
+ logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString);
+
+ return restClient
+ .postWithAuthHeader("/A1-ADAPTER-API:getPolicyInstances", inputJsonString, a1ControllerUsername,
+ a1ControllerPassword) //
+ .flatMap(response -> getValueFromResponse(response, "policy-instance-id-list")) //
+ .flatMapMany(this::parseJsonArrayOfString);
+ }
+
private Mono<String> getValueFromResponse(String response, String key) {
logger.debug("A1 client: response = {}", response);
try {
@@ -189,20 +177,19 @@
}
}
- private Mono<List<String>> parseJsonArrayOfString(String inputString) {
+ private Flux<String> parseJsonArrayOfString(String inputString) {
try {
List<String> arrayList = new ArrayList<>();
- if (inputString.isEmpty()) {
- return Mono.just(arrayList);
- }
- JSONArray jsonArray = new JSONArray(inputString);
- for (int i = 0; i < jsonArray.length(); i++) {
- arrayList.add(jsonArray.getString(i));
+ if (!inputString.isEmpty()) {
+ JSONArray jsonArray = new JSONArray(inputString);
+ for (int i = 0; i < jsonArray.length(); i++) {
+ arrayList.add(jsonArray.getString(i));
+ }
}
logger.debug("A1 client: received list = {}", arrayList);
- return Mono.just(arrayList);
+ return Flux.fromIterable(arrayList);
} catch (JSONException ex) { // invalid json
- return Mono.error(ex);
+ return Flux.error(ex);
}
}
@@ -218,14 +205,26 @@
}
private Flux<String> deletePoliciesForType(String typeId) {
- return getPolicyIdentities(typeId) //
- .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
- .flatMap(policyId -> deletePolicyByIds(typeId, policyId)); //
+ return getPolicyIdentitiesByType(typeId) //
+ .flatMap(policyId -> deletePolicyByTypeId(typeId, policyId)); //
}
- private String createInputJsonString(SdncOnapAdapterInput inputParams) {
+ private Mono<String> deletePolicyByTypeId(String policyTypeId, String policyId) {
+ SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
+ .nearRtRicId(ricConfig.baseUrl()) //
+ .policyTypeId(policyTypeId) //
+ .policyInstanceId(policyId) //
+ .build();
+ String inputJsonString = createInputJsonString(inputParams);
+ logger.debug("POST deletePolicy inputJsonString = {}", inputJsonString);
+
+ return restClient.postWithAuthHeader("/A1-ADAPTER-API:deletePolicyInstance", inputJsonString,
+ a1ControllerUsername, a1ControllerPassword);
+ }
+
+ private String createInputJsonString(SdncOnapAdapterInput params) {
JSONObject inputJson = new JSONObject();
- inputJson.put("input", new JSONObject(gson.toJson(inputParams)));
+ inputJson.put("input", new JSONObject(gson.toJson(params)));
return inputJson.toString();
}
}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java
index 4000b36..bbb3121 100644
--- a/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java
+++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java
@@ -26,7 +26,6 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
-
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -34,7 +33,6 @@
import org.oransc.policyagent.repository.Policy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -78,22 +76,14 @@
.postWithAuthHeader(URL_PREFIX + "getPolicyTypeIdentities", inputJsonString, a1ControllerUsername,
a1ControllerPassword) //
.flatMap(response -> getValueFromResponse(response, "policy-type-id-list")) //
- .flatMap(this::parseJsonArrayOfString);
+ .flatMapMany(this::parseJsonArrayOfString) //
+ .collectList();
}
@Override
public Mono<List<String>> getPolicyIdentities() {
- SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() //
- .nearRtRicUrl(ricConfig.baseUrl()) //
- .build();
- String inputJsonString = createInputJsonString(inputParams);
- logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString);
-
- return restClient
- .postWithAuthHeader(URL_PREFIX + "getPolicyIdentities", inputJsonString, a1ControllerUsername,
- a1ControllerPassword) //
- .flatMap(response -> getValueFromResponse(response, "policy-id-list")) //
- .flatMap(this::parseJsonArrayOfString);
+ return getPolicyIds() //
+ .collectList();
}
@Override
@@ -136,9 +126,8 @@
@Override
public Flux<String> deleteAllPolicies() {
- return getPolicyIdentities() //
- .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) // )
- .flatMap(policyId -> deletePolicyById(policyId)); //
+ return getPolicyIds() //
+ .flatMap(this::deletePolicyById); //
}
@Override
@@ -152,6 +141,20 @@
return Mono.error(new Exception("Status not implemented in the SDNC controller"));
}
+ private Flux<String> getPolicyIds() {
+ SdncOscAdapterInput inputParams = ImmutableSdncOscAdapterInput.builder() //
+ .nearRtRicUrl(ricConfig.baseUrl()) //
+ .build();
+ String inputJsonString = createInputJsonString(inputParams);
+ logger.debug("POST getPolicyIdentities inputJsonString = {}", inputJsonString);
+
+ return restClient
+ .postWithAuthHeader("/A1-ADAPTER-API:getPolicyIdentities", inputJsonString, a1ControllerUsername,
+ a1ControllerPassword) //
+ .flatMap(response -> getValueFromResponse(response, "policy-id-list")) //
+ .flatMapMany(this::parseJsonArrayOfString);
+ }
+
private Mono<String> getValueFromResponse(String response, String key) {
logger.debug("A1 client: response = {}", response);
try {
@@ -167,20 +170,19 @@
}
}
- private Mono<List<String>> parseJsonArrayOfString(String inputString) {
+ private Flux<String> parseJsonArrayOfString(String inputString) {
try {
List<String> arrayList = new ArrayList<>();
- if (inputString.isEmpty()) {
- return Mono.just(arrayList);
- }
- JSONArray jsonArray = new JSONArray(inputString);
- for (int i = 0; i < jsonArray.length(); i++) {
- arrayList.add(jsonArray.getString(i));
+ if (!inputString.isEmpty()) {
+ JSONArray jsonArray = new JSONArray(inputString);
+ for (int i = 0; i < jsonArray.length(); i++) {
+ arrayList.add(jsonArray.getString(i));
+ }
}
logger.debug("A1 client: received list = {}", arrayList);
- return Mono.just(arrayList);
+ return Flux.fromIterable(arrayList);
} catch (JSONException ex) { // invalid json
- return Mono.error(ex);
+ return Flux.error(ex);
}
}
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1Client.java
index 7a43f3c..8f4334c 100644
--- a/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1Client.java
+++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/StdA1Client.java
@@ -37,7 +37,7 @@
public class StdA1Client implements A1Client {
private static final String URL_PREFIX = "/A1-P/v1";
- private static final String POLICYTYPES_URI = "/policytypes";
+ private static final String POLICY_TYPES_URI = "/policytypes";
private static final String POLICY_TYPE_ID = "policyTypeId";
private static final String POLICIES_URI = "/policies";
@@ -70,8 +70,8 @@
@Override
public Mono<List<String>> getPolicyIdentities() {
- return restClient.get(POLICIES_URI) //
- .flatMap(this::parseJsonArrayOfString);
+ return getPolicyIds() //
+ .collectList();
}
@Override
@@ -83,8 +83,9 @@
@Override
public Mono<List<String>> getPolicyTypeIdentities() {
- return restClient.get(POLICYTYPES_URI) //
- .flatMap(this::parseJsonArrayOfString);
+ return restClient.get(POLICY_TYPES_URI) //
+ .flatMapMany(this::parseJsonArrayOfString) //
+ .collectList();
}
@Override
@@ -101,9 +102,8 @@
@Override
public Flux<String> deleteAllPolicies() {
- return getPolicyIdentities() //
- .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) // )
- .flatMap(policyId -> deletePolicyById(policyId)); //
+ return getPolicyIds() //
+ .flatMap(this::deletePolicyById); //
}
@Override
@@ -118,12 +118,17 @@
return restClient.get(uri);
}
+ private Flux<String> getPolicyIds() {
+ return restClient.get(POLICIES_URI) //
+ .flatMapMany(this::parseJsonArrayOfString);
+ }
+
private Mono<String> deletePolicyById(String policyId) {
String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString();
return restClient.delete(uri);
}
- private Mono<List<String>> parseJsonArrayOfString(String inputString) {
+ private Flux<String> parseJsonArrayOfString(String inputString) {
try {
List<String> arrayList = new ArrayList<>();
JSONArray jsonArray = new JSONArray(inputString);
@@ -131,9 +136,9 @@
arrayList.add(jsonArray.getString(i));
}
logger.debug("A1 client: received list = {}", arrayList);
- return Mono.just(arrayList);
+ return Flux.fromIterable(arrayList);
} catch (JSONException ex) { // invalid json
- return Mono.error(ex);
+ return Flux.error(ex);
}
}
diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java
index b451a0a..f5b3b25 100644
--- a/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java
+++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java
@@ -44,9 +44,9 @@
private static final String CONTROLLER_USERNAME = "username";
private static final String CONTROLLER_PASSWORD = "password";
private static final String RIC_1_URL = "RicUrl";
- private static final String POLICYTYPES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyTypeIdentities";
- private static final String POLICIES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyIdentities";
- private static final String POLICYTYPES_URL = "/A1-ADAPTER-API:getPolicyType";
+ private static final String POLICY_TYPES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyTypeIdentities";
+ private static final String POLICY_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyIdentities";
+ private static final String POLICY_TYPES_URL = "/A1-ADAPTER-API:getPolicyType";
private static final String PUT_POLICY_URL = "/A1-ADAPTER-API:putPolicy";
private static final String DELETE_POLICY_URL = "/A1-ADAPTER-API:deletePolicy";
@@ -84,8 +84,8 @@
Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
- verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME,
- CONTROLLER_PASSWORD);
+ verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_IDENTITIES_URL, inputJsonString,
+ CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
}
@@ -102,7 +102,7 @@
Mono<List<String>> returnedMono = clientUnderTest.getPolicyIdentities();
- verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME,
+ verify(asyncRestClientMock).postWithAuthHeader(POLICY_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME,
CONTROLLER_PASSWORD);
StepVerifier.create(returnedMono).expectNext(policyIds).expectComplete().verify();
}
@@ -121,7 +121,7 @@
Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
- verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME,
+ verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_URL, inputJsonString, CONTROLLER_USERNAME,
CONTROLLER_PASSWORD);
StepVerifier.create(returnedMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
}
@@ -140,7 +140,7 @@
Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
- verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME,
+ verify(asyncRestClientMock).postWithAuthHeader(POLICY_TYPES_URL, inputJsonString, CONTROLLER_USERNAME,
CONTROLLER_PASSWORD);
StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
}
@@ -229,7 +229,7 @@
Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
StepVerifier.create(returnedFlux).expectComplete().verify();
- verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetIds,
+ verify(asyncRestClientMock).postWithAuthHeader(POLICY_IDENTITIES_URL, inputJsonStringGetIds,
CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy1,
CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java
index 40f09b9..f4e6821 100644
--- a/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java
+++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java
@@ -21,7 +21,6 @@
package org.oransc.policyagent.clients;
import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -41,9 +40,9 @@
@ExtendWith(MockitoExtension.class)
public class StdA1ClientTest {
private static final String RIC_URL = "RicUrl";
- private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes";
+ private static final String POLICY_TYPES_IDENTITIES_URL = "/policytypes";
private static final String POLICIES_IDENTITIES_URL = "/policies";
- private static final String POLICYTYPES_URL = "/policytypes/";
+ private static final String POLICY_TYPES_URL = "/policytypes/";
private static final String POLICIES_URL = "/policies/";
private static final String POLICY_TYPE_1_NAME = "type1";
@@ -69,42 +68,45 @@
@Test
public void testGetPolicyTypeIdentities() {
Mono<String> policyTypeIds = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).toString());
- when(asyncRestClientMock.get(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds);
+ when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIds);
Mono<?> policyTypeIdsFlux = clientUnderTest.getPolicyTypeIdentities();
- verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
+
+ verify(asyncRestClientMock).get(POLICY_TYPES_IDENTITIES_URL);
StepVerifier.create(policyTypeIdsFlux).expectNextCount(1).expectComplete().verify();
}
@Test
public void testGetPolicyIdentities() {
Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
- when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
+ when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
Mono<?> policyIdsFlux = clientUnderTest.getPolicyIdentities();
+
verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
StepVerifier.create(policyIdsFlux).expectNextCount(1).expectComplete().verify();
}
@Test
public void testGetValidPolicyType() {
- Mono<?> policyTypeResp =
+ Mono<String> policyTypeResp =
Mono.just("{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }");
- doReturn(policyTypeResp).when(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
+ when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
- verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
+
+ verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME);
StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
}
@Test
public void testGetInvalidPolicyType() {
- when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
- .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
+ when(asyncRestClientMock.get(anyString())).thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
- verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
+
+ verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME);
StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
.verify();
}
@@ -115,6 +117,7 @@
Mono<String> policyMono = clientUnderTest
.putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
+
verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID + "?policyTypeId=" + POLICY_TYPE, POLICY_JSON_VALID);
StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
}
@@ -125,6 +128,7 @@
Mono<String> policyMono = clientUnderTest
.putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
+
StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
}