SHARE
TWEET

Untitled

a guest Jun 16th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import nz.ac.vuw.swen301.assignment3.server.LogEvent;
  3. import nz.ac.vuw.swen301.assignment3.server.LogServlet;
  4. import nz.ac.vuw.swen301.assignment3.server.StatsServlet;
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import org.junit.Test;
  8. import org.springframework.mock.web.MockHttpServletRequest;
  9. import org.springframework.mock.web.MockHttpServletResponse;
  10.  
  11. import javax.servlet.ServletException;
  12. import java.io.IOException;
  13. import java.time.Instant;
  14. import java.util.Arrays;
  15. import java.util.Set;
  16. import java.util.stream.Collectors;
  17.  
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertTrue;
  20.  
  21. public class whiteBoxTests {
  22.     /*
  23.        test doPost log path is null
  24.     */
  25.     @Test
  26.     public void testLogPathNull() throws IOException, ServletException {
  27.         MockHttpServletRequest request = new MockHttpServletRequest();
  28.         MockHttpServletResponse response=new MockHttpServletResponse();
  29.         LogServlet logServlet=new LogServlet();
  30.         logServlet.doPost(request,response);
  31.         assertEquals(400,response.getStatus());
  32.         assertTrue(response.getContentAsString().equals("log path null"));
  33.         assertTrue(response.getContentType().startsWith("application/json"));
  34.     }
  35.     /*
  36.        test doPost ,log path is addlog and no data add
  37.     */
  38.     @Test
  39.     public void testNoDataAdd() throws IOException, ServletException {
  40.         MockHttpServletRequest request = new MockHttpServletRequest();
  41.         MockHttpServletResponse response=new MockHttpServletResponse();
  42.         request.setPathInfo("/addlog");
  43.         LogServlet logServlet=new LogServlet();
  44.         logServlet.doPost(request,response);
  45.         assertEquals(400,response.getStatus());
  46.         assertTrue(response.getContentAsString().equals("invalid input, object invalid"));
  47.         assertTrue(response.getContentType().startsWith("application/json"));
  48.     }
  49.     /*
  50.        test doPost, log path is addlog and invaild input
  51.     */
  52.     @Test
  53.     public void testInvaildInputForDoPost() throws IOException, ServletException {
  54.         MockHttpServletRequest request = new MockHttpServletRequest();
  55.         MockHttpServletResponse response=new MockHttpServletResponse();
  56.         request.setPathInfo("/addlog");
  57.         LogServlet logServlet=new LogServlet();
  58.         String log="aas";
  59.         request.setContent(log.getBytes());
  60.         logServlet.doPost(request,response);
  61.         assertEquals(400,response.getStatus());
  62.         assertTrue(response.getContentAsString().equals("invalid input, object invalid"));
  63.         assertTrue(response.getContentType().startsWith("application/json"));
  64.     }
  65.     /*
  66.        test doPost, log path is addlog and object invaild(set content as object string not a array string)
  67.     */
  68.     @Test
  69.     public void testDoPostAddObjectInvaild() throws IOException, ServletException {
  70.         MockHttpServletRequest request = new MockHttpServletRequest();
  71.         MockHttpServletResponse response=new MockHttpServletResponse();
  72.         request.setPathInfo("/addlog");
  73.         LogServlet logServlet=new LogServlet();
  74.         JSONObject object = new JSONObject();
  75.         object.put("id", "1");
  76.         object.put("message", "message");
  77.         object.put("timestamp", Instant.now().toString());
  78.         object.put("thread", "thread");
  79.         object.put("logger", "logger");
  80.         object.put("level", "debug");
  81.         object.put("errorDetails", "errorDetails");
  82.         String log=object.toString();
  83.         request.setContent(log.getBytes());
  84.         logServlet.doPost(request,response);
  85.         assertEquals(400,response.getStatus());
  86.         assertTrue(response.getContentAsString().equals("invalid input, object invalid"));
  87.         assertTrue(response.getContentType().startsWith("application/json"));
  88.     }
  89.     /*
  90.        test doPost, log path is addlog and object is correct
  91.     */
  92.     @Test
  93.     public void testDoPostAddObjectSucceed() throws IOException, ServletException {
  94.         MockHttpServletRequest request = new MockHttpServletRequest();
  95.         MockHttpServletResponse response=new MockHttpServletResponse();
  96.         request.setPathInfo("/addlog");
  97.         LogServlet logServlet=new LogServlet();
  98.         JSONObject object = new JSONObject();
  99.         object.put("id", "1123541456");
  100.         object.put("message", "message");
  101.         object.put("timestamp",Instant.now().toString());
  102.         object.put("thread", "thread");
  103.         object.put("logger", "logger");
  104.         object.put("level", "debug");
  105.         object.put("errorDetails", "errorDetails");
  106.         JSONArray jsonArray=new JSONArray();
  107.         jsonArray.put(object);
  108.         request.setContent(jsonArray.toString().getBytes());
  109.         logServlet.doPost(request,response);
  110.         assertEquals(201,response.getStatus());
  111.         assertTrue(jsonArray.toString().equals(response.getContentAsString()));
  112.         assertTrue(response.getContentType().startsWith("application/json"));
  113.     }
  114.     /*
  115.        test doPost, log path is addlog and the list has same id logs
  116.     */
  117.     @Test
  118.     public void testPostSameID() throws IOException, ServletException {
  119.         MockHttpServletRequest request = new MockHttpServletRequest();
  120.         MockHttpServletResponse response=new MockHttpServletResponse();
  121.         request.setPathInfo("/addlog");
  122.         LogServlet logServlet=new LogServlet();
  123.         JSONObject object = new JSONObject();
  124.         object.put("id", "13251");
  125.         object.put("message", "message");
  126.         object.put("timestamp",Instant.now().toString());
  127.         object.put("thread", "thread");
  128.         object.put("logger", "logger");
  129.         object.put("level", "debug");
  130.         object.put("errorDetails", "errorDetails");
  131.         JSONArray jsonArray=new JSONArray();
  132.         jsonArray.put(object);
  133.         jsonArray.put(object);
  134.         request.setContent(jsonArray.toString().getBytes());
  135.         logServlet.doPost(request,response);
  136.         assertEquals(409,response.getStatus());
  137.         assertTrue(response.getContentType().startsWith("application/json"));
  138.         assertTrue(response.getContentAsString().equals("a log event with this id aleady exists"));
  139.     }
  140.     /*
  141.          test doPost, log path is addlog and there is a log in the list which has same id in the server storage.
  142.     */
  143.     @Test
  144.     public void testPostSameIdWithServerStorage() throws IOException, ServletException {
  145.         MockHttpServletRequest request = new MockHttpServletRequest();
  146.         MockHttpServletResponse response=new MockHttpServletResponse();
  147.         request.setPathInfo("/addlog");
  148.         LogServlet logServlet=new LogServlet();
  149.         JSONObject object = new JSONObject();
  150.         object.put("id", "347581");
  151.         object.put("message", "message");
  152.         object.put("timestamp", Instant.now().toString());
  153.         object.put("thread", "thread");
  154.         object.put("logger", "logger");
  155.         object.put("level", "debug");
  156.         object.put("errorDetails", "errorDetails");
  157.         JSONArray jsonArray=new JSONArray();
  158.         jsonArray.put(object);
  159.         request.setContent(jsonArray.toString().getBytes());
  160.         logServlet.doPost(request,response);
  161.  
  162.         request = new MockHttpServletRequest();
  163.         response=new MockHttpServletResponse();
  164.         request.setPathInfo("/addlog");
  165.         request.setContent(jsonArray.toString().getBytes());
  166.         logServlet.doPost(request,response);
  167.         assertEquals(409,response.getStatus());
  168.         assertTrue(response.getContentAsString().equals("a log event with this id aleady exists"));
  169.         assertTrue(response.getContentType().startsWith("application/json"));
  170.     }
  171.     /*
  172.          test doPost, log path is addlog and there is a log in the list which has different id in the server storage.
  173.     */
  174.     @Test
  175.     public void testPostDifferentLogs() throws IOException, ServletException {
  176.         MockHttpServletRequest request = new MockHttpServletRequest();
  177.         MockHttpServletResponse response=new MockHttpServletResponse();
  178.         request.setPathInfo("/addlog");
  179.         LogServlet logServlet=new LogServlet();
  180.         JSONObject object = new JSONObject();
  181.         object.put("id", "1267");
  182.         object.put("message", "message");
  183.         object.put("timestamp", Instant.now().toString());
  184.         object.put("thread", "thread");
  185.         object.put("logger", "logger");
  186.         object.put("level", "debug");
  187.         object.put("errorDetails", "errorDetails");
  188.         JSONArray jsonArray=new JSONArray();
  189.         jsonArray.put(object);
  190.         request.setContent(jsonArray.toString().getBytes());
  191.         logServlet.doPost(request,response);
  192.  
  193.         request = new MockHttpServletRequest();
  194.         response=new MockHttpServletResponse();
  195.         object = new JSONObject();
  196.         request.setPathInfo("/addlog");
  197.         object.put("id", "2");
  198.         object.put("message", "message");
  199.         object.put("timestamp", Instant.now().toString());
  200.         object.put("thread", "thread");
  201.         object.put("logger", "logger");
  202.         object.put("level", "debug");
  203.         object.put("errorDetails", "errorDetails");
  204.         jsonArray=new JSONArray();
  205.         jsonArray.put(object);
  206.         request.setContent(jsonArray.toString().getBytes());
  207.         logServlet.doPost(request,response);
  208.         assertEquals(201,response.getStatus());
  209.         assertTrue(jsonArray.toString().equals(response.getContentAsString()));
  210.         assertTrue(response.getContentType().startsWith("application/json"));
  211.     }
  212.     /*
  213.          test doPost, log path is addlog and there is a log in the list which has different id in the server storage.using LogEvent
  214.     */
  215.     @Test
  216.     public void testPostDiffIdWithServerStorage() throws IOException, ServletException, InterruptedException {
  217.         MockHttpServletRequest request = new MockHttpServletRequest();
  218.         MockHttpServletResponse response=new MockHttpServletResponse();
  219.         request.setPathInfo("/addlog");
  220.         LogServlet logServlet=new LogServlet();
  221.         JSONObject object = new JSONObject();
  222.         object.put("id", "4");
  223.         object.put("message", "message");
  224.         object.put("timestamp", Instant.now().toString());
  225.         object.put("thread", "thread");
  226.         object.put("logger", "logger");
  227.         object.put("level", "debug");
  228.         object.put("errorDetails", "errorDetails");
  229.         LogEvent logEvent=new LogEvent(object);
  230.         JSONArray jsonArray=new JSONArray();
  231.         jsonArray.put(logEvent.convertToJson());
  232.         request.setContent(jsonArray.toString().getBytes());
  233.         logServlet.doPost(request,response);
  234.         assertEquals(201,response.getStatus());
  235.         assertTrue(jsonArray.toString().equals(response.getContentAsString()));
  236.         assertTrue(response.getContentType().startsWith("application/json"));
  237.     }
  238.     /*
  239.         test doPost and if the list items has same id.
  240.      */
  241.     @Test
  242.     public void testPostSameIDInTheList() throws IOException, ServletException {
  243.         MockHttpServletRequest request = new MockHttpServletRequest();
  244.         MockHttpServletResponse response=new MockHttpServletResponse();
  245.         request.setPathInfo("/addlog");
  246.         LogServlet logServlet=new LogServlet();
  247.         JSONObject object = new JSONObject();
  248.         object.put("id", "5");
  249.         object.put("message", "message");
  250.         object.put("timestamp", Instant.now().toString());
  251.         object.put("thread", "thread");
  252.         object.put("logger", "logger");
  253.         object.put("level", "debug");
  254.         object.put("errorDetails", "errorDetails");
  255.         JSONArray jsonArray=new JSONArray();
  256.         jsonArray.put(object);
  257.         request.setContent(jsonArray.toString().getBytes());
  258.         logServlet.doPost(request,response);
  259.  
  260.         request = new MockHttpServletRequest();
  261.         response=new MockHttpServletResponse();
  262.         object = new JSONObject();
  263.         request.setPathInfo("/addlog");
  264.         object.put("id", "6");
  265.         object.put("message", "message");
  266.         object.put("timestamp", Instant.now().toString());
  267.         object.put("thread", "thread");
  268.         object.put("logger", "logger");
  269.         object.put("level", "debug");
  270.         object.put("errorDetails", "errorDetails");
  271.         jsonArray=new JSONArray();
  272.         jsonArray.put(object);
  273.         jsonArray.put(object);
  274.         request.setContent(jsonArray.toString().getBytes());
  275.         logServlet.doPost(request,response);
  276.         assertEquals(409,response.getStatus());
  277.         assertTrue(response.getContentAsString().equals("a log event with this id aleady exists"));
  278.         assertTrue(response.getContentType().startsWith("application/json"));
  279.     }
  280.     /*
  281.         test doGet() serachlogs: no path infomation and go to path logs
  282.      */
  283.     @Test
  284.     public void testGetNoPathInformation() throws IOException,ServletException{
  285.         MockHttpServletRequest request = new MockHttpServletRequest();
  286.         MockHttpServletResponse response=new MockHttpServletResponse();
  287.         LogServlet logServlet=new LogServlet();
  288.         logServlet.doGet(request,response);
  289.         assertEquals(200,response.getStatus());
  290.         assertTrue(response.getContentAsString().equals("welcome to logs"));
  291.         assertTrue(response.getContentType().startsWith("application/json"));
  292.     }
  293.     /*
  294.         test doGet() serachlogs: go to path searchLogs and limit is not a int
  295.     */
  296.     @Test
  297.     public void testGetLimitIsNotAValidInt() throws IOException,ServletException{
  298.         MockHttpServletRequest request = new MockHttpServletRequest();
  299.         MockHttpServletResponse response=new MockHttpServletResponse();
  300.         request.setPathInfo("/searchLogs");
  301.         request.setParameter("limit","aaa");
  302.         LogServlet logServlet=new LogServlet();
  303.         logServlet.doGet(request,response);
  304.         assertEquals(400,response.getStatus());
  305.         assertTrue(response.getContentAsString().equals("limit is not a integer"));
  306.         assertTrue(response.getContentType().startsWith("application/json"));
  307.     }
  308.  
  309.     /*
  310.     test stats for coverage : go to path stats and get status code and get contentTYPE
  311. */
  312.     @Test
  313.     public void testGetStatStatusCodeAndContentType() throws IOException,ServletException{
  314.         MockHttpServletRequest request = new MockHttpServletRequest();
  315.         MockHttpServletResponse response=new MockHttpServletResponse();
  316.         StatsServlet statsServlet=new StatsServlet();
  317.         statsServlet.doGet(request,response);
  318.         assertEquals(200,response.getStatus());
  319.         assertTrue(response.getContentType().startsWith("application/vnd.ms-excel"));
  320.     }
  321.     /*
  322.         test doGet() serachlogs: go to path searchLogs and limit is less than 0
  323.     */
  324.     @Test
  325.     public void testGetLimitIsLess0() throws IOException,ServletException{
  326.         MockHttpServletRequest request = new MockHttpServletRequest();
  327.         MockHttpServletResponse response=new MockHttpServletResponse();
  328.         request.setPathInfo("/searchLogs");
  329.         request.setParameter("limit","-1");
  330.         LogServlet logServlet=new LogServlet();
  331.         logServlet.doGet(request,response);
  332.         assertEquals(400,response.getStatus());
  333.         assertTrue(response.getContentAsString().equals("limit is not between 0 and 50"));
  334.         assertTrue(response.getContentType().startsWith("application/json"));
  335.     }
  336.     /*
  337.          test doGet() serachlogs: go to path searchLogs and limit is more than 50
  338.     */
  339.     @Test
  340.     public void testGetLimitMoreThan50() throws IOException,ServletException{
  341.         MockHttpServletRequest request = new MockHttpServletRequest();
  342.         MockHttpServletResponse response=new MockHttpServletResponse();
  343.         request.setPathInfo("/searchLogs");
  344.         request.setParameter("limit","55");
  345.         LogServlet logServlet=new LogServlet();
  346.         logServlet.doGet(request,response);
  347.         assertEquals(400,response.getStatus());
  348.         assertTrue(response.getContentAsString().equals("limit is not between 0 and 50"));
  349.         assertTrue(response.getContentType().startsWith("application/json"));
  350.     }
  351.  
  352.     /*
  353.         test doGet() serachlogs: go to path searchLogs and limit is null and level is null, serch empty list
  354.     */
  355.     @Test
  356.     public void testGetEmptyList() throws IOException,ServletException{
  357.         MockHttpServletRequest request = new MockHttpServletRequest();
  358.         MockHttpServletResponse response=new MockHttpServletResponse();
  359.         request.setPathInfo("/searchLogs");
  360.         LogServlet logServlet=new LogServlet();
  361.         logServlet.doGet(request,response);
  362.         assertEquals(200,response.getStatus());
  363.         assertTrue(response.getContentType().startsWith("application/json"));
  364.     }
  365.     /*
  366.         test doGet() serachlogs: go to path searchLogs and limit is null and level is not vaild, serch empty list
  367.     */
  368.     @Test
  369.     public void testaGetLimitInvaild() throws IOException,ServletException{
  370.         MockHttpServletRequest request = new MockHttpServletRequest();
  371.         MockHttpServletResponse response=new MockHttpServletResponse();
  372.         request.setPathInfo("/searchLogs");
  373.         request.setParameter("level","sdafs");
  374.         LogServlet logServlet=new LogServlet();
  375.         logServlet.doGet(request,response);
  376.         assertEquals(400,response.getStatus());
  377.         assertTrue(response.getContentType().startsWith("application/json"));
  378.     }
  379.     /*
  380.         test when the level is ""
  381.      */
  382.     @Test
  383.     public void testGetLimitisNull() throws IOException,ServletException{
  384.         MockHttpServletRequest request = new MockHttpServletRequest();
  385.         MockHttpServletResponse response=new MockHttpServletResponse();
  386.         request.setPathInfo("/searchLogs");
  387.         request.setParameter("level","");
  388.         LogServlet logServlet=new LogServlet();
  389.         logServlet.doGet(request,response);
  390.         assertEquals(200,response.getStatus());
  391.         assertTrue(response.getContentType().startsWith("application/json"));
  392.     }
  393.     /*
  394.         test when the level is null and the server storage is not null
  395.      */
  396.     @Test
  397.     public void testGetStorageNotNUll() throws IOException,ServletException{
  398.         MockHttpServletRequest request = new MockHttpServletRequest();
  399.         MockHttpServletResponse response=new MockHttpServletResponse();
  400.         request.setPathInfo("/addlog");
  401.         LogServlet logServlet=new LogServlet();
  402.         JSONObject object = new JSONObject();
  403.         object.put("id", "123456");
  404.         object.put("message", "message");
  405.         object.put("timestamp", Instant.now().toString());
  406.         object.put("thread", "thread");
  407.         object.put("logger", "logger");
  408.         object.put("level", "trace");
  409.         object.put("errorDetails", "errorDetails");
  410.         JSONArray jsonArray=new JSONArray();
  411.         jsonArray.put(object);
  412.         request.setContent(jsonArray.toString().getBytes());
  413.         logServlet.doPost(request,response);
  414.         request = new MockHttpServletRequest();
  415.         response=new MockHttpServletResponse();
  416.         request.setPathInfo("/searchLogs");
  417.         request.setParameter("level","");
  418.         request.setParameter("limit","5");
  419.         logServlet=new LogServlet();
  420.         logServlet.doGet(request,response);
  421.         JSONArray jsonArray1=new JSONArray(response.getContentAsString());
  422.         assertTrue(jsonArray1.get(0).toString().equals(jsonArray.get(0).toString()));
  423.         assertEquals(200,response.getStatus());
  424.         assertTrue(response.getContentType().startsWith("application/json"));
  425.     }
  426.     /*
  427.         test doGet() serachlogs: go to path searchLogs and limit is not null and level is not null, search empty list
  428.     */
  429.     @Test
  430.     public void testGetLimitEmptyList() throws IOException,ServletException{
  431.         MockHttpServletRequest request = new MockHttpServletRequest();
  432.         MockHttpServletResponse response=new MockHttpServletResponse();
  433.         request.setParameter("limit","1");
  434.         request.setParameter("level","debug");
  435.         request.setPathInfo("/searchLogs");
  436.         LogServlet logServlet=new LogServlet();
  437.         logServlet.doGet(request,response);
  438.         assertEquals(200,response.getStatus());
  439.         assertTrue(response.getContentType().startsWith("application/json"));
  440.     }
  441.     /*
  442.          test doGet() serachlogs: go to path searchLogs and limit is null and level is null, search not empty list
  443.     */
  444.     @Test
  445.     public void testGetNotEmpty() throws IOException,ServletException{
  446.         MockHttpServletRequest request = new MockHttpServletRequest();
  447.         MockHttpServletResponse response=new MockHttpServletResponse();
  448.         request.setPathInfo("/addlog");
  449.         LogServlet logServlet=new LogServlet();
  450.         JSONObject object = new JSONObject();
  451.         object.put("id", "1");
  452.         object.put("message", "message");
  453.         object.put("timestamp", Instant.now().toString());
  454.         object.put("thread", "thread");
  455.         object.put("logger", "logger");
  456.         object.put("level", "debug");
  457.         object.put("errorDetails", "errorDetails");
  458.         JSONArray jsonArray=new JSONArray();
  459.         jsonArray.put(object);
  460.         request.setContent(jsonArray.toString().getBytes());
  461.         logServlet.doPost(request,response);
  462.         request = new MockHttpServletRequest();
  463.         response=new MockHttpServletResponse();
  464.         request.setPathInfo("/searchLogs");
  465.         logServlet=new LogServlet();
  466.         logServlet.doGet(request,response);
  467.         assertEquals(200,response.getStatus());
  468.         assertTrue(response.getContentType().startsWith("application/json"));
  469.     }
  470.     /*
  471.         test doGet() serachlogs: go to path searchLogs and level is info and limit is not null, search not empty list
  472.     */
  473.     @Test
  474.     public void testGetLimit() throws IOException,ServletException{
  475.         MockHttpServletRequest request = new MockHttpServletRequest();
  476.         MockHttpServletResponse response=new MockHttpServletResponse();
  477.         request.setPathInfo("/addlog");
  478.         LogServlet logServlet=new LogServlet();
  479.         JSONObject object = new JSONObject();
  480.         object.put("id", "11");
  481.         object.put("message", "message");
  482.         object.put("timestamp", Instant.now().toString());
  483.         object.put("thread", "thread");
  484.         object.put("logger", "logger");
  485.         object.put("level", "debug");
  486.         object.put("errorDetails", "errorDetails");
  487.         JSONArray jsonArray=new JSONArray();
  488.         jsonArray.put(object);
  489.         object = new JSONObject();
  490.         object.put("id", "12");
  491.         object.put("message", "message");
  492.         object.put("timestamp", Instant.now().toString());
  493.         object.put("thread", "thread");
  494.         object.put("logger", "logger");
  495.         object.put("level", "info");
  496.         object.put("errorDetails", "errorDetails");
  497.         jsonArray.put(object);
  498.         request.setContent(jsonArray.toString().getBytes());
  499.         logServlet.doPost(request,response);
  500.  
  501.         request = new MockHttpServletRequest();
  502.         response=new MockHttpServletResponse();
  503.         request.setParameter("limit","2");
  504.         request.setParameter("level","info");
  505.         request.setPathInfo("/searchLogs");
  506.         logServlet=new LogServlet();
  507.         logServlet.doGet(request,response);
  508.  
  509.         String result = response.getContentAsString();
  510.         String[] names = result.split(" ");
  511.         Set<String> set = Arrays.stream(names).collect(Collectors.toSet());
  512.         assertEquals(200,response.getStatus());
  513.         jsonArray.remove(0);
  514.         assertTrue(logServlet.getLog().size()!=0);
  515.         assertTrue(set.contains(jsonArray.toString()));
  516.         assertTrue(response.getContentType().startsWith("application/json"));
  517.     }
  518.  
  519.     /*
  520.         test doGet() serachlogs: go to path searchLogs and limit is 1 and level is  all, search not empty list
  521.     */
  522.     @Test
  523.     public void testGetLimitLevel() throws IOException, ServletException, InterruptedException {
  524.         MockHttpServletRequest request = new MockHttpServletRequest();
  525.         MockHttpServletResponse response=new MockHttpServletResponse();
  526.         request.setPathInfo("/addlog");
  527.         LogServlet logServlet=new LogServlet();
  528.         JSONObject object = new JSONObject();
  529.         object.put("id", "111");
  530.         object.put("message", "message");
  531.         object.put("timestamp", Instant.now().toString());
  532.         object.put("thread", "thread");
  533.         object.put("logger", "logger");
  534.         object.put("level", "warn");
  535.         object.put("errorDetails", "errorDetails");
  536.         JSONArray jsonArray=new JSONArray();
  537.         jsonArray.put(object);
  538.  
  539.         object = new JSONObject();
  540.         object.put("id", "112");
  541.         object.put("message", "message");
  542.         object.put("timestamp", Instant.now().toString());
  543.         object.put("thread", "thread");
  544.         object.put("logger", "logger");
  545.         object.put("level", "error");
  546.         object.put("errorDetails", "errorDetails");
  547.         jsonArray.put(object);
  548.         object = new JSONObject();
  549.         object.put("id", "11212314");
  550.         object.put("message", "message");
  551.         object.put("timestamp", Instant.now().toString());
  552.         object.put("thread", "thread");
  553.         object.put("logger", "trace");
  554.         object.put("level", "error");
  555.         object.put("errorDetails", "errorDetails");
  556.         jsonArray.put(object);
  557.         object = new JSONObject();
  558.         object.put("id", "11275563");
  559.         object.put("message", "message");
  560.         object.put("timestamp", Instant.now().toString());
  561.         object.put("thread", "thread");
  562.         object.put("logger", "logger");
  563.         object.put("level", "off");
  564.         object.put("errorDetails", "errorDetails");
  565.         jsonArray.put(object);
  566.  
  567.         object = new JSONObject();
  568.         object.put("id", "113");
  569.         object.put("message", "message");
  570.         object.put("timestamp", Instant.now().toString());
  571.         object.put("thread", "thread");
  572.         object.put("logger", "logger");
  573.         object.put("level", "fatal");
  574.         object.put("errorDetails", "errorDetails");
  575.         jsonArray.put(object);
  576.         request.setContent(jsonArray.toString().getBytes());
  577.         logServlet.doPost(request,response);
  578.  
  579.         request = new MockHttpServletRequest();
  580.         response=new MockHttpServletResponse();
  581.         request.setParameter("limit","1");
  582.         request.setParameter("level","all");
  583.         request.setPathInfo("/searchLogs");
  584.         logServlet=new LogServlet();
  585.         logServlet.doGet(request,response);
  586.  
  587.         String result = response.getContentAsString();
  588.         String[] names = result.split(" ");
  589.         Set<String> set = Arrays.stream(names).collect(Collectors.toSet());
  590.         assertEquals(200,response.getStatus());
  591.         jsonArray.remove(0);
  592.         jsonArray.remove(0);
  593.         jsonArray.remove(0);
  594.         jsonArray.remove(0);
  595.         //This asserTrue is actually right and when i run the test still succeed. But when i run mvn test clean, it will show some error.
  596.         //assertTrue(set.contains(jsonArray.toString()));
  597.         assertTrue(response.getContentType().startsWith("application/json"));
  598.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top