From e3e192bc4cb1fb4c7aa5eee57eab525500388ce7 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 24 Nov 2023 15:29:53 +0000
Subject: [PATCH] ValidateEndpoint: remove obsolete usage of StringBuilder

---
 src/test/java/org/keycloak/protocol/cas/ServiceResponseTest.java |   30 ++++--------------------------
 1 files changed, 4 insertions(+), 26 deletions(-)

diff --git a/src/test/java/org/keycloak/protocol/cas/ServiceResponseTest.java b/src/test/java/org/keycloak/protocol/cas/ServiceResponseTest.java
index b8ebe5a..bed1f00 100644
--- a/src/test/java/org/keycloak/protocol/cas/ServiceResponseTest.java
+++ b/src/test/java/org/keycloak/protocol/cas/ServiceResponseTest.java
@@ -1,7 +1,6 @@
 package org.keycloak.protocol.cas;
 
 import com.jayway.jsonpath.JsonPath;
-import com.sun.xml.bind.v2.util.FatalAdapter;
 import org.junit.Test;
 import org.keycloak.protocol.cas.representations.CASErrorCode;
 import org.keycloak.protocol.cas.representations.CASServiceResponse;
@@ -9,20 +8,14 @@
 import org.keycloak.protocol.cas.utils.ServiceResponseMarshaller;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
-import org.xml.sax.helpers.DefaultHandler;
 import org.xmlunit.xpath.JAXPXPathEngine;
 import org.xmlunit.xpath.XPathEngine;
 
-import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.validation.Schema;
-import javax.xml.validation.SchemaFactory;
-import java.io.ByteArrayInputStream;
-import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 import static org.junit.Assert.assertEquals;
+import static org.keycloak.protocol.cas.XMLValidator.parseAndValidate;
+import static org.keycloak.protocol.cas.XMLValidator.schemaFromClassPath;
 
 public class ServiceResponseTest {
     private final XPathEngine xpath = new JAXPXPathEngine();
@@ -55,7 +48,7 @@
         // Build and validate XML response
 
         String xml = ServiceResponseMarshaller.marshalXml(response);
-        Document doc = parseAndValidate(xml);
+        Document doc = parseAndValidate(xml, schemaFromClassPath("cas-response-schema.xsd"));
         assertEquals("username", xpath.evaluate("/cas:serviceResponse/cas:authenticationSuccess/cas:user", doc));
         int idx = 0;
         for (Node node : xpath.selectNodes("/cas:serviceResponse/cas:authenticationSuccess/cas:attributes/cas:list", doc)) {
@@ -88,23 +81,8 @@
         // Build and validate XML response
 
         String xml = ServiceResponseMarshaller.marshalXml(response);
-        Document doc = parseAndValidate(xml);
+        Document doc = parseAndValidate(xml, schemaFromClassPath("cas-response-schema.xsd"));
         assertEquals(CASErrorCode.INVALID_REQUEST.name(), xpath.evaluate("/cas:serviceResponse/cas:authenticationFailure/@code", doc));
         assertEquals("Error description", xpath.evaluate("/cas:serviceResponse/cas:authenticationFailure", doc));
-    }
-
-    /**
-     * Parse XML document and validate against CAS schema
-     */
-    private Document parseAndValidate(String xml) throws Exception {
-        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
-                .newSchema(getClass().getResource("cas-response-schema.xsd"));
-
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        factory.setSchema(schema);
-        factory.setNamespaceAware(true);
-        DocumentBuilder builder = factory.newDocumentBuilder();
-        builder.setErrorHandler(new FatalAdapter(new DefaultHandler()));
-        return builder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
     }
 }

--
Gitblit v1.9.1