View Javadoc
1   /*
2    * Copyright 2009 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 lombok.Getter;
19  import lombok.extern.slf4j.Slf4j;
20  import org.openehealth.ipf.commons.audit.AuditContext;
21  import org.openehealth.ipf.commons.audit.marshal.dicom.Current;
22  import org.openehealth.ipf.commons.audit.model.AuditMessage;
23  import org.openehealth.ipf.commons.audit.queue.AbstractMockedAuditMessageQueue;
24  import org.openehealth.ipf.commons.xml.XsdValidator;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import static org.openehealth.ipf.commons.xml.XmlUtils.source;
31  
32  /**
33   * Mocked sender implementation for ATNA audit records.
34   * Records the messages to allow verification in tests and, optionally,
35   * validates them against the schema from DICOM Part 15 Appendix A.5.1.
36   *
37   * @author Jens Riemschneider
38   */
39  @Slf4j
40  public class MockedAuditMessageQueue implements AbstractMockedAuditMessageQueue {
41  
42      private static final String NEW_VALIDATION_SCHEMA = "/atna2.xsd";
43      private XsdValidator validator = new XsdValidator();
44      private final boolean needValidation;
45  
46      @Getter
47      List<AuditMessage> messages = Collections.synchronizedList(new ArrayList<>());
48  
49      public MockedAuditMessageQueue() {
50          this(true);
51      }
52  
53      public MockedAuditMessageQueue(boolean needValidation) {
54          super();
55          this.needValidation = needValidation;
56      }
57  
58      @Override
59      public void audit(AuditContext auditContext, AuditMessage... auditMessages) {
60          for (AuditMessage message : auditMessages) {
61              String s = Current.toString(message, true);
62              log.debug(s);
63              if (needValidation) {
64                  validator.validate(source(s), NEW_VALIDATION_SCHEMA);
65              }
66              messages.add(message);
67          }
68      }
69  
70      @Override
71      public void clear() {
72          messages.clear();
73      }
74  }