Changes to resolve some sonar "major" issues in the PolicyEngineUtils
module.
Patch 2: Updated the NotificationStoreTest junit to account for the return
of a "Collections.emptyList()" rather than "null" in StdPDPNotification
class.
Patch 3: Updated the StdPDPNotificationTest junit to account for the
return of a "Collections.emptyList()" rather than "null" in
StdPDPNotifaction class.
Issue-ID: POLICY-474
Change-Id: I925774ac181fd38d1206637f47ab27ba499629ef
Signed-off-by: Magnusen, Drew (dm741q) <dm741q@att.com>
14 files changed:
package org.onap.policy.std.test;
import java.util.Collection;
package org.onap.policy.std.test;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
import org.junit.*;
import java.util.LinkedList;
import org.junit.*;
Collection<LoadedPolicy> result = fixture.getLoadedPolicies();
// add additional test code here
Collection<LoadedPolicy> result = fixture.getLoadedPolicies();
// add additional test code here
- assertEquals(null, result);
+ assertEquals(Collections.EMPTY_LIST, result);
Collection<RemovedPolicy> result = fixture.getRemovedPolicies();
// add additional test code here
Collection<RemovedPolicy> result = fixture.getRemovedPolicies();
// add additional test code here
- assertEquals(null, result);
+ assertEquals(Collections.EMPTY_LIST, result);
private static StdPDPNotification notificationRecord = new StdPDPNotification();
private static StdPDPNotification notificationRecord = new StdPDPNotification();
+ public NotificationStore () {
+ // Sonar prefers that we have an empty public constructor
+ // as opposed to an implicit public constructor.
+ }
+
public static StdPDPNotification getDeltaNotification(StdPDPNotification newNotification) {
StdPDPNotification notificationDelta = new StdPDPNotification();
ArrayList<StdRemovedPolicy> removedDelta = new ArrayList<>();
public static StdPDPNotification getDeltaNotification(StdPDPNotification newNotification) {
StdPDPNotification notificationDelta = new StdPDPNotification();
ArrayList<StdRemovedPolicy> removedDelta = new ArrayList<>();
@Override
public String getPolicyName() {
if(policyName!=null && policyName.contains(".xml")){
@Override
public String getPolicyName() {
if(policyName!=null && policyName.contains(".xml")){
- return (policyName.substring(0, policyName.substring(0, policyName.lastIndexOf('.')).lastIndexOf('.')));
+ return policyName.substring(0, policyName.substring(0, policyName.lastIndexOf('.')).lastIndexOf('.'));
}
return this.policyName;
}
}
return this.policyName;
}
package org.onap.policy.std;
import java.util.Collection;
package org.onap.policy.std;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import org.onap.policy.api.LoadedPolicy;
import org.onap.policy.api.NotificationType;
import java.util.HashSet;
import org.onap.policy.api.LoadedPolicy;
import org.onap.policy.api.NotificationType;
removed.addAll(removedPolicies);
return this.removed;
}
removed.addAll(removedPolicies);
return this.removed;
}
+ return Collections.emptyList();
updated.addAll(loadedPolicies);
return updated;
}
updated.addAll(loadedPolicies);
return updated;
}
+ return Collections.emptyList();
@Override
public String getPolicyName() {
if(policyName!=null && policyName.contains(".xml")){
@Override
public String getPolicyName() {
if(policyName!=null && policyName.contains(".xml")){
- return (policyName.substring(0, policyName.substring(0, policyName.lastIndexOf('.')).lastIndexOf('.')));
+ return policyName.substring(0, policyName.substring(0, policyName.lastIndexOf('.')).lastIndexOf('.'));
}
return this.policyName;
}
}
return this.policyName;
}
* @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
* @throws AAFPolicyException exceptions if any.
*/
* @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
* @throws AAFPolicyException exceptions if any.
*/
public void updateProperties(Properties properties) throws AAFPolicyException{
setup(properties);
}
public void updateProperties(Properties properties) throws AAFPolicyException{
setup(properties);
}
* @param action Permissions Action.
* @return
*/
* @param action Permissions Action.
* @return
*/
public boolean checkAuthPerm(String mechID, String pass, String type, String instance, String action){
return checkAuth(mechID, pass) && checkPerm(mechID, pass, type, instance, action);
}
public boolean checkAuthPerm(String mechID, String pass, String type, String instance, String action){
return checkAuth(mechID, pass) && checkPerm(mechID, pass, type, instance, action);
}
* @param pass Password.
* @return True or False.
*/
* @param pass Password.
* @return True or False.
*/
public boolean checkAuth(String userName, String pass){
if(aafAuthn!=null){
try {
public boolean checkAuth(String userName, String pass){
if(aafAuthn!=null){
try {
* @param action Permissions Action.
* @return True or False.
*/
* @param action Permissions Action.
* @return True or False.
*/
public boolean checkPerm(String userName, String pass, String type, String instance, String action){
int i =0;
Boolean result= false;
public boolean checkPerm(String userName, String pass, String type, String instance, String action){
int i =0;
Boolean result= false;
private static final long serialVersionUID = 1910606668038621L;
public AAFPolicyException() {
private static final long serialVersionUID = 1910606668038621L;
public AAFPolicyException() {
+ // Empty constructor.
+ // Nothing needs to be initialized in this exception class.
}
public AAFPolicyException(String message) {
}
public AAFPolicyException(String message) {
*
* @param notification <code>PDPNotification</code> of {@link org.onap.policy.api.PDPNotification} is the object that has information of the notification.
*/
*
* @param notification <code>PDPNotification</code> of {@link org.onap.policy.api.PDPNotification} is the object that has information of the notification.
*/
public void notificationReceived(PDPNotification notification);
/**
public void notificationReceived(PDPNotification notification);
/**
*/
public static synchronized BackUpMonitor getInstance(String resourceNodeName, String resourceName,
Properties properties, BackUpHandler handler) throws BackUpMonitorException {
*/
public static synchronized BackUpMonitor getInstance(String resourceNodeName, String resourceName,
Properties properties, BackUpHandler handler) throws BackUpMonitorException {
- if (resourceNodeName == null || resourceNodeName.trim().equals("") || resourceName == null
- || resourceName.trim().equals("") || properties == null || handler == null) {
+ if (resourceNodeName == null || "".equals(resourceNodeName.trim()) || resourceName == null
+ || "".equals(resourceName.trim()) || properties == null || handler == null) {
LOGGER.error("Error while getting Instance. Please check resourceName and/or properties file");
return null;
} else if ((resourceNodeName.equals(ResourceNode.ASTRA.toString())
LOGGER.error("Error while getting Instance. Please check resourceName and/or properties file");
return null;
} else if ((resourceNodeName.equals(ResourceNode.ASTRA.toString())
// This is to validate given Properties with required values.
private static Boolean validate(Properties properties) {
if (properties.getProperty("javax.persistence.jdbc.driver") == null
// This is to validate given Properties with required values.
private static Boolean validate(Properties properties) {
if (properties.getProperty("javax.persistence.jdbc.driver") == null
- || properties.getProperty("javax.persistence.jdbc.driver").trim().equals("")) {
+ || "".equals(properties.getProperty("javax.persistence.jdbc.driver").trim())) {
LOGGER.error("javax.persistence.jdbc.driver property is empty");
return false;
}
if (properties.getProperty("javax.persistence.jdbc.url") == null
LOGGER.error("javax.persistence.jdbc.driver property is empty");
return false;
}
if (properties.getProperty("javax.persistence.jdbc.url") == null
- || properties.getProperty("javax.persistence.jdbc.url").trim().equals("")) {
+ || "".equals(properties.getProperty("javax.persistence.jdbc.url").trim())) {
LOGGER.error("javax.persistence.jdbc.url property is empty");
return false;
}
if (properties.getProperty("javax.persistence.jdbc.user") == null
LOGGER.error("javax.persistence.jdbc.url property is empty");
return false;
}
if (properties.getProperty("javax.persistence.jdbc.user") == null
- || properties.getProperty("javax.persistence.jdbc.user").trim().equals("")) {
+ || "".equals(properties.getProperty("javax.persistence.jdbc.user").trim())) {
LOGGER.error("javax.persistence.jdbc.user property is empty");
return false;
}
if (properties.getProperty(PING_INTERVAL) == null
LOGGER.error("javax.persistence.jdbc.user property is empty");
return false;
}
if (properties.getProperty(PING_INTERVAL) == null
- || properties.getProperty(PING_INTERVAL).trim().equals("")) {
+ || "".equals(properties.getProperty(PING_INTERVAL).trim())) {
LOGGER.info("ping_interval property not specified. Taking default value");
} else {
try {
LOGGER.info("ping_interval property not specified. Taking default value");
} else {
try {
BackUpMonitorEntity masterEntity = masterEntities.get(0);
if (!masterEntity.getResourceName().equals(selfEntity.getResourceName())) {
Date currentTime = new Date();
BackUpMonitorEntity masterEntity = masterEntities.get(0);
if (!masterEntity.getResourceName().equals(selfEntity.getResourceName())) {
Date currentTime = new Date();
timeDiff = currentTime.getTime() - masterEntity.getTimeStamp().getTime();
if (timeDiff > (pingInterval + 1500)) {
// This is down or has an issue and we need to become Master while turning the
timeDiff = currentTime.getTime() - masterEntity.getTimeStamp().getTime();
if (timeDiff > (pingInterval + 1500)) {
// This is down or has an issue and we need to become Master while turning the
JsonNode patchNode = JsonDiff.asJson(oldNotification, notification);
LOGGER.info("Generated JSON Patch is " + patchNode.toString());
JsonPatch patch = JsonPatch.fromJson(patchNode);
JsonNode patchNode = JsonDiff.asJson(oldNotification, notification);
LOGGER.info("Generated JSON Patch is " + patchNode.toString());
JsonPatch patch = JsonPatch.fromJson(patchNode);
- try {
- JsonNode patched = patch.apply(oldNotification);
- LOGGER.info("Generated New Notification is : " + patched.toString());
- return patched.toString();
- } catch (JsonPatchException e) {
- LOGGER.error("Error generating Patched " + e.getMessage(), e);
- return null;
- }
+ return generatePatchNotification(patch, oldNotification);
} catch (IOException e) {
LOGGER.error("Error generating Patched " + e.getMessage(), e);
return null;
}
}
} catch (IOException e) {
LOGGER.error("Error generating Patched " + e.getMessage(), e);
return null;
}
}
+ private String generatePatchNotification(JsonPatch patch, JsonNode oldNotification) {
+ try {
+ JsonNode patched = patch.apply(oldNotification);
+ LOGGER.info("Generated New Notification is : " + patched.toString());
+ return patched.toString();
+ } catch (JsonPatchException e) {
+ LOGGER.error("Error generating Patched " + e.getMessage(), e);
+ return null;
+ }
+ }
+
+ /**
* Updates Notification in the Database while Performing the health check.
*
* @param notification
* Updates Notification in the Database while Performing the health check.
*
* @param notification
StdPDPNotification.class);
if (notificationObject.getNotificationType() != null) {
LOGGER.info("Performing Patched notification ");
StdPDPNotification.class);
if (notificationObject.getNotificationType() != null) {
LOGGER.info("Performing Patched notification ");
- try {
- handler.runOnNotification(notificationObject);
- notificationRecord = lastMasterNotification;
- } catch (Exception e) {
- LOGGER.error("Error in Clients Handler Object : " + e.getMessage(), e);
- }
+ performPatchNotification(notificationObject);
+
}
} catch (IOException e) {
LOGGER.info("Error while notification Conversion " + e.getMessage(), e);
}
} catch (IOException e) {
LOGGER.info("Error while notification Conversion " + e.getMessage(), e);
- // Used to set LastMasterNotification Record.
+ private static void performPatchNotification(PDPNotification notificationObject) {
+ try {
+ handler.runOnNotification(notificationObject);
+ notificationRecord = lastMasterNotification;
+ } catch (Exception e) {
+ LOGGER.error("Error in Clients Handler Object : " + e.getMessage(), e);
+ }
+
+ }
+
+ // Used to set LastMasterNotification Record.
private static void setLastNotification(String notification) {
synchronized (notificationLock) {
lastMasterNotification = notification;
private static void setLastNotification(String notification) {
synchronized (notificationLock) {
lastMasterNotification = notification;
- if (lastMasterNotification != null && !lastMasterNotification.equals("\"notificationType\":null")) {
+ if (lastMasterNotification != null && !"\"notificationType\":null".equals(lastMasterNotification)) {
if (lastMasterNotification.equals(notificationRecord)) {
return;
}
if (lastMasterNotification.equals(notificationRecord)) {
return;
}
private static final long serialVersionUID = 6778134503685443473L;
public BackUpMonitorException() {
private static final long serialVersionUID = 6778134503685443473L;
public BackUpMonitorException() {
+ // Nothing for this constructor to initialize
+ // in an exception class.
}
public BackUpMonitorException(String message) {
}
public BackUpMonitorException(String message) {
public Iterable<String> fetch() throws MRApiException {
try {
return this.consumer.fetch();
public Iterable<String> fetch() throws MRApiException {
try {
return this.consumer.fetch();
public void close() {
this.consumer.close();
}
public void close() {
this.consumer.close();
}
if (logLevel.compareTo(level) > 0) {
return;
}
if (logLevel.compareTo(level) > 0) {
return;
}
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
sb.append(new Date()).append(' ').append(level);
logtail(sb, args);
}
@Override
public void log(Exception e, Object... args) {
sb.append(new Date()).append(' ').append(level);
logtail(sb, args);
}
@Override
public void log(Exception e, Object... args) {
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
sb.append(new Date()).append(" EXCEPTION ").append(e.getMessage());
logtail(sb, args);
logger.error(e.getMessage() + e);
sb.append(new Date()).append(" EXCEPTION ").append(e.getMessage());
logtail(sb, args);
logger.error(e.getMessage() + e);
- private void logtail(StringBuffer sb, Object[] args) {
+ private void logtail(StringBuilder sb, Object[] args) {
for (Object o: args) {
String s = o.toString();
if (s.length() > 0) {
for (Object o: args) {
String s = o.toString();
if (s.length() > 0) {
public static String[] decodeBasicEncoding(String encodedValue) throws UnsupportedEncodingException {
if(encodedValue!=null && encodedValue.contains("Basic ")){
String encodedUserPassword = encodedValue.replaceFirst("Basic" + " ", "");
public static String[] decodeBasicEncoding(String encodedValue) throws UnsupportedEncodingException {
if(encodedValue!=null && encodedValue.contains("Basic ")){
String encodedUserPassword = encodedValue.replaceFirst("Basic" + " ", "");
- String usernameAndPassword = null;
+ String usernameAndPassword;
byte[] decodedBytes = Base64.getDecoder().decode(encodedUserPassword);
usernameAndPassword = new String(decodedBytes, "UTF-8");
StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
byte[] decodedBytes = Base64.getDecoder().decode(encodedUserPassword);
usernameAndPassword = new String(decodedBytes, "UTF-8");
StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
public void notificationTest() throws IOException{
// Notification Delta test first.
NotificationStore.recordNotification(new StdPDPNotification());
public void notificationTest() throws IOException{
// Notification Delta test first.
NotificationStore.recordNotification(new StdPDPNotification());
- assertEquals("{\"removedPolicies\":null,\"loadedPolicies\":null,\"notificationType\":null}", PolicyUtils.objectToJsonString(NotificationStore.getDeltaNotification(new StdPDPNotification())));
+ assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(NotificationStore.getDeltaNotification(new StdPDPNotification())));
// Initialize test
StdPDPNotification notification = new StdPDPNotification();
notification.setNotificationType(NotificationType.BOTH);
// Initialize test
StdPDPNotification notification = new StdPDPNotification();
notification.setNotificationType(NotificationType.BOTH);