Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2014 GraphAware
   *
   * This file is part of GraphAware.
   *
   * GraphAware is free software: you can redistribute it and/or modify it under the terms of
   * the GNU General Public License as published by the Free Software Foundation, either
   * version 3 of the License, or (at your option) any later version.
   *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU General Public License for more details. You should have received a copy of
  * the GNU General Public License along with this program.  If not, see
  * <http://www.gnu.org/licenses/>.
  */
 
 package com.graphaware.test.util;
 
 
 
 import static org.junit.Assert.*;

Utilities mainly intended for testing.
 
 public final class TestUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);

    
Assert that two JSON objects represented as Strings are semantically equal.

Parameters:
one one.
two two.
 
     public static void assertJsonEquals(String oneString two) {
         ObjectMapper mapper = new ObjectMapper();
         try {
             assertTrue(mapper.readTree(one).equals(mapper.readTree(two)));
         } catch (IOException e) {
             fail();
         }
     }

    
Convert a JSON file to String.

Parameters:
fileName name of the file present in the root of the resources directory.
Returns:
JSON as String.
 
     public static String jsonAsString(String fileName) {
         return jsonAsString(""fileName);
     }

    
Convert a JSON file to String.

Parameters:
caller the class calling this method. The file is expected to be in the resources directory in the same package as this class.
fileName name of the file present in the resources directory in the same package as the class above.
Returns:
JSON as String.
 
     public static String jsonAsString(Class callerString fileName) {
         return jsonAsString(caller.getClass().getPackage().getName().replace(".""/") + "/"fileName);
     }

    
Convert a JSON file to String.

Parameters:
packagePath path to package. The file is expected to be in the resources directory in the same package.
fileName name of the file present in the resources directory in the package defined above.
Returns:
JSON as String.
 
     public static String jsonAsString(String packagePathString fileName) {
         try {
             return IOUtils.toString(new ClassPathResource(packagePath + fileName + ".json").getInputStream());
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }

    
Issue an HTTP GET and assert the response status code.

Parameters:
url to GET.
expectedStatusCode expected status code.
Returns:
the body of the response.
    public static String get(String urlfinal int expectedStatusCode) {
        return method(new HttpGet(url), expectedStatusCode);
    }

    
Issue an HTTP POST with empty body and assert the response status code.

Parameters:
url to POST to.
expectedStatusCode expected status code.
Returns:
the body of the response.
    public static String post(String urlfinal int expectedStatusCode) {
        return post(urlnullexpectedStatusCode);
    }

    
Issue an HTTP POST and assert the response status code.

Parameters:
url to POST to.
json request body.
expectedStatusCode expected status code.
Returns:
the body of the response.
    public static String post(String urlString jsonfinal int expectedStatusCode) {
        HttpPost post = new HttpPost(url);
        if (json != null) {
            post.setEntity(new StringEntity(json.));
        }
        return method(postexpectedStatusCode);
    }

    
Issue an HTTP PUT with empty body and assert the response status code.

Parameters:
url to PUT to.
expectedStatusCode expected status code.
Returns:
the body of the response.
    public static String put(String urlfinal int expectedStatusCode) {
        return put(urlnullexpectedStatusCode);
    }

    
Issue an HTTP PUT and assert the response status code.

Parameters:
url to POST to.
json request body.
expectedStatusCode expected status code.
Returns:
the body of the response.
    public static String put(String urlString jsonfinal int expectedStatusCode) {
        HttpPut put = new HttpPut(url);
        if (json != null) {
            put.setEntity(new StringEntity(json.));
        }
        return method(putexpectedStatusCode);
    }

    
Issue an HTTP DELETE and assert the response status code.

Parameters:
url to DELETE.
expectedStatusCode expected status code.
Returns:
the body of the response.
    public static String delete(String urlfinal int expectedStatusCode) {
        return method(new HttpDelete(url), expectedStatusCode);
    }

    
Issue a HTTP call and assert the response status code.

Parameters:
method HTTP method.
expectedStatusCode expected status code.
Returns:
the body of the response.
    protected static String method(HttpRequestBase methodfinal int expectedStatusCode) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            ResponseHandler<StringresponseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(final HttpResponse responsethrows IOException {
                    assertEquals(expectedStatusCoderesponse.getStatusLine().getStatusCode());
                    if (response.getEntity() != null) {
                        return EntityUtils.toString(response.getEntity());
                    } else {
                        return null;
                    }
                }
            };
            String result = httpClient.execute(methodresponseHandler);
            .debug("HTTP " + method.getMethod() + " returned: " + result);
            return result;
        } catch (IOException e) {
            fail(e.getMessage());
            return null;
        }
    }

    
Execute a set of cypher statements against a database in a single transaction.

Parameters:
serverUrl URL of the database server.
cypherStatements to execute.
Returns:
body of the server response.
    public static String executeCypher(String serverUrlString... cypherStatements) {
        StringBuilder stringBuilder = new StringBuilder("{\"statements\" : [ {");
        for (String statement : cypherStatements) {
            stringBuilder.append("\"statement\" : \"").append(statement).append("\"");
        }
        stringBuilder.append("}]}");
        while (serverUrl.endsWith("/")) {
            serverUrl = serverUrl.substring(0, serverUrl.length() - 1);
        }
        return post(serverUrl + "/db/data/transaction/commit"stringBuilder.toString(), .);
    }

    
Measure the time of the timed callback.

Parameters:
timed callback.
Returns:
time in microseconds
    public static long time(Timed timed) {
        long start = System.nanoTime();
        timed.time();
        return (System.nanoTime() - start) / 1000;
    }

    
Timed operation.
    public interface Timed {

        
Perform the operation to be timed.
        void time();
    }
    private TestUtils() {
    }
New to GrepCode? Check out our FAQ X