diff options
author | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2018-11-06 09:33:48 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-11-06 09:33:48 +0000 |
commit | 9daaecd1a2450fe2bae247ae320b4e7c6d7ceee5 (patch) | |
tree | 71f9de5d0d6240c0beb1f769671e30adf26f15f6 /vid-app-common/src/main/java | |
parent | 5a7cdb624c0abe6f2b7e80bcbbaa740c4ee35e39 (diff) | |
parent | 56f2ad282e7a8d316b92878ae151767fea847265 (diff) |
Merge "HealthCheckController/test refactor"
Diffstat (limited to 'vid-app-common/src/main/java')
4 files changed, 132 insertions, 206 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; + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/dao/ConnectionFactory.java b/vid-app-common/src/main/java/org/onap/vid/dao/ConnectionFactory.java new file mode 100644 index 000000000..bd7a67b5a --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/dao/ConnectionFactory.java @@ -0,0 +1,20 @@ +package org.onap.vid.dao; + +import org.springframework.stereotype.Component; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import static com.google.common.base.Preconditions.checkNotNull; + +@Component +public class ConnectionFactory { + + public Connection getConnection(String url, String username, String password) throws SQLException { + checkNotNull(url); + checkNotNull(username); + checkNotNull(password); + return DriverManager.getConnection(url, username, password); + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/dao/FnAppDoaImpl.java b/vid-app-common/src/main/java/org/onap/vid/dao/FnAppDoaImpl.java index 65fc5217f..e3fdc95f2 100644 --- a/vid-app-common/src/main/java/org/onap/vid/dao/FnAppDoaImpl.java +++ b/vid-app-common/src/main/java/org/onap/vid/dao/FnAppDoaImpl.java @@ -7,9 +7,9 @@ * 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. @@ -21,94 +21,38 @@ package org.onap.vid.dao; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; import java.sql.*; - +@Repository public class FnAppDoaImpl { - /** The logger. */ - static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnAppDoaImpl.class); - - public int getProfileCount(String driver, String URL, String username, String password) { - Connection dbc = null; - PreparedStatement pst = null; - ResultSet rs = null; - String q = null; - int count = 0; - try { - dbc = getConnection(URL,username,password); - logger.debug(EELFLoggerDelegate.debugLogger, "getConnection:::"+ dbc); - q = "select count(*) from fn_app"; - pst = dbc.prepareStatement(q); - rs = pst.executeQuery(); - - if (rs.next()) - count = rs.getInt(1); - } catch(Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); - } finally { - cleanup(rs,pst,dbc); - } - logger.debug(EELFLoggerDelegate.debugLogger, "count:::"+ count); - return count; - } - - public static Connection getConnection(String url, String username, String password) throws SQLException { - java.sql.Connection con=null; - - if( url!=null && username!=null && password!=null ){ - con = DriverManager.getConnection(url, username, password); - } - - logger.info("Connection Successful"); - - return con; - - } - - public static void cleanup(ResultSet rs, PreparedStatement st, Connection c) { - if (rs != null) { - closeResultSet(rs); - } - if (st != null) { - closePreparedStatement(st); - } - if (c != null) { - rollbackAndCloseConnection(c); - } - } - - private static void rollbackAndCloseConnection(Connection c) { - try { - c.rollback(); - } catch (Exception e) { - if (logger != null) - logger.error("Error when trying to rollback connection", e); + static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnAppDoaImpl.class); + + private ConnectionFactory connectionFactory; + + @Autowired + public FnAppDoaImpl(ConnectionFactory connectionFactory) { + this.connectionFactory = connectionFactory; + } + + public int getProfileCount(String URL, String username, String password) throws SQLException { + String q = "select count(*) from fn_app"; + int count = 0; + try (Connection dbc = connectionFactory.getConnection(URL, username, password); + PreparedStatement pst = dbc.prepareStatement(q); ResultSet rs = pst.executeQuery()) { + logger.debug(EELFLoggerDelegate.debugLogger, "getConnection:::", dbc); + if (rs.next()) { + count = rs.getInt(1); + } + } catch (SQLException ex) { + logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); + throw ex; } - try { - c.close(); - } catch (Exception e) { - if (logger != null) - logger.error("Error when trying to close connection", e); - } - } - - private static void closePreparedStatement(PreparedStatement st) { - try { - st.close(); - } catch (Exception e) { - if (logger != null) - logger.error("Error when trying to close statement", e); - } - } - private static void closeResultSet(ResultSet rs) { - try { - rs.close(); - } catch (Exception e) { - if (logger != null) - logger.error("Error when trying to close result set", e); - } - } + logger.debug(EELFLoggerDelegate.debugLogger, "count:::", count); + return count; + } } |