Looks like an integration test
REST
Because to design interfaces, you must first understand REST related knowledge.
REST refers to the presentation layer state transition, and here refers to the presentation layer of resources.
Resources are all types of files on the server, including pictures, videos, tables, etc.
The presentation layer of resources is the presentation form of these resources, such as Json, html, jpg, etc.
REST describes the communication between the client and the server as the form that the client needs to obtain the resources on the server through different Http methods.
A resource is described by a URL, which is the address where the resource exists.
GET is used to obtain resources, POST is used to create or update, PUT is used to update, and DELETE is used to DELETE.
The URL uses only nouns, the action is described by Http method, and the result is Http status code.
This style of interface is called RESTful interface.
Http request for Java
The implementation is very simple. Because it is a database function, it can not be made asynchronous. Some other third-party encapsulation is not necessary. It is directly implemented in the native way of Java.
The code is as follows:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpClient { public static String getJsonByID(int id) { String url = "http://hn216.api.yesapi.cn/"; String param = "s=App.Table.Get&return_data=0&model_name=user_key&id=" + id + "&app_key=F2B5FDFB4FC01963EFF6AA9C131B394A&sign=127F18768AC425CE58487292BFDBE6B8"; String result = HttpClient.doPost(url, param); return result; } private static String doPost(String httpUrl, String param) { HttpURLConnection connection = null; InputStream is = null; OutputStream os = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpUrl); // Open connection via remote url connection object connection = (HttpURLConnection) url.openConnection(); // Set connection request mode connection.setRequestMethod("POST"); // Set timeout for connecting to host server: 15000 MS connection.setConnectTimeout(15000); // Set the timeout for reading the data returned by the host server: 60000 milliseconds connection.setReadTimeout(60000); // The default value is: false. It needs to be set to true when transmitting / writing data to the remote server connection.setDoOutput(true); // The default value is true. It is set to true when reading data from the remote service. This parameter is optional connection.setDoInput(true); // Format the incoming parameters: the request parameters should be in the form of name1 = value1 & Name2 = Value2. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Setting authentication information: Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); // Get an output stream through the connection object os = connection.getOutputStream(); // The parameters are written out / transmitted through the output stream object, which is written out through the byte array os.write(param.getBytes()); // Get an input stream through the connection object and read it to the remote if (connection.getResponseCode() == 200) { is = connection.getInputStream(); // Wrap the input stream object: charset is set according to the requirements of the work project team br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer(); String temp = null; // Loop through line by line to read data while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // close resource if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } // Disconnect from remote address url connection.disconnect(); } return result; } }
According to the REST design, the get method should be used here, but because the key is transmitted, it will be safer not to put the information on the URL with post?
Jason analysis
Json is a string written in a special format, which belongs to the data organization structure commonly used in the industry.
There are many mature third-party frameworks. Google's GSON is used here.
The parsing code is as follows:
public class Analysis { public static String analysis(String result){ JsonParser parser = new JsonParser(); JsonObject jsonObject = parser.parse(result).getAsJsonObject(); JsonObject data = jsonObject.get("data").getAsJsonObject(); JsonObject data1 = data.get("data").getAsJsonObject(); String keyWord = data1.get("keyWord").getAsString(); return keyWord; } }
Because the interface provided by other websites is used, the structure will look more complex.
Pile function and simulation interface
The progress of other groups is slightly slow, which is used to replace the functions that need to call the interface. The term of software testing is pile function.
Part of the encryption algorithm:
public static String entry(String original, String key) { return key + ": " + original; } public static String decry(String cipher, String key) { return cipher.replaceAll(key + ": ", ""); }
As in the actual situation, the parameters we need to provide for encrypting data are original data and key, and the return value is ciphertext.
When decrypting data, we need to provide ciphertext and key, and the return value is the original data.
The interface for obtaining the key comes from a key website and can return the key according to the given user ID.
Because the workload of authority management is too large, it should wait until this part is completed.
test
Combine the above parts and package them into jar s, import them into the database using the previous method, and create external functions.
The test in the database is exactly as expected, in other words, it passes test x
good.