Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
Feature detection mechanism cuba-platform/cuba-rest-js#20
Browse files Browse the repository at this point in the history
I18n approach cuba-platform/front-generator#33

Added tests for VersionController and UserSessionController
  • Loading branch information
vyacheslav-pushkin committed Dec 4, 2019
1 parent 1cb5426 commit e85b144
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ public static CloseableHttpResponse sendPut(String url, String token, String bod
return httpClient.execute(httpPut);
}

public static CloseableHttpResponse sendPutWithHeaders(String url, String token, String body, @Nullable Map<String, String> params, Map<String, String> headers) throws Exception {
URIBuilder uriBuilder = new URIBuilder(URI_BASE + url);
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(uriBuilder.build());
StringEntity stringEntity = new StringEntity(body);
httpPut.setEntity(stringEntity);
httpPut.setHeader("Authorization", "Bearer " + token);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
return httpClient.execute(httpPut);
}

public static CloseableHttpResponse sendDelete(String url, String token, @Nullable Map<String, String> params) throws Exception {
URIBuilder uriBuilder = new URIBuilder(URI_BASE + url);
if (params != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.haulmont.rest.demo.http.rest;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.junit.Test;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static com.haulmont.rest.demo.http.rest.RestTestUtils.*;
import static org.junit.Assert.*;

public class UserSessionControllerFT extends AbstractRestControllerFT {

@Test
public void setSessionLocale() throws Exception {
setSessionLocale(
"en",
oauthToken,
response -> assertEquals(HttpStatus.SC_OK, statusCode(response))
);
}

@Test
public void setSessionLocaleUnauthorized() throws Exception {
setSessionLocale(
"en",
null,
response -> assertEquals(HttpStatus.SC_UNAUTHORIZED, statusCode(response))
);
}

@Test
public void setSessionLocaleUnsupported() throws Exception {
setSessionLocale(
"a string representing unsupported locale",
oauthToken,
response -> assertEquals(HttpStatus.SC_UNPROCESSABLE_ENTITY, statusCode(response))
);
}

private void setSessionLocale(
String locale, @Nullable String token, Consumer<CloseableHttpResponse> assertionsCallback
) throws Exception {
String url = "/user-session/locale";

Map<String, String> headers = new HashMap<>();
headers.put(HttpHeaders.ACCEPT_LANGUAGE, locale);

try (CloseableHttpResponse response = sendPutWithHeaders(url, token, "", null, headers)) {
assertionsCallback.accept(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.haulmont.rest.demo.http.rest;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.junit.Test;

import static com.haulmont.rest.demo.http.rest.RestTestUtils.*;
import static org.junit.Assert.*;

public class VersionControllerFT extends AbstractRestControllerFT {

@Test
public void getApiVersion() throws Exception {
String url = "/version";
try (CloseableHttpResponse response = sendGet(url, oauthToken, null)) {
assertEquals(HttpStatus.SC_OK, statusCode(response));

String version = responseToString(response);
assertNotNull(version);
assertTrue(version.length() > 0);
}
}
}

0 comments on commit e85b144

Please sign in to comment.