1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.xds.core.validate.responses;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
21 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
22 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryResponse;
23 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
24 import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorInfo;
25 import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
26 import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
27 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
28 import org.openehealth.ipf.commons.ihe.xds.core.transform.responses.ResponseTransformer;
29 import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
30 import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.fail;
34 import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_18;
35 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_ERROR_CODE_IN_RESPONSE;
36 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_SEVERITY_IN_RESPONSE;
37 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_STATUS_IN_RESPONSE;
38
39
40
41
42
43 public class RegistryResponseValidatorTest {
44 private RegistryResponseValidator validator;
45 private Response response;
46 private ResponseTransformer transformer;
47
48 @Before
49 public void setUp() {
50 validator = new RegistryResponseValidator();
51 EbXMLFactory factory = new EbXMLFactory30();
52 transformer = new ResponseTransformer(factory);
53 response = SampleData.createResponse();
54 }
55
56 @Test
57 public void testGoodCase() throws XDSMetaDataException {
58 validator.validate(transformer.toEbXML(response), ITI_18);
59 }
60
61 @Test
62 public void testInvalidStatus() {
63 response.setStatus(null);
64 expectFailure(INVALID_STATUS_IN_RESPONSE);
65 }
66
67 @Test
68 public void testInvalidErrorCode() {
69 response.getErrors().add(new ErrorInfo(null, null, Severity.ERROR, null, null));
70 expectFailure(INVALID_ERROR_CODE_IN_RESPONSE);
71 }
72
73 @Test
74 public void testInvalidSeverity() {
75 EbXMLRegistryResponse ebXML = transformer.toEbXML(response);
76 ((RegistryResponseType)ebXML.getInternal()).getRegistryErrorList().getRegistryError().get(0).setSeverity("lol");
77 expectFailure(INVALID_SEVERITY_IN_RESPONSE, ebXML);
78 }
79
80 private void expectFailure(ValidationMessage expectedMessage) {
81 expectFailure(expectedMessage, transformer.toEbXML(response));
82 }
83
84 private void expectFailure(ValidationMessage expectedMessage, EbXMLRegistryResponse ebXMLRegistryResponse) {
85 try {
86 validator.validate(ebXMLRegistryResponse, ITI_18);
87 fail("Expected exception: " + XDSMetaDataException.class);
88 }
89 catch (XDSMetaDataException e) {
90 assertEquals(expectedMessage, e.getValidationMessage());
91 }
92 }
93 }