diff options
author | Dan Timoney <dtimoney@att.com> | 2024-02-21 13:55:50 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2024-02-21 13:55:50 +0000 |
commit | 7ee91efb37ab936c130e0f4aa40d961be6873fa3 (patch) | |
tree | 0feac2c6eca679100de58ee29e9c9f69dfcee7ed /a1-policy-management | |
parent | b02b72a9ac3a701129d097d378147bb3537394f1 (diff) | |
parent | 00fd5055c54d94b99cd52e8bd737f2bfac4287d0 (diff) |
Merge "Improve error message for A1 client custom adapter handling"
Diffstat (limited to 'a1-policy-management')
2 files changed, 36 insertions, 12 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java index 87776c0f..b5c10bf6 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java @@ -3,6 +3,7 @@ * ONAP : ccsdk oran * ====================================================================== * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved. + * Copyright (C) 2024 OpenInfra Foundation Europe. 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. @@ -105,22 +106,31 @@ public class A1ClientFactory { private A1Client createCustomAdapter(Ric ric) throws ServiceException { try { - Class<?> clazz = Class.forName(ric.getConfig().getCustomAdapterClass()); - if (A1Client.class.isAssignableFrom(clazz)) { - Constructor<?> constructor = clazz.getConstructor(RicConfig.class, AsyncRestClientFactory.class); - logger.debug("A1Client (" + clazz.getTypeName() + ") being created for ric: {}", - ric.getConfig().getRicId()); - return (A1Client) constructor.newInstance(ric.getConfig(), this.restClientFactory); - } else if (A1Client.Factory.class.isAssignableFrom(clazz)) { - A1Client.Factory factory = (A1Client.Factory) clazz.getDeclaredConstructor().newInstance(); - logger.debug("A1Client (" + clazz.getTypeName() + ") factory creating client for ric: {}", - ric.getConfig().getRicId()); - return factory.create(ric.getConfig(), this.restClientFactory); + if (ric.getConfig().getCustomAdapterClass() != null && !ric.getConfig().getCustomAdapterClass().isEmpty()) { + Class<?> clazz = Class.forName(ric.getConfig().getCustomAdapterClass()); + if (A1Client.class.isAssignableFrom(clazz)) { + Constructor<?> constructor = clazz.getConstructor(RicConfig.class, AsyncRestClientFactory.class); + logger.debug("A1Client (" + clazz.getTypeName() + ") being created for ric: {}", + ric.getConfig().getRicId()); + return (A1Client) constructor.newInstance(ric.getConfig(), this.restClientFactory); + } else if (A1Client.Factory.class.isAssignableFrom(clazz)) { + A1Client.Factory factory = (A1Client.Factory) clazz.getDeclaredConstructor().newInstance(); + logger.debug("A1Client (" + clazz.getTypeName() + ") factory creating client for ric: {}", + ric.getConfig().getRicId()); + return factory.create(ric.getConfig(), this.restClientFactory); + } else { + throw new ServiceException("The custom class must either implement A1Client.Factory or A1Client"); + } } else { - throw new ServiceException("The custom class must either implement A1Client.Factory or A1Client"); + throw new ServiceException("Custom adapter class is required to use A1ProtocolType.CUSTOM_PROTOCOL"); } } catch (ClassNotFoundException e) { throw new ServiceException("Could not find class: " + ric.getConfig().getCustomAdapterClass(), e); + } catch (NoSuchMethodException e) { + throw new ServiceException("Could not find the required constructor in class " + + ric.getConfig().getCustomAdapterClass(), e); + } catch (ServiceException e) { + throw e; } catch (Exception e) { throw new ServiceException("Cannot create custom adapter: " + ric.getConfig().getCustomAdapterClass(), e); } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java index f50d5537..a23540e5 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java @@ -3,6 +3,7 @@ * ONAP : ccsdk oran * ====================================================================== * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved. + * Copyright (C) 2024 OpenInfra Foundation Europe. 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. @@ -41,6 +42,7 @@ import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -148,6 +150,18 @@ class A1ClientFactoryTest { factoryUnderTest.createClient(new Ric(ricConfig("", "junk")), A1ProtocolType.CUSTOM_PROTOCOL); }); assertEquals("Could not find class: junk", e.getMessage()); + + Exception exceptionNoSuchMethod = Assertions.assertThrows(Exception.class, () -> { + factoryUnderTest.createClient(new Ric(ricConfig("", MockA1Client.class.getName())), + A1ProtocolType.CUSTOM_PROTOCOL); + }); + assertEquals("Could not find the required constructor in class " + MockA1Client.class.getName(), + exceptionNoSuchMethod.getMessage()); + + Exception exceptionNullCustomAdaptor = Assertions.assertThrows(Exception.class, () -> { + factoryUnderTest.createClient(new Ric(ricConfig("", null)), A1ProtocolType.CUSTOM_PROTOCOL); + }); + assertEquals("Custom adapter class is required to use A1ProtocolType.CUSTOM_PROTOCOL", exceptionNullCustomAdaptor.getMessage()); } @Test |