Removed throwing of runtime exceptions 78/3278/1
authorPatrikBuhr <patrik.buhr@est.tech>
Wed, 15 Apr 2020 10:48:16 +0000 (12:48 +0200)
committerPatrikBuhr <patrik.buhr@est.tech>
Wed, 15 Apr 2020 10:55:31 +0000 (12:55 +0200)
Change-Id: I74565f457b0d93be7d580f775a48c2589c5a0e25
Issue-ID: NONRTRIC-164
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/main/java/org/oransc/policyagent/repository/Lock.java
policy-agent/src/test/java/org/oransc/policyagent/clients/SdncOscA1ClientTest.java

index 6860173..c29d70e 100644 (file)
@@ -69,7 +69,6 @@ public class SdncOscA1Client implements A1Client {
         .create(); //
 
     private static final String GET_POLICY_RPC = "getA1Policy";
-    private static final String UNHANDELED_PROTOCOL = "Bug, unhandeled protocoltype: ";
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
     private final ControllerConfig controllerConfig;
     private final AsyncRestClient restClient;
@@ -110,8 +109,14 @@ public class SdncOscA1Client implements A1Client {
             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
                 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString) //
                 .collectList();
+        } else {
+            return Mono.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
+
+    }
+
+    private Exception createIllegalProtocolException() {
+        return new NullPointerException("Bug, unhandeled protocoltype: " + this.protocolType);
     }
 
     @Override
@@ -128,14 +133,18 @@ public class SdncOscA1Client implements A1Client {
             OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig);
             final String ricUrl = uri.createGetSchemaUri(policyTypeId);
             return post(GET_POLICY_RPC, ricUrl, Optional.empty());
+        } else {
+            return Mono.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
     }
 
     @Override
     public Mono<String> putPolicy(Policy policy) {
-        final String ricUrl = getUriBuilder().createPutPolicyUri(policy.type().name(), policy.id());
-        return post("putA1Policy", ricUrl, Optional.of(policy.json()));
+        return getUriBuilder() //
+            .flatMap(builder -> {
+                String ricUrl = builder.createPutPolicyUri(policy.type().name(), policy.id());
+                return post("putA1Policy", ricUrl, Optional.of(policy.json()));
+            });
     }
 
     @Override
@@ -154,8 +163,9 @@ public class SdncOscA1Client implements A1Client {
                 .flatMapMany(Flux::fromIterable)
                 .flatMap(type -> post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty())) //
                 .flatMap(SdncJsonHelper::parseJsonArrayOfString);
+        } else {
+            return Flux.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
     }
 
     @Override
@@ -166,17 +176,21 @@ public class SdncOscA1Client implements A1Client {
 
     @Override
     public Mono<String> getPolicyStatus(Policy policy) {
-        final String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.type().name(), policy.id());
-        return post("getA1PolicyStatus", ricUrl, Optional.empty());
+        return getUriBuilder() //
+            .flatMap(builder -> {
+                String ricUrl = builder.createGetPolicyStatusUri(policy.type().name(), policy.id());
+                return post("getA1PolicyStatus", ricUrl, Optional.empty());
+            });
     }
 
-    private A1UriBuilder getUriBuilder() {
+    private Mono<A1UriBuilder> getUriBuilder() {
         if (protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
-            return new StdA1ClientVersion1.UriBuilder(ricConfig);
+            return Mono.just(new StdA1ClientVersion1.UriBuilder(ricConfig));
         } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
-            return new OscA1Client.UriBuilder(ricConfig);
+            return Mono.just(new OscA1Client.UriBuilder(ricConfig));
+        } else {
+            return Mono.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
     }
 
     private Mono<A1ProtocolType> tryOscProtocolVersion() {
@@ -203,13 +217,17 @@ public class SdncOscA1Client implements A1Client {
                 .flatMapMany(Flux::fromIterable)
                 .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
                 .flatMap(SdncJsonHelper::parseJsonArrayOfString);
+        } else {
+            return Flux.error(createIllegalProtocolException());
         }
-        throw new NullPointerException(UNHANDELED_PROTOCOL + this.protocolType);
     }
 
     private Mono<String> deletePolicyById(String type, String policyId) {
-        final String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
-        return post("deleteA1Policy", ricUrl, Optional.empty());
+        return getUriBuilder() //
+            .flatMap(builder -> {
+                String ricUrl = builder.createDeleteUri(type, policyId);
+                return post("deleteA1Policy", ricUrl, Optional.empty());
+            });
     }
 
     private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
index e8c75ec..6d3f594 100644 (file)
@@ -28,6 +28,7 @@ import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 
+import java.lang.invoke.MethodHandles;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -46,6 +47,8 @@ import org.oransc.policyagent.repository.Ric;
 import org.oransc.policyagent.repository.Rics;
 import org.oransc.policyagent.repository.Service;
 import org.oransc.policyagent.repository.Services;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -84,6 +87,7 @@ public class PolicyController {
     @Autowired
     private Services services;
 
+    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
     private static Gson gson = new GsonBuilder() //
         .serializeNulls() //
         .create(); //
@@ -399,7 +403,7 @@ public class PolicyController {
             policyInfo.service = p.ownerServiceName();
             policyInfo.lastModified = p.lastModified();
             if (!policyInfo.validate()) {
-                throw new NullPointerException("BUG, all fields must be set");
+                logger.error("BUG, all fields must be set");
             }
             v.add(policyInfo);
         }
index 9f02f08..def2b30 100644 (file)
@@ -117,7 +117,7 @@ public class Lock {
         synchronized (this) {
             if (lockCounter <= 0) {
                 lockCounter = -1; // Might as well stop, to make it easier to find the problem
-                throw new NullPointerException("Number of unlocks must match the number of locks");
+                logger.error("Number of unlocks must match the number of locks");
             }
             this.lockCounter--;
             if (lockCounter == 0) {
index 2b60fe0..fd50425 100644 (file)
@@ -39,6 +39,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.stubbing.OngoingStubbing;
 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterOutput;
+import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest;
 import org.oransc.policyagent.configuration.ControllerConfig;
 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
 import org.oransc.policyagent.repository.Policy;
@@ -163,7 +164,7 @@ public class SdncOscA1ClientTest {
             .block();
         assertEquals("OK", returned, "");
         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
-        ImmutableAdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
+        AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
             .nearRtRicUrl(expUrl) //
             .body(POLICY_JSON_VALID) //
             .build();
@@ -185,9 +186,13 @@ public class SdncOscA1ClientTest {
 
         Mono<String> returnedMono = clientUnderTest
             .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
+        StepVerifier.create(returnedMono) //
+            .expectSubscription() //
+            .expectErrorMatches(t -> t instanceof WebClientResponseException) //
+            .verify();
 
         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
-        ImmutableAdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
+        AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
             .nearRtRicUrl(expUrl) //
             .body(policyJson) //
             .build();
@@ -207,7 +212,7 @@ public class SdncOscA1ClientTest {
             .block();
         assertEquals("OK", returned, "");
         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
-        ImmutableAdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
+        AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
             .nearRtRicUrl(expUrl) //
             .build();
         String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
@@ -227,7 +232,7 @@ public class SdncOscA1ClientTest {
         assertEquals("OK", returnedStatus, "unexpected status");
 
         final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
-        ImmutableAdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
+        AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
             .nearRtRicUrl(expUrl) //
             .build();
         String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);