diff options
Diffstat (limited to 'src')
4 files changed, 111 insertions, 132 deletions
diff --git a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java index af6e6151..837e37c2 100644 --- a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java +++ b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java @@ -87,7 +87,7 @@ public class MusicHealthCheck { pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)"); pQuery.addValue(UUID.randomUUID()); ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency); - System.out.println(rs); + logger.info(rs.toString()); if (rs != null) { return Boolean.TRUE; } else { @@ -119,7 +119,7 @@ public class MusicHealthCheck { String host = MusicUtil.getMyZkHost(); logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host); try { - MusicLockingService lockingService = MusicCore.getLockingServiceHandle(); + MusicCore.getLockingServiceHandle(); // additionally need to call the ZK to create,aquire and delete lock } catch (MusicLockingException e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR, diff --git a/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java b/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java index bef61f25..c406c694 100644 --- a/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java +++ b/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java @@ -3,6 +3,7 @@ * org.onap.music * =================================================================== * Copyright (c) 2017 AT&T Intellectual Property + * Copyright (c) 2019 IBM Intellectual Property * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -143,22 +144,8 @@ public enum AppMessages { this.resolution = resolution; } - - - - AppMessages(ErrorTypes eType, ErrorSeverity alarmSeverity, - ErrorSeverity errorSeverity, String errorCode, String errorDescription, String details, - String resolution) { - - this.eType = eType; - this.alarmSeverity = alarmSeverity; - this.errorSeverity = errorSeverity; - this.errorCode = errorCode; - this.errorDescription = errorDescription; - this.details = details; - this.resolution = resolution; - } - public ErrorTypes getEType() { + + public ErrorTypes getEType() { return eType; } diff --git a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java index 67f50dbb..8e4facd2 100755 --- a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java @@ -70,7 +70,6 @@ import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; -//import org.onap.music.main.CacheAccess; import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; @@ -126,7 +125,7 @@ public class RestMusicAdminAPI { ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); resultMap.put("Exception", "Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing."); - return Response.status(Status.UNAUTHORIZED).entity(resultMap).build(); + return response.status(Status.UNAUTHORIZED).entity(resultMap).build(); } PreparedQueryObject pQuery = new PreparedQueryObject(); @@ -137,7 +136,7 @@ public class RestMusicAdminAPI { if (!rs.all().isEmpty()) { resultMap.put("Exception", "Application " + appName + " has already been onboarded. Please contact admin."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } pQuery = new PreparedQueryObject(); @@ -158,12 +157,12 @@ public class RestMusicAdminAPI { if (returnStr.contains("Failure")) { resultMap.put("Exception", "Oops. Something wrong with onboarding process. Please retry later or contact admin."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } CachingUtil.updateisAAFCache(appName, isAAF); resultMap.put("Success", "Your application " + appName + " has been onboarded with MUSIC."); resultMap.put("Generated AID", uuid); - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } @@ -173,9 +172,8 @@ public class RestMusicAdminAPI { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response getOnboardedInfoSearch(JsonOnboard jsonObj) throws Exception { + ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); String appName = jsonObj.getAppname(); String uuid = jsonObj.getAid(); String isAAF = jsonObj.getIsAAF(); @@ -185,7 +183,7 @@ public class RestMusicAdminAPI { ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); resultMap.put("Exception", "Unauthorized: Please check the request parameters. Enter atleast one of the following parameters: appName(ns), aid, isAAF."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } PreparedQueryObject pQuery = new PreparedQueryObject(); @@ -212,21 +210,21 @@ public class RestMusicAdminAPI { ResultSet rs = MusicCore.get(pQuery); Iterator<Row> it = rs.iterator(); while (it.hasNext()) { - Row row = (Row) it.next(); + Row row = it.next(); resultMap.put(row.getUUID("uuid").toString(), row.getString("keyspace_name")); } if (resultMap.isEmpty()) { if (uuid != null) { resultMap.put("Exception", "Please make sure Aid is correct and application is onboarded."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } else { resultMap.put("Exception", "Application is not onboarded. Please make sure all the information is correct."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } } - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } @@ -236,9 +234,8 @@ public class RestMusicAdminAPI { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response deleteOnboardApp(JsonOnboard jsonObj) throws Exception { + ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); String appName = jsonObj.getAppname(); String aid = jsonObj.getAid(); PreparedQueryObject pQuery = new PreparedQueryObject(); @@ -247,7 +244,7 @@ public class RestMusicAdminAPI { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); resultMap.put("Exception", "Please make sure either appName(ns) or Aid is present"); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } if (aid != null) { pQuery.appendQueryString( @@ -275,10 +272,10 @@ public class RestMusicAdminAPI { "Oops. Something went wrong. Please make sure Aid is correct or Application is onboarded"); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } pQuery.appendQueryString( @@ -292,7 +289,7 @@ public class RestMusicAdminAPI { "Application not found. Please make sure Application exists."); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } else if (rows.size() == 1) { uuid = rows.get(0).getUUID("uuid").toString(); pQuery = new PreparedQueryObject(); @@ -314,13 +311,13 @@ public class RestMusicAdminAPI { UUID.fromString(uuid))); MusicCore.eventualPut(pQuery); resultMap.put("Success", "Your application " + appName + " has been deleted."); - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } else { resultMap.put("Failure", "More than one Aid exists for this application, so please provide Aid."); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MULTIPLERECORDS, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } } @@ -331,9 +328,8 @@ public class RestMusicAdminAPI { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response updateOnboardApp(JsonOnboard jsonObj) throws Exception { + ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); String aid = jsonObj.getAid(); String appName = jsonObj.getAppname(); String userId = jsonObj.getUserId(); @@ -346,7 +342,7 @@ public class RestMusicAdminAPI { resultMap.put("Exception", "Please make sure Aid is present"); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } if (appName == null && userId == null && password == null && isAAF == null) { @@ -354,7 +350,7 @@ public class RestMusicAdminAPI { "No parameters found to update. Please update atleast one parameter."); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } if (appName != null) { @@ -368,7 +364,7 @@ public class RestMusicAdminAPI { + " has already been onboarded. Please contact admin."); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.ALREADYEXIST, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } } @@ -404,10 +400,10 @@ public class RestMusicAdminAPI { "Oops. Something went wrong. Please make sure Aid is correct and application is onboarded"); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } Client client = Client.create(); @@ -418,6 +414,7 @@ public class RestMusicAdminAPI { @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response callbackOps(final JSONObject inputJsonObj) { + ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); Map<String, Object> resultMap = new HashMap<>(); new Thread(new Runnable() { public void run() { @@ -425,11 +422,11 @@ public class RestMusicAdminAPI { } }).start(); - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } private Response makeAsyncCall(JSONObject inputJsonObj) { - + ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); Map<String, Object> resultMap = new HashMap<>(); try { logger.info(EELFLoggerDelegate.applicationLogger, "Got notification: " + inputJsonObj.getData()); @@ -438,11 +435,10 @@ public class RestMusicAdminAPI { JSONCallbackResponse jsonResponse = mapper.readValue(dataStr, JSONCallbackResponse.class); String operation = jsonResponse.getOperation(); Map<String, String> changeValueMap = jsonResponse.getChangeValue(); - String primaryKey = jsonResponse.getPrimary_key(); - String ksTableName = jsonResponse.getFull_table(); //conductor.plans - if(ksTableName.equals("admin.notification_master")) { + String ksTableName = jsonResponse.getFull_table(); //conductor.plans + if("admin.notification_master".equals(ksTableName)) { CachingUtil.updateCallbackNotifyList(new ArrayList<String>()); - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } List<String> inputUpdateList = jsonResponse.getUpdateList(); @@ -465,7 +461,6 @@ public class RestMusicAdminAPI { } List<String> updateList = jsonResponse.getUpdateList(); - //logger.info("update list from trigger: "+updateList); for(String element : updateList) { if(notifiyList.contains(element)) { logger.info("Found the notifyOn property: "+element); @@ -509,38 +504,32 @@ public class RestMusicAdminAPI { "Oops. Something went wrong. Please make sure Callback properties are onboarded."); logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } logger.info(EELFLoggerDelegate.applicationLogger, "Going through list: "+operation+ " && List: "+jsonResponse.getUpdateList()); String key = "admin" + "." + "notification_master" + "." + baseRequestObj.getUuid(); String lockId = MusicCore.createLockReference(key); ReturnType lockAcqResult = MusicCore.acquireLock(key, lockId); - if(! lockAcqResult.getResult().toString().equals("SUCCESS")) { + if(!"SUCCESS".equals(lockAcqResult.getResult().toString())) { logger.error(EELFLoggerDelegate.errorLogger, "Some other node is notifying the caller..: "); } logger.info(EELFLoggerDelegate.applicationLogger, operation+ ": Operation :: changeValue: "+changeValueMap); - if(operation.equals("update")) { + if("update".equals(operation)) { String notifyWhenChangeIn = baseRequestObj.getNotifyWhenChangeIn(); // conductor.plans.status - if(null!=field_value) { - if(field_value.equals(notifyWhenChangeIn)) { + if(null!=field_value && field_value.equals(notifyWhenChangeIn)) { notifyCallBackAppl(jsonResponse, baseRequestObj); - } } - } else if(operation.equals("delete")) { + } else if("delete".equals(operation)) { String notifyWhenDeletesIn = baseRequestObj.getNotifyWhenDeletesIn(); // conductor.plans.status - if(null!=field_value) { - if(field_value.equals(notifyWhenDeletesIn)) { + if(null!=field_value && field_value.equals(notifyWhenDeletesIn)) { notifyCallBackAppl(jsonResponse, baseRequestObj); - } } - } else if(operation.equals("insert")) { + } else if("insert".equals(operation)) { String notifyWhenInsertsIn = baseRequestObj.getNotifyWhenInsertsIn(); // conductor.plans.status - if(null!=field_value) { - if(field_value.equals(notifyWhenInsertsIn)) { + if(null!=field_value && field_value.equals(notifyWhenInsertsIn)) { notifyCallBackAppl(jsonResponse, baseRequestObj); - } } } MusicCore.releaseLock(lockId, true); @@ -549,7 +538,7 @@ public class RestMusicAdminAPI { logger.error(EELFLoggerDelegate.errorLogger, "Exception while notifying...."+e.getMessage()); } logger.info(EELFLoggerDelegate.applicationLogger, "callback is completed. Notification was sent from Music..."); - return Response.status(Status.OK).entity(resultMap).build(); + return response.status(Status.OK).entity(resultMap).build(); } private void notifyCallBackAppl(JSONCallbackResponse jsonResponse, JsonCallback baseRequestObj) throws Exception { @@ -564,7 +553,7 @@ public class RestMusicAdminAPI { logger.info(EELFLoggerDelegate.applicationLogger, "Notification Response sending is: "+jsonNotification); logger.info("Notification Response sending is: "+jsonNotification); jsonNotification.setPassword(baseRequestObj.getApplicationPassword()); - WebResource webResource = client.resource(endpoint); + client.resource(endpoint); String authData = username+":"+password; byte[] plainCredsBytes = authData.getBytes(); byte[] base64CredsBytes = Base64.encode(plainCredsBytes); @@ -588,7 +577,7 @@ public class RestMusicAdminAPI { sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { - ; + logger.error(e.getMessage(), e); } try { @@ -663,47 +652,6 @@ public class RestMusicAdminAPI { logger.error("Exception while reading response from Caller"); logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond.."); } - - /*ClientResponse response = null; - try { - response = webResource.header("Authorization", "Basic " + base64Creds).accept("application/json").type("application/json") - .post(ClientResponse.class, jsonNotification); - } catch (com.sun.jersey.api.client.ClientHandlerException chf) { - boolean ok = false; - logger.info(EELFLoggerDelegate.applicationLogger, "Is Service down?"); - long now= System.currentTimeMillis(); - long end = now+notifytimeout; - while(! ok) { - logger.info(EELFLoggerDelegate.applicationLogger, "retrying since error in notifying callback.."); - try { - response = webResource.header("Authorization", "Basic " + base64Creds).accept("application/json").type("application/json") - .post(ClientResponse.class, jsonNotification); - if(response.getStatus() == 200) ok = true; - }catch (Exception e) { - logger.info(EELFLoggerDelegate.applicationLogger, "Retry until "+(end-System.currentTimeMillis())); - if(response == null && System.currentTimeMillis() < end) ok = false; - else ok = true; - try{ Thread.sleep(notifyinterval); } catch(Exception e1) {} - } - } - } - if(response == null) { - logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond.."); - return; - } - JsonNotifyClientResponse responseStr = response.getEntity(JsonNotifyClientResponse.class); - logger.info(EELFLoggerDelegate.applicationLogger, "Response from Notified client: "+responseStr); - - if(response.getStatus() != 200){ - long now= System.currentTimeMillis(); - long end = now+30000; - while(response.getStatus() != 200 && System.currentTimeMillis() < end) { - logger.info(EELFLoggerDelegate.applicationLogger, "retrying since error in notifying callback.."); - response = webResource.header("Authorization", "Basic " + base64Creds).accept("application/json").type("application/json") - .post(ClientResponse.class, jsonNotification); - } - logger.info(EELFLoggerDelegate.applicationLogger, "Exception while notifying.. "+response.getStatus()); - }*/ } private JsonNotification constructJsonNotification(JSONCallbackResponse jsonResponse, JsonCallback baseRequestObj) { @@ -777,6 +725,7 @@ public class RestMusicAdminAPI { } jsonNotification.setResponse_body(newMap); } catch(Exception e) { + logger.error(e.getMessage(), e); e.printStackTrace(); } return jsonNotification; @@ -792,8 +741,7 @@ public class RestMusicAdminAPI { "select id, endpoint_userid, endpoint_password, notify_to_endpoint, notify_insert_on," + " notify_delete_on, notify_update_on, request, notifyon from admin.notification_master allow filtering"; pQuery.appendQueryString(cql); - //pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), fullTable)); - + ResultSet rs = MusicCore.get(pQuery); Iterator<Row> it = rs.iterator(); while (it.hasNext()) { @@ -857,12 +805,6 @@ public class RestMusicAdminAPI { if(triggerName == null || triggerName.length() == 0) triggerName = "MusicTrigger"; - /*JsonCallback callBackCache = CachingUtil.getCallBackCache(notify_field); - if(callBackCache != null) { - resultMap.put("Exception", "The notification property has already been onboarded."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - }*/ - String[] allFields = notify_field.split(":"); String inserts = null; String updates = null; @@ -908,17 +850,17 @@ public class RestMusicAdminAPI { CachingUtil.updateCallBackCache(notify_field, jsonCallback); pQuery = new PreparedQueryObject(); pQuery.appendQueryString(cql); - ResultType nonKeyRelatedPut = MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL); + MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL); logger.info(EELFLoggerDelegate.applicationLogger, "Created trigger"); - //callBackCache.put(jsonCallback.getApplicationName(), jsonMap); } catch (InvalidQueryException e) { logger.error(EELFLoggerDelegate.errorLogger,"Exception callback_api table not configured."+e.getMessage()); resultMap.put("Exception", "Please make sure admin.notification_master table is configured."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } catch(Exception e) { + logger.error(e.getMessage(), e); e.printStackTrace(); resultMap.put("Exception", "Exception Occured."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Callback api successfully registered").toMap()).build(); } @@ -928,7 +870,6 @@ public class RestMusicAdminAPI { @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response deleteCallbackProp(JsonNotification jsonNotification) { - Map<String, Object> resultMap = new HashMap<>(); ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); String notifyOn = jsonNotification.getNotify_field(); @@ -943,13 +884,4 @@ public class RestMusicAdminAPI { } return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Callback api successfully deleted").toMap()).build(); } - - /*public String encodePwd(String password) { - return Base64.getEncoder().encodeToString(password.getBytes()); - } - - public String decodePwd(String password) { - byte[] bytes = Base64.getDecoder().decode(password); - return new String(bytes); - }*/ } diff --git a/src/test/java/org/onap/music/exceptions/MusicExceptionMapperTest.java b/src/test/java/org/onap/music/exceptions/MusicExceptionMapperTest.java new file mode 100644 index 00000000..bbfa9832 --- /dev/null +++ b/src/test/java/org/onap/music/exceptions/MusicExceptionMapperTest.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2019 IBM Intellectual Property + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================= + * ==================================================================== + */ + +package org.onap.music.exceptions; + +import org.codehaus.jackson.map.exc.UnrecognizedPropertyException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.ws.rs.core.Response; +import java.io.EOFException; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(PowerMockRunner.class) +public class MusicExceptionMapperTest { + + @Test + public void testToResponse() { + MusicExceptionMapper musicExceptionMapper = new MusicExceptionMapper(); + UnrecognizedPropertyException unrecognizedPropertyException = PowerMockito.mock(UnrecognizedPropertyException.class); + Response response = musicExceptionMapper.toResponse(unrecognizedPropertyException); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertTrue(((Map)response.getEntity()).get("error").toString().startsWith("Unknown field :")); + + EOFException eofException = PowerMockito.mock(EOFException.class); + response = musicExceptionMapper.toResponse(eofException); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertTrue(((Map)response.getEntity()).get("error").toString().equals("Request body cannot be empty".trim())); + + IllegalArgumentException illegalArgumentException = PowerMockito.mock(IllegalArgumentException.class); + PowerMockito.when(illegalArgumentException.getMessage()).thenReturn("ERROR MSG"); + response = musicExceptionMapper.toResponse(illegalArgumentException); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertTrue(((Map)response.getEntity()).get("error").toString().equals("ERROR MSG".trim())); + } +}
\ No newline at end of file |