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 --- .../org/onap/policy/models/base/ExceptionsTest.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'models-base/src/test/java') 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()); + } } } -- cgit 1.2.3-korg