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.commons.ihe.hl7v3.iti55;
18  
19  import org.junit.Test;
20  import org.openehealth.ipf.commons.audit.codes.EventActionCode;
21  import org.openehealth.ipf.commons.audit.codes.EventIdCode;
22  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
23  import org.openehealth.ipf.commons.audit.codes.ParticipantObjectTypeCode;
24  import org.openehealth.ipf.commons.audit.model.AuditMessage;
25  import org.openehealth.ipf.commons.audit.model.TypeValuePairType;
26  import org.openehealth.ipf.commons.ihe.core.atna.AuditDataset.HumanUser;
27  import org.openehealth.ipf.commons.ihe.hl7v3.atna.HL7v3AuditorTestBase;
28  import org.openehealth.ipf.commons.ihe.hl7v3.audit.Hl7v3AuditDataset;
29  
30  import java.nio.charset.StandardCharsets;
31  import java.util.Base64;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNotNull;
35  import static org.openehealth.ipf.commons.ihe.core.atna.event.IHEAuditMessageBuilder.IHE_HOME_COMMUNITY_ID;
36  
37  /**
38   * @author Christian Ohr
39   */
40  public class Iti55AuditStrategyTest extends HL7v3AuditorTestBase {
41  
42      @Test
43      public void testServerSide() {
44          testRequest(true);
45      }
46  
47      @Test
48      public void testClientSide() {
49          testRequest(false);
50      }
51  
52      private void testRequest(boolean serverSide) {
53          Iti55AuditStrategy strategy = new Iti55AuditStrategy(serverSide);
54          Hl7v3AuditDataset auditDataset = getHl7v3AuditDataset(strategy);
55          AuditMessage auditMessage = makeAuditMessage(strategy, auditContext, auditDataset);
56  
57          assertNotNull(auditMessage);
58          auditMessage.validate();
59          assertCommonV3AuditAttributes(auditMessage,
60                  EventOutcomeIndicator.Success,
61                  EventIdCode.Query,
62                  EventActionCode.Execute,
63                  serverSide,
64                  serverSide);
65  
66          TypeValuePairType detail = auditMessage.findParticipantObjectIdentifications(poi -> ParticipantObjectTypeCode.System.equals(poi.getParticipantObjectTypeCode()))
67                  .get(0).getParticipantObjectDetails().get(0);
68          assertNotNull(detail);
69          assertEquals(IHE_HOME_COMMUNITY_ID, detail.getType());
70          assertEquals(HOME_COMMUNITY_ID, new String(Base64.getDecoder().decode(detail.getValue()), StandardCharsets.UTF_8));
71      }
72  
73      private Hl7v3AuditDataset getHl7v3AuditDataset(Iti55AuditStrategy strategy) {
74          Hl7v3AuditDataset auditDataset = strategy.createAuditDataset();
75          auditDataset.setEventOutcomeIndicator(EventOutcomeIndicator.Success);
76          // auditDataset.setLocalAddress(SERVER_URI);
77          auditDataset.setRemoteAddress(CLIENT_IP_ADDRESS);
78          auditDataset.setMessageId(MESSAGE_ID);
79          auditDataset.setPatientIds(PATIENT_IDS);
80          auditDataset.setSourceUserId(REPLY_TO_URI);
81          auditDataset.setDestinationUserId(SERVER_URI);
82          auditDataset.setRequestPayload(QUERY_PAYLOAD);
83          auditDataset.setPurposesOfUse(PURPOSES_OF_USE);
84          auditDataset.getHumanUsers().add(new HumanUser(USER_ID, USER_NAME, USER_ROLES));
85          auditDataset.setHomeCommunityId(HOME_COMMUNITY_ID);
86          return auditDataset;
87      }
88  }