/* * Copyright 2010-2023 Amazon.com, Inc. or its affiliates. All Rights * Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is * distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either * express or implied. See the License for the specific language * governing * permissions and limitations under the License. */ package com.amazonaws.util; import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import org.apache.commons.io.IOUtils; import org.junit.Test; /** * Test for filtering out XML namespace attributes when reading XML. */ public class NamespaceRemovingInputStreamTest { private static final String SAMPLE_RESPONSE_1 = "\n" + " \n" + " 195078\n" + " \n" + " \n" + " b1e8f1f7-42e9-494c-ad09-2674e557526d\n" + " \n" + ""; private static final String SAMPLE_RESPONSE_2 = "\n" + " \n" + " 195078\n" + " \n" + " \n" + " b1e8f1f7-42e9-494c-ad09-2674e557526d\n" + " \n" + ""; private static final String SAMPLE_RESPONSE_3 = "\n" + " \n" + " 195078\n" + " \n" + " \n" + " b1e8f1f7-42e9-494c-ad09-2674e557526d\n" + " \n" + ""; private static final String EXPECTED_RESULT = "\n" + " \n" + " 195078\n" + " \n" + " \n" + " b1e8f1f7-42e9-494c-ad09-2674e557526d\n" + " \n" + ""; @Test public void testNamespaceRemoval() throws Exception { assertEquals(EXPECTED_RESULT, removeNamespace(SAMPLE_RESPONSE_1)); assertEquals(EXPECTED_RESULT, removeNamespace(SAMPLE_RESPONSE_2)); assertEquals(EXPECTED_RESULT, removeNamespace(SAMPLE_RESPONSE_3)); } private String removeNamespace(String xml) throws Exception { NamespaceRemovingInputStream inputStream = new NamespaceRemovingInputStream(new ByteArrayInputStream(xml.getBytes())); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(inputStream, outputStream); return new String(outputStream.toByteArray()); } }