/**
 * Copyright (C) 2013
 * by 52 North Initiative for Geospatial Open Source Software GmbH
 *
 * Contact: Andreas Wytzisk
 * 52 North Initiative for Geospatial Open Source Software GmbH
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * This program is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed WITHOUT ANY WARRANTY; even without the implied
 * WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program (see gnu-gpl v2.txt). If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or
 * visit the Free Software Foundation web page, http://www.fsf.org.
 */
package org.n52.sos.service.it.v2.soap;

import net.opengis.om.x20.OMObservationType;
import net.opengis.sos.x20.InsertResultTemplateDocument;
import net.opengis.sos.x20.InsertResultTemplateType;
import net.opengis.sos.x20.InsertResultTemplateType.ProposedTemplate;
import net.opengis.sos.x20.ResultTemplateType;
import net.opengis.swe.x20.DataRecordDocument;
import net.opengis.swe.x20.TextEncodingDocument;
import net.opengis.swe.x20.TextEncodingType;

import org.apache.xmlbeans.XmlException;
import org.junit.Test;

/**
 * Test for SOAP SOS 2.0 InsertResultTemplate request.
 *
 * @author Christian Autermann <c.autermann@52north.org>
 * @author Carsten Hollmann <c.hollmann@52north.org>
 * @since 4.0.0
 */
public class InsertResultTemplateTest extends AbstractSosV2SoapTest {
    @Override
    @Test
    public void missingServiceParameter() throws XmlException {
        InsertResultTemplateDocument insertResultTemplateDocument =
                getMinimalRequest();
        addVersionParameter(insertResultTemplateDocument
                .getInsertResultTemplate());
        missingServiceParameter(insertResultTemplateDocument
                .getInsertResultTemplate(), insertResultTemplateDocument);
    }

    @Override
    @Test
    public void emptyServiceParameter() throws XmlException {
        InsertResultTemplateDocument insertResultTemplateDocument =
                getMinimalRequest();
        addVersionParameter(insertResultTemplateDocument
                .getInsertResultTemplate());
        emptyServiceParameter(insertResultTemplateDocument
                .getInsertResultTemplate(), insertResultTemplateDocument);
    }

    @Test
    @Override
    public void invalidServiceParameter() throws XmlException {
        InsertResultTemplateDocument insertResultTemplateDocument =
                getMinimalRequest();
        addVersionParameter(insertResultTemplateDocument
                .getInsertResultTemplate());
        invalidServiceParameter(insertResultTemplateDocument
                .getInsertResultTemplate(), insertResultTemplateDocument);
    }

    protected TextEncodingDocument createTextEncoding() {
        TextEncodingDocument textEncodingDocument = TextEncodingDocument.Factory
                .newInstance();
        TextEncodingType textEncodingType = textEncodingDocument
                .addNewTextEncoding();
        textEncodingType.setTokenSeparator(",");
        textEncodingType.setBlockSeparator(";");
        return textEncodingDocument;
    }

    protected InsertResultTemplateDocument getMinimalRequest() {
        InsertResultTemplateDocument insertResultTemplateDocument =
                InsertResultTemplateDocument.Factory.newInstance();
        InsertResultTemplateType insertResultTemplateType =
                insertResultTemplateDocument.addNewInsertResultTemplate();
        ProposedTemplate proposedTemplate = insertResultTemplateType
                .addNewProposedTemplate();
        ResultTemplateType resultTemplate = proposedTemplate
                .addNewResultTemplate();
        resultTemplate.setOffering("offering");
        OMObservationType observationType = resultTemplate
                .addNewObservationTemplate().addNewOMObservation();
        observationType.setId("id");
        observationType.addNewPhenomenonTime().setNilReason("template");
        observationType.addNewResultTime().setHref("#");
        observationType.addNewProcedure();
        observationType.addNewObservedProperty();
        observationType.addNewFeatureOfInterest().setHref("#");
        observationType.addNewResult();
        TextEncodingDocument textEncodingDocument = createTextEncoding();
        resultTemplate.addNewResultEncoding().set(textEncodingDocument);
        DataRecordDocument dataRecordDocument = DataRecordDocument.Factory
                .newInstance();
        dataRecordDocument.addNewDataRecord().addNewField().setName("field");
        resultTemplate.addNewResultStructure().set(dataRecordDocument);
        return insertResultTemplateDocument;
    }
}

@@ -998,16 +998,51 @@
 .soap;%0A%0A
+import javax.xml.namespace.QName;%0A%0A
 import n
@@ -1344,32 +1344,81 @@
 RecordDocument;%0A
+import net.opengis.swe.x20.DataRecordType.Field;%0A
 import net.openg
@@ -1494,16 +1494,53 @@
 ingType;
+%0Aimport net.opengis.swe.x20.TextType;
 %0A%0Aimport
@@ -4937,16 +4937,30 @@
 %0A       
+ Field field =
  dataRec
@@ -5007,23 +5007,242 @@
 ld()
-.setName(%22field
+;%0A        field.setName(%22field%22);%0A        TextType text = (TextType)field.addNewAbstractDataComponent().substitute(new QName(%22http://www.opengis.net/swe/2.0%22, %22Text%22, %22swe%22), TextType.type);%0A        text.setDefinition(%22text-definition
 %22);%0A

Add Text element to DataRecord field.