View Javadoc
1   /*
2    * Copyright 2017 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.core.atna;
17  
18  import org.junit.After;
19  import org.junit.Before;
20  import org.openehealth.ipf.commons.audit.AuditContext;
21  import org.openehealth.ipf.commons.audit.DefaultAuditContext;
22  import org.openehealth.ipf.commons.audit.codes.*;
23  import org.openehealth.ipf.commons.audit.marshal.dicom.Current;
24  import org.openehealth.ipf.commons.audit.model.ActiveParticipantType;
25  import org.openehealth.ipf.commons.audit.model.AuditMessage;
26  import org.openehealth.ipf.commons.audit.model.ParticipantObjectIdentificationType;
27  import org.openehealth.ipf.commons.audit.queue.RecordingAuditMessageQueue;
28  import org.openehealth.ipf.commons.audit.types.ActiveParticipantRoleId;
29  import org.openehealth.ipf.commons.audit.types.EventId;
30  import org.openehealth.ipf.commons.audit.types.PurposeOfUse;
31  
32  import java.nio.charset.StandardCharsets;
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.Base64;
36  import java.util.List;
37  
38  import static org.junit.Assert.*;
39  
40  /**
41   * @author Dmytro Rud
42   */
43  public class AuditorTestBase {
44  
45      protected static final String REPLY_TO_URI = "http://141.44.162.126:8090/services/iti55-response";
46      protected static final String USER_ID = "alias<user@issuer>";
47      protected static final String USER_NAME = "Dr. Klaus-Peter Kohlrabi";
48      protected static final String QUERY_PAYLOAD = "<query><coffee /></query>";
49      protected static final String SERVER_URI = "http://www.icw.int/pxs/iti55-service";
50      protected static final String QUERY_ID = "queryIdExtension@queryIdRoot";
51      protected static final String MESSAGE_ID = "messageIdExtension@messageIdRoot";
52      protected static final String HOME_COMMUNITY_ID = "urn:oid:3.14.15.926";
53      protected static final String CLIENT_IP_ADDRESS = "141.44.162.126";
54      protected static final String SUBMISSION_SET_UUID = "6d334214-2a2e-43ef-a362-cbe6e77b91a0";
55      protected static final String[] PATIENT_IDS = new String[]{"1234^^^&1.2.3.4.5.6&ISO", "durak^^^&6.7.8.9.10&KRYSO"};
56  
57      protected static final PurposeOfUse[] PURPOSES_OF_USE = {
58              PurposeOfUse.of("12", "1.0.14265.1", "Law Enforcement"),
59              PurposeOfUse.of("13", "1.0.14265.1", "Something Else")};
60  
61      protected static final List<ActiveParticipantRoleId> USER_ROLES = Arrays.asList(
62          ActiveParticipantRoleId.of("ABC", "1.2.3.4.5", "Role_ABC"),
63          ActiveParticipantRoleId.of("DEF", "1.2.3.4.5.6", "Role_DEF"));
64  
65  
66      protected DefaultAuditContext auditContext;
67      protected RecordingAuditMessageQueue recorder;
68  
69      @Before
70      public void setUp() {
71          auditContext = new DefaultAuditContext();
72          recorder = new RecordingAuditMessageQueue();
73          auditContext.setAuditMessageQueue(recorder);
74      }
75  
76      @After
77      public void tearDown() {
78          recorder.clear();
79      }
80  
81  
82      protected void assertCommonAuditAttributes(AuditMessage auditMessage,
83                                               EventOutcomeIndicator eventOutcomeIndicator,
84                                               EventId eventId,
85                                               EventActionCode eventActionCode,
86                                               String sourceUserId,
87                                               String destinationUserId,
88                                               boolean serverSide,
89                                               boolean requiresPatient) {
90          assertEquals(eventOutcomeIndicator, auditMessage.getEventIdentification().getEventOutcomeIndicator());
91          assertEquals(eventActionCode, auditMessage.getEventIdentification().getEventActionCode());
92          assertEquals(eventId, auditMessage.getEventIdentification().getEventID());
93  
94          ActiveParticipantType human = auditMessage.getActiveParticipants().stream()
95                  .filter(apt -> apt.getUserName() != null)
96                  .findFirst().orElse(null);
97  
98          ActiveParticipantType source = auditMessage.getActiveParticipants().stream()
99                  .filter(apt -> ActiveParticipantRoleIdCode.Source == apt.getRoleIDCodes().get(0))
100                 .findFirst().orElseThrow(() -> new AssertionError("Expected source participant"));
101 
102         assertEquals(human == null, source.isUserIsRequestor());
103         assertEquals(sourceUserId, source.getUserID());
104         if (!serverSide) {
105             assertNotNull(source.getAlternativeUserID());
106         }
107 
108         ActiveParticipantType destination = auditMessage.getActiveParticipants().stream()
109                 .filter(apt -> ActiveParticipantRoleIdCode.Destination == apt.getRoleIDCodes().get(0))
110                 .findFirst().orElseThrow(() -> new AssertionError("Expected destination active participant"));
111         assertFalse(destination.isUserIsRequestor());
112         assertEquals(destinationUserId, destination.getUserID());
113         if (serverSide) {
114             assertNotNull(destination.getAlternativeUserID());
115         }
116 
117         if (requiresPatient) {
118             ParticipantObjectIdentificationType patient = auditMessage.getParticipantObjectIdentifications().stream()
119                     .filter(poi -> ParticipantObjectIdTypeCode.PatientNumber == poi.getParticipantObjectIDTypeCode())
120                     .findFirst().orElseThrow(() -> new AssertionError("Expected patient participant object"));
121 
122             assertEquals(PATIENT_IDS[0], patient.getParticipantObjectID());
123             assertEquals(ParticipantObjectTypeCode.Person, patient.getParticipantObjectTypeCode());
124             assertEquals(ParticipantObjectTypeCodeRole.Patient, patient.getParticipantObjectTypeCodeRole());
125 
126 
127             // Feeds
128             if (!patient.getParticipantObjectDetails().isEmpty()) {
129                 assertEquals(MESSAGE_ID, new String(
130                         Base64.getDecoder().decode(patient.getParticipantObjectDetails().get(0).getValue()),
131                         StandardCharsets.UTF_8));
132             }
133 
134             // Queries
135             if (patient.getParticipantObjectQuery() != null) {
136                 assertEquals(QUERY_PAYLOAD, new String(
137                         Base64.getDecoder().decode(patient.getParticipantObjectQuery()),
138                         StandardCharsets.UTF_8));
139             }
140         }
141 
142         /*
143         auditDataset.setUserName(USER_NAME);
144         auditDataset.getPurposesOfUse().addAll(NEW_PURPOSES_OF_USE);
145         auditDataset.getUserRoles().addAll(NEW_USER_ROLES);
146         */
147     }
148 
149     protected static <T extends AuditDataset> AuditMessage[] makeAuditMessages(AuditStrategySupport<T> auditStrategy, AuditContext auditContext, T auditDataset) {
150         return auditStrategy.makeAuditMessage(auditContext, auditDataset);
151     }
152 
153     protected static <T extends AuditDataset> AuditMessage makeAuditMessage(AuditStrategySupport<T> auditStrategy, AuditContext auditContext, T auditDataset) {
154         return makeAuditMessages(auditStrategy, auditContext, auditDataset)[0];
155     }
156 
157     protected static <T extends AuditDataset> String printAuditMessage(AuditStrategySupport<T> auditStrategy, AuditContext auditContext, T auditDataset) {
158         return Current.toString(makeAuditMessages(auditStrategy, auditContext, auditDataset)[0], true);
159     }
160 
161     protected static String printAuditMessage(AuditMessage auditMessage) {
162         return Current.toString(auditMessage, true);
163     }
164 }