diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-01-19 22:23:10 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-01-19 22:23:10 +0000 |
commit | 7bfd0ec1c77719edda241a893a75a9b8797b7a42 (patch) | |
tree | 626192580c3d0ccb910d2d34b03f30c0865c69c4 | |
parent | 8084d25dd393e564ea57f9218bf44e3d1c037aa0 (diff) | |
parent | f49a43f102d99edd269a49ee531b9a2194ac6937 (diff) |
Merge "Convert Model Exception to Runtime Exception"
3 files changed, 38 insertions, 9 deletions
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. @@ -77,6 +77,18 @@ public class PfModelRuntimeException extends RuntimeException implements ErrorRe } /** + * 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. * * @param statusCode the return code for the 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 <T, R> Map<String, R> mapMap(Map<String, T> source, Function<T, R> mapFunc, - Map<String, R> defaultValue) { + public static <K, T, R> Map<K, R> mapMap(Map<K, T> source, Function<T, R> mapFunc, + Map<K, R> defaultValue) { if (source == null) { return defaultValue; } - Map<String, R> map = new LinkedHashMap<>(); - for (Entry<String, T> ent : source.entrySet()) { + Map<K, R> map = new LinkedHashMap<>(); + for (Entry<K, T> 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 <T, R> Map<String, R> mapMap(Map<String, T> source, Function<T, R> mapFunc) { + public static <K, T, R> Map<K, R> mapMap(Map<K, T> source, Function<T, R> mapFunc) { return mapMap(source, mapFunc, null); } diff --git a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java index 112f6a203..52d17e74f 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import org.junit.Test; import org.onap.policy.models.errors.concepts.ErrorResponse; @@ -60,5 +61,21 @@ public class ExceptionsTest { assertEquals("Runtime Message\nIO runtime exception message", String.join("\n", errorResponse.getErrorDetails())); assertEquals(key, re.getObject()); + + PfModelRuntimeException pfre = new PfModelRuntimeException(ae); + assertEquals(ae.getErrorResponse().getResponseCode(), pfre.getErrorResponse().getResponseCode()); + assertEquals(ae.getMessage(), pfre.getMessage()); + + try { + try { + throw new PfModelException(Status.BAD_GATEWAY, "An Exception"); + } catch (PfModelException pfme) { + throw new PfModelRuntimeException(pfme); + } + } catch (PfModelRuntimeException pfmre) { + assertEquals(Status.BAD_GATEWAY, pfmre.getErrorResponse().getResponseCode()); + assertEquals("An Exception", pfmre.getMessage()); + assertEquals(PfModelException.class.getName(), pfmre.getCause().getClass().getName()); + } } } |