From f49a43f102d99edd269a49ee531b9a2194ac6937 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 18 Jan 2021 11:17:28 +0000 Subject: Convert Model Exception to Runtime Exception Sometimes the exception thrown by the database providers has to be converted to a runtime exception, especiall if the database provider is running in a thread (called by the rum() method that does not return exceptions). It is convenient to have a constructor on the PfModelRuntimeException class that takes a PfModelException class as its argument, thus creating the runtime exception. Also the mapMap method in pfUtils is updated to allow the key of the map to be templated. By doing this, the utility can be used for maps that are keyed with types other than String (such as UUID). Issue-ID: POLICY-2971 Change-Id: Ib0b3678de531fd383753a1cd0ce47a06f4079ec0 Signed-off-by: liamfallon --- .../onap/policy/models/base/PfModelRuntimeException.java | 14 +++++++++++++- .../src/main/java/org/onap/policy/models/base/PfUtils.java | 12 ++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'models-base/src/main/java/org') diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java index 5c317d1d8..cd0562f90 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019, 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,6 +76,18 @@ public class PfModelRuntimeException extends RuntimeException implements ErrorRe this(statusCode, message, exception, null); } + /** + * Instantiates a new model runtime exception from a PfModelException instance. + * + * @param exception the exception that caused this model exception + */ + public PfModelRuntimeException(final PfModelException exception) { + super(exception.getMessage(), exception); + this.object = exception.getObject(); + errorResponse.setResponseCode(exception.getErrorResponse().getResponseCode()); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); + } + /** * Instantiates a new model runtime exception. * diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java index 57d23450a..6a119d6c4 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -137,14 +137,14 @@ public final class PfUtils { * @param defaultValue value to be returned if source is {@code null} * @return a new map, containing mappings of all of the items in the original map */ - public static Map mapMap(Map source, Function mapFunc, - Map defaultValue) { + public static Map mapMap(Map source, Function mapFunc, + Map defaultValue) { if (source == null) { return defaultValue; } - Map map = new LinkedHashMap<>(); - for (Entry ent : source.entrySet()) { + Map map = new LinkedHashMap<>(); + for (Entry ent : source.entrySet()) { map.put(ent.getKey(), mapFunc.apply(ent.getValue())); } @@ -159,7 +159,7 @@ public final class PfUtils { * @return a new map, containing mappings of all of the items in the original map, or {@code null} if the source is * {@code null} */ - public static Map mapMap(Map source, Function mapFunc) { + public static Map mapMap(Map source, Function mapFunc) { return mapMap(source, mapFunc, null); } -- cgit 1.2.3-korg