/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Web Questionnaires 2
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency. Portions created by TripleDev are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 *        Anton Dmitrijev
 */
package eionet.webq.converter;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import eionet.webq.dto.UploadedXmlFile;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;

public class MultipartFileConverterTest {

    @Test
    public void convertToUploadedFile() throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<derogations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xml:lang=\"en\" xsi:noNamespaceSchemaLocation=\"http://biodiversity.eionet.europa.eu/schemas/bernconvention/derogations.xsd\" country=\"\">" +
                "</derogations>";
        String originalFilename = "file.xml";
        MockMultipartFile xmlFileUpload = new MockMultipartFile("xmlFileUpload", originalFilename, MediaType.APPLICATION_XML_VALUE, xml.getBytes());

        MultipartFileConverter multipartFileConverter = new MultipartFileConverter();
        UploadedXmlFile xmlFile = multipartFileConverter.convert(xmlFileUpload);
        assertThat(xmlFile.getName(), equalTo(originalFilename));
        assertThat(xmlFile.getContent(), equalTo(xml.getBytes()));
        assertThat(xmlFile.getXmlSchema(), equalTo("http://biodiversity.eionet.europa.eu/schemas/bernconvention/derogations.xsd"));
    }
}

@@ -925,16 +925,72 @@
 mlFile;%0A
+import eionet.webq.exception.WebQuestionnaireException;%0A
 import o
@@ -1106,50 +1106,243 @@
 le;%0A
-%0Apublic class MultipartFileConverterTest %7B
+import org.springframework.web.multipart.MultipartFile;%0A%0Apublic class MultipartFileConverterTest %7B%0A    private MultipartFileConverter fileConverter = new MultipartFileConverter();%0A    private final String originalFilename = %22file.xml%22;
 %0A%0A  
@@ -1427,91 +1427,83 @@
 ing 
-xml = %22%3C?xml version=%5C%221.0%5C%22 encoding=%5C%22UTF-8%5C%22?%3E%22 +%0A                %22%3Cderog
+schemaLocation = %22testSchema%22;%0A        String rootAttributesDeclar
 ation
-s
  
+= %22
 xmln
@@ -1558,183 +1558,54 @@
 e%5C%22 
-xml:lang=%5C%22en%5C%22 xsi:noNamespaceSchemaLocation=%5C%22http://biodiversity.eionet.europa.eu/schemas/bernconvention/derogations.xsd%5C%22 country=%5C%22%5C%22%3E%22 +%0A                %22%3C/derog
+%22 + noNamespaceSchemaAttribute(schemaLoc
 ation
-s%3E%22
+)
 ;%0A  
@@ -1614,196 +1614,86 @@
     
-String originalFilename = %22file.xml%22;%0A        MockMultipartFile xmlFileUpload = new MockMultipartFile(%22xmlFileUpload%22, originalFilename, MediaType.APPLICATION_XML_VALUE, xml.getBytes()
+byte%5B%5D fileContent = xmlWithRootElementAttributes(rootAttributesDeclaration
 );%0A
-%0A
     
@@ -1713,47 +1713,31 @@
 File
-Converter multipartFileConverter = new 
+ xmlFileUpload = create
 Mult
@@ -1741,37 +1741,40 @@
 ultipartFile
-Converter(
+(fileContent
 );%0A
+%0A
         Uplo
@@ -1799,18 +1799,9 @@
 e = 
-multipartF
+f
 ileC
@@ -1833,16 +1833,17 @@
 pload);%0A
+%0A
         
@@ -1953,22 +1953,19 @@
 lTo(
-xml.getBytes()
+fileContent
 ));%0A
@@ -2019,86 +2019,974 @@
 lTo(
-%22http://biodiversity.eionet.europa.eu/schemas/bernconvention/derogations.xsd%22)
+schemaLocation));%0A        assertThat(xmlFile.getSizeInBytes(), equalTo(xmlFileUpload.getSize()));%0A    %7D%0A%0A    @Test(expected = WebQuestionnaireException.class)%0A    public void throwsExceptionIfNamespaceXsiNotDeclared() %7B%0A        fileConverter.convert(createMultipartFile(xmlWithRootElementAttributes(noNamespaceSchemaAttribute(%22foo%22))));%0A    %7D%0A%0A    private String noNamespaceSchemaAttribute(String schemaLocation) %7B%0A        return %22xsi:noNamespaceSchemaLocation=%5C%22%22 + schemaLocation + %22%5C%22%22;%0A    %7D%0A%0A    private MultipartFile createMultipartFile(byte%5B%5D content) %7B%0A        return new MockMultipartFile(%22xmlFileUpload%22, originalFilename, MediaType.APPLICATION_XML_VALUE, content);%0A    %7D%0A%0A    private byte%5B%5D xmlWithRootElementAttributes(String rootAttributesDeclaration) %7B%0A        String xml = %22%3C?xml version=%5C%221.0%5C%22 encoding=%5C%22UTF-8%5C%22?%3E%22 +%0A                %22%3Cderogations %22 + rootAttributesDeclaration + %22 %3E%22 +%0A                %22%3C/derogations%3E%22;%0A        return xml.getBytes(
 );%0A 

add test, xsi namespace must be declared.