This patch fixes some Sonar issues in common/util directory.
Change-Id: I76ae980a3abb1d8a16d0197654cd9d759b13d597
Issue-Id: VFC-331
Signed-off-by: Shashank Kumar Shankar <shashank.kumar.shankar@intel.com>
private static Object getJsonFieldObject(JSONObject jsonObj, String fieldName, int classType) {
try {
if(null != jsonObj && jsonObj.has(fieldName)) {
- Object result = new Object();
+ Object result;
switch(classType) {
case TYPE_STRING:
result = "null".equals(jsonObj.getString(fieldName)) ? "" : jsonObj.getString(fieldName);
* @return
* @since VFC 1.0
*/
- public static String getStrValueByJson(JSONObject json, String parentKey, String key) {
+ public static String getStrValueByParentJson(JSONObject json, String parentKey, String key) {
if(parentKey.isEmpty()) {
return getStrValueByjson(json, key);
}
public static final String APPLICATION = "application/json";
+ public static final String NO_RESULT_EXCEPTION =
+ "org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result";
+
private static final Logger LOGGER = LoggerFactory.getLogger(RestfulUtil.class);
private static final Restful REST_CLIENT = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
if(null == result || result.isEmpty()) {
LOGGER.error("result from url:" + url + " result:" + result);
throw new ServiceException(
- ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
+ ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
}
JSONArray rsArray = null;
} catch(JSONException e) {
LOGGER.error("getResources error:" + e);
throw new ServiceException(
- ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
+ ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
}
return rsArray;
}
if(null == result || result.isEmpty()) {
LOGGER.error("result from url:" + url + " result:" + result);
throw new ServiceException(
- ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
+ ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
}
JSONArray rsArray = null;
} catch(JSONException e) {
LOGGER.error("getResources error:" + e);
throw new ServiceException(
- ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
+ ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
}
return rsArray;
}
public static boolean isInteger(String... strs) {
try {
for(String str : strs) {
- if(!StringUtils.isEmpty(str)) {
- int value = Integer.parseInt(str);
- if(value < 0) {
- return false;
- }
+ if(!StringUtils.isEmpty(str) && Integer.parseInt(str) < 0) {
+ return false;
}
}
} catch(NumberFormatException e) {
public static boolean isNumeric(String... strs) {
try {
for(String str : strs) {
- if(!StringUtils.isEmpty(str)) {
- float value = Float.parseFloat(str);
- if(value < 0) {
- return false;
- }
+ if(!StringUtils.isEmpty(str) && Float.parseFloat(str) < 0) {
+ return false;
}
}
} catch(NumberFormatException e) {
* @since VFC 1.0
*/
public static boolean compareZeroByInteger(String tatol, String used, String drTotal) {
- Integer ftotal = (int)0;
- Integer fused = (int)0;
- Integer fdrTotal = (int)0;
+ Integer ftotal = 0;
+ Integer fused = 0;
+ Integer fdrTotal = 0;
if(!StringUtils.isEmpty(tatol)) {
ftotal = Integer.valueOf(tatol);
}
*/
@SuppressWarnings("unchecked")
private static Map<String, String> getContextHeader(HttpServletRequest context) {
- Map<String, String> header = new HashMap<String, String>();
+ Map<String, String> header = new HashMap<>();
Enumeration<String> headerNames = context.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
* @since VFC 1.0
*/
public static JSONObject genHttpResponse(int retCode, String msg) {
- return genHttpResponse(null, createCodeMap(-1, retCode), msg, null);
+ return genHttpResponse(createCodeMap(-1, retCode), msg, null);
}
/**
* @since VFC 1.0
*/
public static JSONObject genHttpResponse(int retCode, String msg, Map<String, Object> map) {
- return genHttpResponse(null, createCodeMap(-1, retCode), msg, map);
+ return genHttpResponse(createCodeMap(-1, retCode), msg, map);
}
/**
* Roa request common return method <br/>
*
- * @param context
- * The http request context
* @param httpStatusCode
* The http response code
* @param retCode
* @return JSONObject The response for http request
* @since VFC 1.0
*/
- public static JSONObject genHttpResponse(HttpServletRequest context, int httpStatusCode, int retCode, String msg) {
- return genHttpResponse(context, createCodeMap(httpStatusCode, retCode), msg, null);
+ public static JSONObject genHttpResponse(int httpStatusCode, int retCode, String msg) {
+ return genHttpResponse(createCodeMap(httpStatusCode, retCode), msg, null);
}
/**
*
* Roa request common return method.<br>
*
- * @param context, The http request context
* @param codeMap
* @param msg, The message of request
* @param map, Other message of request
* @return
* @since VFC 1.0
*/
- public static JSONObject genHttpResponse(HttpServletRequest context, Map<String, Integer> codeMap, String msg,
+ public static JSONObject genHttpResponse(Map<String, Integer> codeMap, String msg,
Map<String, Object> map) {
JSONObject object = new JSONObject();
* @since VFC 1.0
*/
public static <T> JSONObject get(List<T> list) {
- Map<String, Object> map = new HashMap<String, Object>(10);
+ Map<String, Object> map = new HashMap<>(10);
map.put(ParamConstant.PARAM_DATA, list);
LOGGER.info("function=get; msg=get map:{}", map.toString());
return ResponseUtil.genHttpResponse(HttpConstant.OK_CODE, ResponseConstant.QUERY_SUCESS_MSG, map);
import java.lang.reflect.Modifier;
import org.junit.Test;
-import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
import mockit.Mock;
import mockit.MockUp;
jsonObj.put("a", "1");
jsonObj.put("b", "2");
String parentKey = "";
- String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
+ String result = JsonUtil.getStrValueByParentJson(jsonObj, parentKey, key);
String expectedResult = "1";
assertEquals(expectedResult, result);
}
return new JSONObject();
}
};
- String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
+ String result = JsonUtil.getStrValueByParentJson(jsonObj, parentKey, key);
String expectedResult = null;
assertEquals(expectedResult, result);
}
jsonObj.put("a", "1");
jsonObj.put("b", "2");
String parentKey = "b";
- String result = JsonUtil.getStrValueByJson(jsonObj, parentKey, key);
+ String result = JsonUtil.getStrValueByParentJson(jsonObj, parentKey, key);
String expectedResult = null;
assertEquals(expectedResult, result);
}
int httpStatusCode = -1;
int retCode1 = -1;
String msg1 = "123";
- JSONObject result = ResponseUtil.genHttpResponse(null, httpStatusCode, retCode1, msg1);
+ JSONObject result = ResponseUtil.genHttpResponse(httpStatusCode, retCode1, msg1);
JSONObject expectedResult = new JSONObject();
expectedResult.put("msg", "123");
assertEquals(result.toString(), expectedResult.toString());
map.put("a", -1);
map.put("b", 1);
String msg1 = "123";
- JSONObject result = ResponseUtil.genHttpResponse(null, codeMap, msg1, map);
+ JSONObject result = ResponseUtil.genHttpResponse(codeMap, msg1, map);
JSONObject expectedResult = new JSONObject();
expectedResult.put("msg", "123");
expectedResult.put("a", "-1");