Using alertover to send notification message for response failure

When I do interface automation, because the server instability causes possible use case failure, but this failure representation is only empty when acquiring the json object of the responding entity, which may cause trouble when checking the problem later. So I add a notification of the failure of acquiring the response, even if I know the server. Abnormal. The temporary use of free alertover, used for a long time, simple and reliable is its advantage, the follow-up will add a Wechat reminder. Share the code for your reference.

Here are the methods for obtaining json objects of response entities (ignoring some encapsulation methods):

    /**
     * Getting response entities
     * <p>Cookies are automatically set, but each project needs to implement cookie management on its own.</p>
     * <p>This method only deals with text information. For file processing, two expired methods can be invoked to solve the problem.</p>
     *
     * @param request Request object
     * @return Returns an object of type json
     */
    public static JSONObject getHttpResponse(HttpRequestBase request) {
        if (!isRightRequest(request)) return new JSONObject();
        beforeRequest(request);
        JSONObject res = new JSONObject();
        RequestInfo requestInfo = new RequestInfo(request);
        if (HEADER_KEY) output("===========request header===========", Arrays.asList(request.getAllHeaders()));
        long start = Time.getTimeStamp();
        try (CloseableHttpResponse response = ClientManage.httpsClient.execute(request)) {
            long end = Time.getTimeStamp();
            long elapsed_time = end - start;
            if (HEADER_KEY) output("===========response header===========", Arrays.asList(response.getAllHeaders()));
            int status = getStatus(response, res);
            JSONObject setCookies = afterResponse(response);
            String content = getContent(response);
            int data_size = content.length();
            res.putAll(getJsonResponse(content, setCookies));
            int code = iBase == null ? -2 : iBase.checkCode(res, requestInfo);
            if (!iBase.isRight(res))
                new AlertOver("Response status code error:" + status, "State code error:" + status, requestInfo.getUrl(), requestInfo).sendSystemMessage();
            MySqlTest.saveApiTestDate(requestInfo, data_size, elapsed_time, status, getMark(), code, LOCAL_IP, COMPUTER_USER_NAME);
        } catch (Exception e) {
            logger.warn("The acquisition request failed accordingly!", e);
            if (!SysInit.isBlack(requestInfo.getHost()))
                new AlertOver("Interface request failed", requestInfo.toString(), requestInfo.getUrl(), requestInfo).sendSystemMessage();
        } finally {
            HEADER_KEY = false;
            if (!SysInit.isBlack(requestInfo.getHost())) {
                if (requests.size() > 9) requests.removeFirst();
                boolean add = requests.add(request);
            }
        }
        return res;
    }

The following is the code for the alertover class, which is relatively simple:

package com.fun.utils.message;

import com.fun.frame.httpclient.FanLibrary;
import com.fun.base.bean.RequestInfo;
import com.fun.base.interfaces.IMessage;
import com.fun.db.mysql.MySqlTest;
import com.fun.config.SysInit;
import net.sf.json.JSONObject;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AlertOver extends FanLibrary implements IMessage {

    private static Logger logger = LoggerFactory.getLogger(AlertOver.class);

    String title;

    String content;

    String murl;

    RequestInfo requestInfo;

    private static String system = "s-7e93ec02-1308-480c-bc11-a7260c14";//Systematic anomaly

    private static String function = "s-7e3b7ea5-b4b0-4479-a0e3-bce6c830";//Dysfunction

    private static String business = "s-466a191a-cbb8-4164-b8be-9779bb88";//Business exceptions

    private static String remind = "s-f49ac5bc-008b-4b11-890e-6715ef89";//Reminder push

    private static String code = "s-490d0fc6-35cc-4430-9f87-09cdeb05";//Program exception

    private static final String testGroup = "g-4eefc0ad-19af-4b1c-9d0b-ef87be15";

    public AlertOver() {
        this("test title", "test content!");
    }

    public AlertOver(String title, String content) {
        this.title = title;
        this.content = content + LINE + "Sending source:" + COMPUTER_USER_NAME;
    }

    public AlertOver(String title, String content, String url) {
        this(title, content);
        this.murl = url;
    }

    public AlertOver(String title, String content, String url, RequestInfo requestInfo) {
        this(title, content);
        this.murl = url;
        this.requestInfo = requestInfo;
    }

    /**
     * Sending system exception
     */
    public void sendSystemMessage() {
        if (SysInit.isBlack(murl)) return;
        sendMessage(system);
        MySqlTest.saveAlertOverMessage(requestInfo, "system", title, LOCAL_IP, COMPUTER_USER_NAME);
        logger.info("Send system error alerts, title: {},ip: {},computer: {}", title, LOCAL_IP, COMPUTER_USER_NAME);
    }

    /**
     * Sending dysfunction
     */
    public void sendFunctionMessage() {
        sendMessage(function);
    }

    /**
     * Send business exception
     */
    public void sendBusinessMessage() {
        sendMessage(business);
    }

    /**
     * Sender exception
     */
    public void sendCodeMessage() {
        sendMessage(code);
    }

    /**
     * Reminder push
     */
    public void sendRemindMessage() {
        sendMessage(remind);
    }

    /**
     * send message
     *
     * @return
     */
    public void sendMessage(String source) {
        if (SysInit.isBlack(murl)) return;
        String url = "https://api.alertover.com/v1/alert";
        String receiver = testGroup;//Test group ID
        JSONObject jsonObject = new JSONObject();// New json array
        jsonObject.put("frame", source);// Add send source id
        jsonObject.put("receiver", receiver);// Add receive group id
        jsonObject.put("content", content);// send content
        jsonObject.put("title", title);// Send title
        jsonObject.put("url", murl);// Send title
        jsonObject.put("sound", "pianobar");// Send voice
        logger.debug("Information details:{}", jsonObject.toString());
        HttpPost httpPost = getHttpPost(url, jsonObject);
        //Cancel sending
        getHttpResponse(httpPost);
    }
}

Selection of Technical Articles

  1. One line of java code prints a heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Scheme Sharing
  4. Performance testing framework
  5. How to Enjoy Performance Testing on Linux Command Line Interface
  6. Graphic HTTP Brain Map
  7. How to Test Probabilistic Business Interface
  8. httpclient handles multi-user simultaneous online
  9. Automatically convert swagger documents into test code
  10. Five lines of code to build static blogs
  11. How httpclient handles 302 redirection
  12. A preliminary study on the testing framework of linear interface based on java

Selection of non-technical articles

  1. Why choose software testing as a career path?
  2. Ten Steps to Become a Great Java Developer
  3. Writing to everyone about programming thinking
  4. Obstacles to automated testing
  5. The Problems of Automated Testing
  6. Tested "Code Immortality" Brain Map

Click on the Public Number Map

Tags: Programming JSON Java Linux MySQL

Posted on Mon, 09 Sep 2019 00:26:42 -0700 by Skippy93