Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.devbliss.doctest;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertFalse;
  import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.assertNull;
  import static org.junit.Assert.assertTrue;
  
  import java.io.File;
 import java.net.URI;
 import java.util.List;
 
 
 
 
 
 public abstract class LogicDocTest {
 
     protected static DocTestMachine docTestMachine;
 
     private final ApiTest apiTest;
     private final JSONHelper jsonHelper;
     private final FileHelper fileHelper;
 
     @Before
     public void ensureDocTestClassSet() {
     }
 
     @AfterClass
     public static void finishDocTest() throws Exception {
     }
 
     public LogicDocTest(
             DocTestMachine docTest,
             ApiTest apiTest,
             JSONHelper jsonHelper,
             FileHelper fileHelper) {
         . = docTest;
         this. = apiTest;
         this. = jsonHelper;
         this. = fileHelper;
     }

    
This method must be overridden if you want to set an introduction The default value of the introduction is: empty.

Returns:
 
     protected String getIntroduction() {
         return new String();
     }

    
Returns the name of the report file

Returns:
name of the file without extension
 
     protected abstract String getFileName();
 
     protected void say(String say) {
         .say(say);
     }

    
say will result in a text in the resulting document with the support of JSON representations for objects and highlighted text.

Parameters:
say the text to be said
objects multiple Objects matching the amount of placeholdern in the text
 
     protected void say(String sayObject... objects) {
         String[] stringRepresentations = new String[objects.length];
         for (int i = 0; i < objects.lengthi++) {
             if (objects[iinstanceof String) {
                 stringRepresentations[i] = (Stringobjects[i];
             } else {
                 stringRepresentations[i] = .toJson(objects[i], true);
             }
         }
         .say(saystringRepresentations);
    }
    protected void sayNextSection(String sectionName) {
        .sayNextSectionTitle(sectionName);
    }
    private void sayUri(ApiRequest apiRequestObject objthrows Exception {
        .sayRequest(apiRequest.toJson(obj), showHeaders());
    }
    private void sayUri(ApiRequest apiRequestthrows Exception {
        .sayRequest(apiRequestnullshowHeaders());
    }

    
The given POJO will be converted to JSON and be pretty printed.

Parameters:
obj
Throws:
java.lang.Exception
    protected void sayObject(Object objthrows Exception {
        .sayPreformatted(obj == null ? "" : new JSONHelper().toJson(objtrue));
    }

    
The given String will be formatted as-is and be highlighted in a fancy box.

Parameters:
code
Throws:
java.lang.Exception
    protected void sayPreformattedCode(String codethrows Exception {
        .sayPreformatted(code == null ? "" : code);
    }
    protected ApiResponse makeGetRequestSilent(URI urithrows Exception {
        return doGetRequest(uri).;
    }
    private Context doGetRequest(URI urithrows Exception {
        return .get(uri);
    }
    protected ApiResponse makeGetRequest(URI urithrows Exception {
        Context context = doGetRequest(uri);
        sayUri(context.apiRequest);
        .sayResponse(context.apiResponseshowHeaders());
        return context.apiResponse;
    }
    protected ApiResponse makePostRequestSilent(URI uriObject objthrows Exception {
        return doPostRequest(uriobj).;
    }
    private Context doPostRequest(URI uriObject objthrows Exception {
        return .post(uriobj);
    }
    protected ApiResponse makePostRequest(URI urithrows Exception {
        return makePostRequest(urinull);
    }
    protected ApiResponse makePostRequest(URI uriObject objthrows Exception {
        Context context = doPostRequest(uriobj);
        sayUri(context.apiRequestobj);
        .sayResponse(context.apiResponseshowHeaders());
        return context.apiResponse;
    }
    protected ApiResponse makePostUploadRequest(URI uriFile fileToUploadString paramName)
            throws Exception {
        FileBody fileBodyToUpload = new FileBody(fileToUpload);
        String mimeType = new MimetypesFileTypeMap().getContentType(fileToUpload);
        Context context =
                .post(urinullnew PostUploadWithoutRedirectImpl(paramName,
                        fileBodyToUpload));
        .sayUploadRequest(context.apiRequestfileBodyToUpload.getFilename(),
                .readFile(fileToUpload), fileToUpload.length(), mimeTypeshowHeaders());
        .sayResponse(context.apiResponseshowHeaders());
        return context.apiResponse;
    }
    protected ApiResponse makePutRequestSilent(URI uriObject objthrows Exception {
        return doPutRequest(uriobj).;
    }
    private Context doPutRequest(URI uriObject objthrows Exception {
        return .put(uriobj);
    }
    protected ApiResponse makePutRequest(URI urithrows Exception {
        return makePutRequest(urinull);
    }
    protected ApiResponse makePutRequest(URI uriObject objthrows Exception {
        Context context = doPutRequest(uriobj);
        sayUri(context.apiRequestobj);
        .sayResponse(context.apiResponseshowHeaders());
        return context.apiResponse;
    }
    protected ApiResponse makeDeleteRequestSilent(URI urithrows Exception {
        return doDeleteRequest(uri).;
    }
    private Context doDeleteRequest(URI urithrows Exception {
        return .delete(uri);
    }
    protected ApiResponse makeDeleteRequestSilent(URI uriObject objthrows Exception {
        return doDeleteRequest(uriobj).;
    }
    private Context doDeleteRequest(URI uriObject objthrows Exception {
        return .delete(uriobj);
    }
    protected ApiResponse makeDeleteRequest(URI urithrows Exception {
        return makeDeleteRequest(urinull);
    }
    protected ApiResponse makeDeleteRequest(URI uriObject objthrows Exception {
        Context context = doDeleteRequest(uriobj);
        sayUri(context.apiRequestobj);
        .sayResponse(context.apiResponseshowHeaders());
        return context.apiResponse;
    }

    

Parameters:
expected
given
message
    protected void assertEqualsAndSay(Object expectedObject givenString message) {
        assertEquals(expectedgiven);
        .sayVerify(message);
    }

    
At first converts both objects to Json and then asserts that they are equal. The resulting doc will sport the expected Json String.

Parameters:
expected POJO
result POJO
    protected void assertJsonEqualsAndSay(Object expectedObject result) {
        assertJsonEqualsAndSay(expectedresult""null);
    }

    
At first converts both objects to Json and then asserts that they are equal, except on the fields mentioned in exceptions. The resulting doc will sport the expected Json String.

Parameters:
expected POJO
result POJO
    protected void assertJsonEqualsAndSay(Object expectedObject resultList<Stringexceptions) {
        assertJsonEqualsAndSay(expectedresult""exceptions);
    }

    
First converts both objects to Json and then asserts that they are equal, except on the fields mentioned in exceptions. The resulting doc will sport the expected Json String after the given message.

Parameters:
expected POJO
result POJO
message Additional message to be concatenated to the expected Json
exceptions a List of fieldnames that will be omitted in comparison
    protected void assertJsonEqualsAndSay(Object expectedObject resultString message,
            List<Stringexceptions) {
        String expectedJson;
        String resultingJson;
        if (exceptions != null && exceptions.size() > 0) {
            expectedJson = .toJsonAndSkipCertainFields(expectedexceptionstrue);
            resultingJson = .toJsonAndSkipCertainFields(resultexceptionstrue);
        } else {
            expectedJson = .toJson(expectedtrue);
            resultingJson = .toJson(resulttrue);
        }
        assertEquals(expectedJsonresultingJson);
        .sayVerify(message + expectedJson);
    }
    protected void assertTrueAndSay(Boolean conditionString message) {
        assertTrue(condition);
        .sayVerify(message);
    }
    protected void assertFalseAndSay(Boolean conditionString message) {
        assertFalse(condition);
        .sayVerify(message);
    }
    protected void assertNullAndSay(Object objectString message) {
        assertNull(object);
        .sayVerify(message);
    }
    protected void assertNotNullAndSay(Object objectString message) {
        assertNotNull(object);
        .sayVerify(message);
    }

    
Assert that the value of the cookie with the given name equals the given value. The resulting cookie name and value will be printed, separated by a colon.

Parameters:
name The name of the cookie
expectedValue The expected value
    protected void assertCookieEqualsAndSay(String nameString expectedValue) {
        String value = .getTestState().getCookieValue(name);
        assertEquals(expectedValuevalue);
        .sayVerify(name + ": " + value);
    }

    
Assert that the cookie with the given name is present. The resulting cookie name and value will be printed, separated by a colon.

Parameters:
name The name of the cookie
    protected void assertCookiePresentAndSay(String name) {
        String value = .getTestState().getCookieValue(name);
        assertNotNull(value);
        .sayVerify(name + ": " + value);
    }

    
Assert that the cookie with the given name is not. The resulting cookie name and null will be printed, separated by a colon.

Parameters:
name The name of the cookie
    protected void assertCookieNotPresentAndSay(String name) {
        String value = .getTestState().getCookieValue(name);
        assertNull(value);
        .sayVerify(name + ": " + value);
    }

    
Assert that a cookie is present that matches the attributes of the given cookie The resulting cookie will be output with its parameters, in JSON format.

Parameters:
expected The cookie to check
    protected void assertCookieMatchesAndSay(Cookie expected) {
        Cookie cookie = .getTestState().getCookie(expected.name);
        assertNotNull(cookie);
        assertEquals(expected.valuecookie.value);
        if (expected.expires == null) {
            assertNull(cookie.expires);
        } else {
            assertNotNull(cookie.expires);
        }
        assertEquals(expected.pathcookie.path);
        assertEquals(expected.domaincookie.domain);
        assertEquals(expected.securecookie.secure);
        assertEquals(expected.httpOnlycookie.httpOnly);
        .sayVerify(.toJson(cookie));
    }

    
Get the value of the cookie with the given name

Parameters:
name The name of the cookie
Returns:
The cookies value, or null if no cookie with that name was found
    protected String getCookieValue(String name) {
        return .getTestState().getCookieValue(name);
    }

    
Add the cookie with the given name and value to the current state

Parameters:
name The name of the cookie
value The value of the cookie
    protected void addCookie(String nameString value) {
        .getTestState().addCookie(
                new Cookie(namevaluenull"/""localhost"falsefalse));
    }

    
Clear all cookies from the current state
    protected void clearCookies() {
        .getTestState().clearCookies();
    }

    
defines the headers that we want to render for the documentation for this case: its the default declaration and no headers are declared if you want to define headers to display for a request or response, you HAVE TO override this function in your test IMPORTANT: you don't have to care about the case of the headers name

Returns:
    public List<StringshowHeaders() {
        return new ArrayList<String>();
    }
New to GrepCode? Check out our FAQ X