-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCqlLibraryServiceTest.java
More file actions
115 lines (103 loc) · 4.01 KB
/
Copy pathCqlLibraryServiceTest.java
File metadata and controls
115 lines (103 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gov.cms.madie.madiefhirservice.services;
import gov.cms.madie.madiefhirservice.exceptions.CqlLibraryNotFoundException;
import gov.cms.madie.models.dto.CqlLibraryDto;
import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class CqlLibraryServiceTest {
@InjectMocks private CqlLibraryService cqlLibraryService;
@Mock private RestTemplate restTemplate;
@BeforeEach
void setup() {
ReflectionTestUtils.setField(
cqlLibraryService, "madieLibraryService", "http://test.libraries-url");
ReflectionTestUtils.setField(
cqlLibraryService, "librariesVersionedUri", "/cql-libraries/versioned");
}
@Test
void getLibraryReturnsLibrary() {
CqlLibraryDto theLibrary =
CqlLibraryDto.builder().cqlLibraryName("FHIRHelpers").version("4.0.001").build();
ResponseEntity<CqlLibraryDto> response = ResponseEntity.ok(theLibrary);
when(restTemplate.exchange(
any(URI.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
.thenReturn(response);
CqlLibraryDto output =
cqlLibraryService.getLibrary(
"FHIRHelpers",
"4.0.001",
Optional.empty(),
"OKTA_TOKEN",
CqlCompilerException.ErrorSeverity.Info);
assertThat(output, is(notNullValue()));
assertThat(output, is(equalTo(theLibrary)));
}
@Test
void getLibraryReturnsExceptionForLibraryNotFound() {
ResponseEntity<Object> response = ResponseEntity.notFound().build();
when(restTemplate.exchange(
any(URI.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
.thenReturn(response);
Exception ex =
assertThrows(
CqlLibraryNotFoundException.class,
() ->
cqlLibraryService.getLibrary(
"FHIRHelpers",
"4.0.001",
Optional.empty(),
"OKTA_TOKEN",
CqlCompilerException.ErrorSeverity.Info));
assertThat(
ex.getMessage(),
is(equalTo("Cannot find a CQL Library with name: FHIRHelpers, version: 4.0.001")));
}
@Test
void getLibraryReturnsNullForConflict() {
ResponseEntity<Object> response = ResponseEntity.status(HttpStatus.CONFLICT).build();
when(restTemplate.exchange(
any(URI.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
.thenReturn(response);
CqlLibraryDto output =
cqlLibraryService.getLibrary(
"FHIRHelpers",
"4.0.001",
Optional.empty(),
"OKTA_TOKEN",
CqlCompilerException.ErrorSeverity.Info);
assertThat(output, is(nullValue()));
}
@Test
void getLibraryReturnsNullForOkNoBody() {
ResponseEntity<Object> response = ResponseEntity.ok().build();
when(restTemplate.exchange(
any(URI.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
.thenReturn(response);
CqlLibraryDto output =
cqlLibraryService.getLibrary(
"FHIRHelpers",
"4.0.001",
Optional.empty(),
"OKTA_TOKEN",
CqlCompilerException.ErrorSeverity.Info);
assertThat(output, is(nullValue()));
}
}