View Javadoc
1   /*
2    * Copyright 2016 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.iti65;
18  
19  import ca.uhn.fhir.context.FhirVersionEnum;
20  import org.hl7.fhir.dstu3.model.*;
21  import org.hl7.fhir.instance.model.api.IBaseResource;
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.openehealth.ipf.commons.ihe.fhir.IpfFhirServlet;
25  import org.openehealth.ipf.commons.ihe.fhir.iti65.Iti65Constants;
26  import org.openehealth.ipf.platform.camel.ihe.fhir.test.FhirTestContainer;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import java.security.MessageDigest;
31  import java.util.Date;
32  
33  /**
34   *
35   */
36  abstract class AbstractTestIti65 extends FhirTestContainer {
37  
38      private static final Logger LOG = LoggerFactory.getLogger(AbstractTestIti65.class);
39  
40      private static final String BINARY_FULL_URL = "urn:uuid:8da1cfcc-05db-4aca-86ad-82aa756a64bb";
41      private static final String REFERENCE_FULL_URL = "urn:uuid:8da1cfcc-05db-4aca-86ad-82aa756a64bc";
42      private static final String MANIFEST_FULL_URL = "urn:uuid:8da1cfcc-05db-4aca-86ad-82aa756a64bd";
43  
44      public static void startServer(String contextDescriptor) {
45          IpfFhirServlet servlet = new IpfFhirServlet(FhirVersionEnum.DSTU3);
46          startServer(servlet, contextDescriptor, false, DEMO_APP_PORT, "FhirServlet");
47          startClient(String.format("http://localhost:%d/", DEMO_APP_PORT));
48      }
49  
50      protected Bundle provideAndRegister() throws Exception {
51          Bundle bundle = new Bundle().setType(Bundle.BundleType.TRANSACTION);
52          bundle.getMeta().addProfile(Iti65Constants.ITI65_PROFILE);
53  
54          // Manifest
55  
56          DocumentManifest manifest = new DocumentManifest();
57          manifest.setStatus(Enumerations.DocumentReferenceStatus.CURRENT)
58                  .setCreated(new Date())
59                  .setDescription("description")
60                  .setSource("source")
61                  .setSubject(new Reference("Patient/a2"))
62                  .setId("id");
63          manifest.addContent()
64                  .setP(new Reference(REFERENCE_FULL_URL));
65          bundle.addEntry()
66                  .setFullUrl(MANIFEST_FULL_URL)
67                  .setRequest(
68                          new Bundle.BundleEntryRequestComponent()
69                                  .setMethod(Bundle.HTTPVerb.POST)
70                                  .setUrl("DocumentManifest"))
71                  .setResource(manifest);
72  
73          // Reference
74  
75          byte[] documentContent = "YXNkYXNkYXNkYXNkYXNk".getBytes();
76  
77          Date timestamp = new DateTime()
78                  .withDate(2013, 7, 1)
79                  .withTime(13, 11, 33, 0)
80                  .withZone(DateTimeZone.UTC).toDate();
81          DocumentReference reference = new DocumentReference();
82          reference.getMeta().setLastUpdated(timestamp);
83  
84          reference.setMasterIdentifier(
85                  new Identifier()
86                          .setSystem("urn:ietf:rfc:3986")
87                          .setValue("urn:oid:129.6.58.92.88336"))
88                  .setCreated(timestamp)
89                  .setIndexed(timestamp) // creation of document reference resource
90                  .setDescription("Physical")
91                  .setSubject(new Reference("Patient/a2"))
92                  .addAuthor(new Reference("Practitioner/a3"))
93                  .addAuthor(new Reference("Practitioner/a4"))
94                  .setStatus(Enumerations.DocumentReferenceStatus.CURRENT);
95  
96          reference.getType().addCoding()
97                  .setSystem("http://ihe.net/connectathon/classCodes")
98                  .setCode("History and Physical")
99                  .setDisplay("History and Physical");
100         reference.addContent()
101                 .setAttachment(
102                         new Attachment()
103                                 .setContentType("text/plain")
104                                 .setLanguage("en/us")
105                                 .setSize(documentContent.length)
106                                 .setHash(MessageDigest.getInstance("SHA-1").digest(documentContent))
107                                 .setUrl(BINARY_FULL_URL))
108                 .setFormat(new Coding("urn:oid:1.3.6.1.4.1.19376.1.2.3", "urn:ihe:pcc:handp:2008", null));
109         bundle.addEntry()
110                 .setFullUrl(REFERENCE_FULL_URL)
111                 .setRequest(
112                         new Bundle.BundleEntryRequestComponent()
113                                 .setMethod(Bundle.HTTPVerb.POST)
114                                 .setUrl("DocumentReference"))
115                 .setResource(reference);
116 
117         // Binary
118 
119         Binary binary = new Binary()
120                 .setContentType("text/plain")
121                 .setContent(documentContent);
122         binary.getMeta().setLastUpdated(timestamp);
123 
124         bundle.addEntry()
125                 .setFullUrl(BINARY_FULL_URL)
126                 .setRequest(new Bundle.BundleEntryRequestComponent()
127                         .setMethod(Bundle.HTTPVerb.POST)
128                         .setUrl("Binary"))
129                 .setResource(binary);
130 
131         return bundle;
132     }
133 
134     protected Bundle sendManually(Bundle bundle) {
135         return client.transaction().withBundle(bundle).execute();
136     }
137 
138     protected void printAsXML(IBaseResource resource) {
139         LOG.info(context.newXmlParser().setPrettyPrint(true).encodeResourceToString(resource));
140     }
141 
142 
143 }