package org.keycloak.protocol.cas;
import org.junit.Test;
import org.keycloak.protocol.cas.representations.CasServiceResponse;
import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
import org.keycloak.protocol.cas.utils.ServiceResponseMarshaller;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class ServiceResponseTest {
private static final String EXPECTED_JSON_SUCCESS = "{\n" +
" \"serviceResponse\" : {\n" +
" \"authenticationSuccess\" : {\n" +
" \"user\" : \"username\",\n" +
" \"proxyGrantingTicket\" : \"PGTIOU-test\",\n" +
" \"proxies\" : [ \"https://proxy1/pgtUrl\", \"https://proxy2/pgtUrl\" ],\n" +
" \"attributes\" : {\n" +
" \"string\" : \"abc\",\n" +
" \"list\" : [ \"a\", \"b\" ],\n" +
" \"int\" : 123\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
private static final String EXPECTED_XML_SUCCESS = "\n" +
" \n" +
" username\n" +
" PGTIOU-test\n" +
" \n" +
" https://proxy1/pgtUrl\n" +
" https://proxy2/pgtUrl\n" +
" \n" +
" \n" +
" abc\n" +
" a\n" +
" b\n" +
" 123\n" +
" \n" +
" \n" +
"";
private static final String EXPECTED_JSON_FAILURE = "{\n" +
" \"serviceResponse\" : {\n" +
" \"authenticationFailure\" : {\n" +
" \"code\" : \"ERROR_CODE\",\n" +
" \"description\" : \"Error description\"\n" +
" }\n" +
" }\n" +
"}";
private static final String EXPECTED_XML_FAILURE = "\n" +
" Error description\n" +
"";
@Test
public void testSuccessResponse() throws Exception {
Map attributes = new HashMap<>();
attributes.put("list", Arrays.asList("a", "b"));
attributes.put("int", 123);
attributes.put("string", "abc");
CasServiceResponse response = ServiceResponseHelper.createSuccess("username", attributes, "PGTIOU-test",
Arrays.asList("https://proxy1/pgtUrl", "https://proxy2/pgtUrl"));
assertEquals(EXPECTED_JSON_SUCCESS, ServiceResponseMarshaller.marshalJson(response));
assertEquals(EXPECTED_XML_SUCCESS, ServiceResponseMarshaller.marshalXml(response));
}
@Test
public void testErrorResponse() throws Exception {
CasServiceResponse response = ServiceResponseHelper.createFailure("ERROR_CODE", "Error description");
assertEquals(EXPECTED_JSON_FAILURE, ServiceResponseMarshaller.marshalJson(response));
assertEquals(EXPECTED_XML_FAILURE, ServiceResponseMarshaller.marshalXml(response));
}
}