diff options
author | Joanna Jeremicz <joanna.jeremicz@nokia.com> | 2018-09-19 14:35:00 +0200 |
---|---|---|
committer | Joanna Jeremicz <joanna.jeremicz@nokia.com> | 2018-09-28 07:39:18 +0200 |
commit | 56f2ad282e7a8d316b92878ae151767fea847265 (patch) | |
tree | 6fff9ecd3b4c197ff37bd0f96d430edd75d93adf /vid-app-common/src/main/java/org/onap/vid/controllers | |
parent | 52cb6586c32cb99b1aab99bebfa51b01d98beba4 (diff) |
HealthCheckController/test refactor
Issue-ID: VID-312
Change-Id: Iabdb91c9b4940ff7a173656819ad5fa807a734e9
Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/controllers')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/controllers/HealthCheckController.java | 173 | ||||
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/controllers/HealthStatus.java | 31 |
2 files changed, 83 insertions, 121 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/controllers/HealthCheckController.java b/vid-app-common/src/main/java/org/onap/vid/controllers/HealthCheckController.java index 12cc68e61..86e832ba7 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controllers/HealthCheckController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controllers/HealthCheckController.java @@ -25,7 +25,7 @@ import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.dao.FnAppDoaImpl; import org.onap.vid.model.GitRepositoryState; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -38,6 +38,9 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.springframework.http.HttpStatus.OK; + /** * Controller for user profile view. The view is restricted to authenticated * users. The view name resolves to page user_profile.jsp which uses Angular. @@ -47,158 +50,86 @@ import java.util.Properties; @RequestMapping("/") public class HealthCheckController extends UnRestrictedBaseController { - /** - * The logger. - */ private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(HealthCheckController.class); - - /** - * The Constant dateFormat. - */ - final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS"); - - private static final String HEALTH_CHECK_PATH = "/healthCheck"; + private static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS"); private static final String GIT_PROPERTIES_FILENAME = "git.properties"; + private FnAppDoaImpl fnAppDoaImpl; - /** - * Model for JSON response with health-check results. - */ - public class HealthStatus { - // Either 200 or 500 - public int statusCode; - - // Additional detail in case of error, empty in case of success. - public String message; - - public String date; - - public HealthStatus(int code, String msg) { - this.statusCode = code; - this.message = msg; - } - - public HealthStatus(int code, String date, String msg) { - this.statusCode = code; - this.message = msg; - this.date = date; - } - - public int getStatusCode() { - return statusCode; - } - - public void setStatusCode(int code) { - this.statusCode = code; - } - - public String getMessage() { - return message; - } - - public void setMessage(String msg) { - this.message = msg; - } - - public String getDate() { - return date; - } - - public void setDate(String date) { - this.date = date; - } - + @Autowired + public HealthCheckController(FnAppDoaImpl fnAppDoaImpl) { + this.fnAppDoaImpl = fnAppDoaImpl; } - @SuppressWarnings("unchecked") - public int getProfileCount(String driver, String URL, String username, String password) { - FnAppDoaImpl doa = new FnAppDoaImpl(); - int count = doa.getProfileCount(driver, URL, username, password); - return count; - } - - /** * Obtain the HealthCheck Status from the System.Properties file. * Used by IDNS for redundancy * * @return ResponseEntity The response entity - * @throws IOException Signals that an I/O exception has occurred. */ @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public HealthStatus gethealthCheckStatusforIDNS() { - - String driver = SystemProperties.getProperty("db.driver"); - String URL = SystemProperties.getProperty("db.connectionURL"); - String username = SystemProperties.getProperty("db.userName"); - String password = SystemProperties.getProperty("db.password"); - - LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "password::" + password); - - - HealthStatus healthStatus = null; - try { - LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); - int count = getProfileCount(driver, URL, username, password); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count); - healthStatus = new HealthStatus(200, "health check succeeded"); - } catch (Exception ex) { - - LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); - healthStatus = new HealthStatus(500, "health check failed: " + ex.toString()); - } - return healthStatus; + public HealthStatus getHealthCheckStatusForIDNS() { + return createCorrespondingStatus(); } /** * Obtain the HealthCheck Status from the System.Properties file. * * @return ResponseEntity The response entity - * @throws IOException Signals that an I/O exception has occurred. - * Project : */ @RequestMapping(value = "rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public HealthStatus getHealthCheck( @PathVariable("User-Agent") String UserAgent, @PathVariable("X-ECOMP-RequestID") String ECOMPRequestID) { + LOGGER.debug(EELFLoggerDelegate.debugLogger, "User-Agent ", UserAgent); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "X-ECOMP-RequestID ", ECOMPRequestID); + return createCorrespondingStatus(); + } - String driver = SystemProperties.getProperty("db.driver"); - String URL = SystemProperties.getProperty("db.connectionURL"); - String username = SystemProperties.getProperty("db.userName"); - String password = SystemProperties.getProperty("db.password"); - - LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "password::" + password); - + @RequestMapping(value = "/commitInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public GitRepositoryState getCommitInfo() throws IOException { + Properties properties = new Properties(); + properties.load(getClass().getClassLoader().getResourceAsStream(GIT_PROPERTIES_FILENAME)); + return new GitRepositoryState(properties); + } - HealthStatus healthStatus = null; + private HealthStatus createCorrespondingStatus() { + logData(); try { - LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "User-Agent" + UserAgent); - LOGGER.debug(EELFLoggerDelegate.debugLogger, "X-ECOMP-RequestID" + ECOMPRequestID); + int count = fnAppDoaImpl.getProfileCount(getUrl(), getUsername(), getPassword()); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::", count); + return okStatus(); + } catch (Exception ex) { + String errorMsg = ex.getMessage(); + LOGGER.error(EELFLoggerDelegate.errorLogger, errorMsg); + return errorStatus(errorMsg); + } + } + private void logData() { + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::", getUrl()); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::", getUsername()); + } - int count = getProfileCount(driver, URL, username, password); + private HealthStatus okStatus() { + return new HealthStatus(OK, dateFormat.format(new Date()), "health check succeeded"); + } - LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count); - healthStatus = new HealthStatus(200, dateFormat.format(new Date()), "health check succeeded"); - } catch (Exception ex) { + private HealthStatus errorStatus(String msg) { + return new HealthStatus(INTERNAL_SERVER_ERROR, dateFormat.format( + new Date()), "health check failed: " + msg); + } - LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); - healthStatus = new HealthStatus(500, dateFormat.format(new Date()), "health check failed: " + ex.toString()); - } - return healthStatus; + private String getUrl() { + return SystemProperties.getProperty("db.connectionURL"); } - @RequestMapping(value = "/commitInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public GitRepositoryState getCommitInfo() throws IOException { - Properties properties = new Properties(); - properties.load(getClass().getClassLoader().getResourceAsStream(GIT_PROPERTIES_FILENAME)); - return new GitRepositoryState(properties); + private String getUsername() { + return SystemProperties.getProperty("db.userName"); + } + + private String getPassword() { + return SystemProperties.getProperty("db.password"); } } diff --git a/vid-app-common/src/main/java/org/onap/vid/controllers/HealthStatus.java b/vid-app-common/src/main/java/org/onap/vid/controllers/HealthStatus.java new file mode 100644 index 000000000..6056c2898 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/controllers/HealthStatus.java @@ -0,0 +1,31 @@ +package org.onap.vid.controllers; + +import org.springframework.http.HttpStatus; + +/** + * Model for JSON response with health-check results. + */ +public final class HealthStatus { + + private final int statusCode; + private final String detailedMsg; + private final String date; + + public HealthStatus(HttpStatus code, String date, String detailedMsg) { + this.statusCode = code.value(); + this.detailedMsg = detailedMsg; + this.date = date; + } + + public int getStatusCode() { + return statusCode; + } + + public String getDetailedMsg() { + return detailedMsg; + } + + public String getDate() { + return date; + } +} |