View Javadoc
1   /*
2    * Copyright 2018 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.validate.query;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_QUERY_PARAMETER_VALUE;
21  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_REQUIRED_QUERY_PARAMETER;
22  
23  import java.util.List;
24  
25  import org.junit.Test;
26  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
28  import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
29  import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
30  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
31  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryRegistryTransformer;
32  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
33  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
34  
35  public class StatusValidationTest {
36      private static final StatusValidation validator = new StatusValidation(QueryParameter.DOC_ENTRY_STATUS);
37  
38      @Test
39      public void validateStatusSuccess() {
40          validator.validate(validQueryRequestWithStatus());
41      }
42  
43      @Test
44      public void ignoreInvalidContentIfValidIsAlsoPresent() {
45          validator.validate(
46                  invalidQueryRequestWithStatus("('invalid', '" + AvailabilityStatus.APPROVED.getQueryOpcode() + "')"));
47      }
48  
49      @Test
50      public void onlyInvalidContentIsPresent() {
51          expectValidationError(INVALID_QUERY_PARAMETER_VALUE, invalidQueryRequestWithStatus("('invalid')"));
52      }
53  
54      @Test
55      public void noStatusIsPresent() {
56          expectValidationError(MISSING_REQUIRED_QUERY_PARAMETER, invalidQueryRequestWithStatus(null));
57      }
58  
59      private void expectValidationError(ValidationMessage expectedError, EbXMLAdhocQueryRequest request) {
60          try {
61              validator.validate(request);
62              fail("XDSMetaDataException expected");
63          } catch (XDSMetaDataException e) {
64              assertEquals(0, e.getValidationMessage().compareTo(expectedError));
65          }
66      }
67  
68      private EbXMLAdhocQueryRequest invalidQueryRequestWithStatus(String statusQuery) {
69          QueryRegistry createFindDocumentsQuery = SampleData.createFindDocumentsQuery();
70          EbXMLAdhocQueryRequest ebXML = new QueryRegistryTransformer().toEbXML(createFindDocumentsQuery);
71          List<String> slotValues = ebXML.getSlotValues(QueryParameter.DOC_ENTRY_STATUS.getSlotName());
72          slotValues.clear();
73          if (statusQuery != null)
74              slotValues.add(statusQuery);
75          return ebXML;
76      }
77      private EbXMLAdhocQueryRequest validQueryRequestWithStatus() {
78          QueryRegistry createFindDocumentsQuery = SampleData.createFindDocumentsQuery();
79          return new QueryRegistryTransformer().toEbXML(createFindDocumentsQuery);
80      }
81  }