diff options
author | Jim Hahn <jrh3@att.com> | 2020-04-07 13:58:09 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-04-07 17:15:07 -0400 |
commit | 9b3ff5f270572a6760ff07dda9577cdadb53b088 (patch) | |
tree | eacd325199fbb72fddaba94a057178a19e9c3360 /models-dao | |
parent | 3000fdca611c32b7001c553621660b8ea0d2eb49 (diff) |
Address sonar issues in models
Addressed the following sonar issues:
- use RE2 instead of java.util Pattern for "+" and "*"
- don't use deprecated methods
- for Date(long), sonar appeared not to parse the argument's
type correctly. Modified the code slightly to make sonar happy
- duplicate blocks of code
- either log or throw
- missing assert in junit
- for SDNR & VFC, eliminated threads, as they are unnecessary
- duplicate code block in different branches
- useless assignments
- redeclaring abstract methods
- cyclomatic complexity
- used lombok in some cases (e.g., EqualsAndHashCode)
- assert argument order
- actually deleted ControlLoopTargetType, because it is not needed
and sonar complains regardless of which order is used
- add private constructor to utility classes
- use StandardCharsets instead of literals
Also:
- added logback-test.xml to SO to eliminate the voluminous output
from the junit test
Issue-ID: POLICY-2305
Change-Id: I586c331781bedbd54a115a71847d04d293689445
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-dao')
3 files changed, 26 insertions, 37 deletions
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java index d61229722..d7d1784eb 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java @@ -58,7 +58,7 @@ public class PfDaoFactory { | InvocationTargetException | NoSuchMethodException | SecurityException e) { String errorMessage = "Policy Framework DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\""; - LOGGER.error(errorMessage, e); + LOGGER.error(errorMessage); throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, e); } diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java index d78172436..0b32b5123 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 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. @@ -120,7 +120,7 @@ public class DefaultPfDao implements PfDao { } catch (final Exception ex) { String errorMessage = "Creation of Policy Framework persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed"; - LOGGER.warn(errorMessage, ex); + LOGGER.warn(errorMessage); throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ex); } LOGGER.debug("Created Policy Framework persistence unit \"{}\"", daoParameters.getPersistenceUnit()); @@ -362,11 +362,7 @@ public class DefaultPfDao implements PfDao { try { if (filterMap != null) { - StringBuilder bld = new StringBuilder(filterQueryString); - for (String key : filterMap.keySet()) { - bld.append("c." + key + "= :" + key + AND); - } - filterQueryString = bld.toString(); + filterQueryString = buildFilter(filterMap, filterQueryString); } filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); if (getRecordNum > 0) { @@ -405,42 +401,30 @@ public class DefaultPfDao implements PfDao { } } + private String buildFilter(final Map<String, Object> filterMap, String filterQueryString) { + StringBuilder bld = new StringBuilder(filterQueryString); + for (String key : filterMap.keySet()) { + bld.append("c." + key + "= :" + key + AND); + } + return bld.toString(); + } + @Override public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey key) { - if (someClass == null) { - return null; - } - final EntityManager mg = getEntityManager(); - try { - final T t = mg.find(someClass, key); - if (t != null) { - mg.refresh(t); - } - return checkAndReturn(someClass, t); - } finally { - mg.close(); - } + return genericGet(someClass, key); } @Override public <T extends PfConcept> T get(final Class<T> someClass, final PfReferenceKey key) { - if (someClass == null) { - return null; - } - final EntityManager mg = getEntityManager(); - try { - final T t = mg.find(someClass, key); - if (t != null) { - mg.refresh(t); - } - return checkAndReturn(someClass, t); - } finally { - mg.close(); - } + return genericGet(someClass, key); } @Override public <T extends PfConcept> T get(final Class<T> someClass, final PfTimestampKey key) { + return genericGet(someClass, key); + } + + private <T extends PfConcept> T genericGet(final Class<T> someClass, final Object key) { if (someClass == null) { return null; } @@ -570,7 +554,11 @@ public class DefaultPfDao implements PfDao { final EntityManager mg = getEntityManager(); long size = 0; try { - size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class) + /* + * Concatenation should be safe because the class name should be safe, thus + * disabling sonar. + */ + size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class) // NOSONAR .getSingleResult(); } finally { mg.close(); diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java index f5702e60a..433196421 100644 --- a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java +++ b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 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. @@ -21,6 +21,7 @@ package org.onap.policy.models.dao; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -166,7 +167,7 @@ public class EntityTest { pfDao.getConcept(PfReferenceKey.class, nullRefKey); pfDao.size(null); - pfDao.close(); + assertThatCode(() -> pfDao.close()).doesNotThrowAnyException(); } private void testAllOps() { |