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  
17  package org.openehealth.ipf.platform.camel.ihe.fhir.test;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import ca.uhn.fhir.rest.client.api.IGenericClient;
21  import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
22  import org.hl7.fhir.dstu3.model.CapabilityStatement;
23  import org.hl7.fhir.dstu3.model.OperationOutcome;
24  import org.hl7.fhir.instance.model.api.IBaseResource;
25  import org.junit.Assert;
26  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
27  import org.openehealth.ipf.commons.audit.model.AuditMessage;
28  import org.openehealth.ipf.commons.audit.queue.AbstractMockedAuditMessageQueue;
29  import org.openehealth.ipf.platform.camel.ihe.ws.StandardTestContainer;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   *
36   */
37  public class FhirTestContainer extends StandardTestContainer {
38  
39      protected static IGenericClient client;
40      protected static FhirContext context;
41  
42      protected static IGenericClient startClient(String base) {
43          context = FhirContext.forDstu3();
44          context.getRestfulClientFactory().setSocketTimeout(1000000); // for debugging
45          client = context.newRestfulGenericClient(base);
46          return client;
47      }
48  
49      public void assertConformance(String type) {
50          CapabilityStatement conf = client.capabilities().ofType(CapabilityStatement.class).execute();
51          assertEquals(1, conf.getRest().size());
52          CapabilityStatement.CapabilityStatementRestComponent component = conf.getRest().iterator().next();
53          assertTrue(component.getResource().stream()
54                  .map(CapabilityStatement.CapabilityStatementRestResourceComponent::getType)
55                  .anyMatch(type::equals));
56      }
57  
58      protected void assertAndRethrow(BaseServerResponseException e, OperationOutcome.IssueType issueType) {
59          // Check ATNA Audit
60          AbstractMockedAuditMessageQueue sender = getAuditSender();
61          assertEquals(1, sender.getMessages().size());
62          AuditMessage event = sender.getMessages().get(0);
63          assertEquals(
64                  EventOutcomeIndicator.MajorFailure,
65                  event.getEventIdentification().getEventOutcomeIndicator());
66          assertAndRethrowException(e, issueType);
67      }
68  
69      protected void assertAndRethrowException(BaseServerResponseException e, OperationOutcome.IssueType expectedIssue) {
70          // Hmm, I wonder if this could not be done automatically...
71          OperationOutcome oo = context.newXmlParser().parseResource(OperationOutcome.class, e.getResponseBody());
72          Assert.assertEquals(OperationOutcome.IssueSeverity.ERROR, oo.getIssue().get(0).getSeverity());
73          Assert.assertEquals(expectedIssue, oo.getIssue().get(0).getCode());
74  
75          // Check ATNA Audit
76          AbstractMockedAuditMessageQueue sender = getAuditSender();
77          Assert.assertEquals(1, sender.getMessages().size());
78          AuditMessage event = sender.getMessages().get(0);
79          Assert.assertEquals(EventOutcomeIndicator.MajorFailure,
80                  event.getEventIdentification().getEventOutcomeIndicator());
81  
82          throw e;
83      }
84  
85      // For quickly checking output
86      protected void printAsXML(IBaseResource resource) {
87          System.out.println(context.newXmlParser().setPrettyPrint(true).encodeResourceToString(resource));
88      }
89  }