forked from Luke-Sikina/picsure-search-refinement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConceptControllerTest.java
151 lines (119 loc) · 7.17 KB
/
ConceptControllerTest.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package edu.harvard.dbmi.avillach.dictionary.concept;
import edu.harvard.dbmi.avillach.dictionary.concept.model.CategoricalConcept;
import edu.harvard.dbmi.avillach.dictionary.concept.model.Concept;
import edu.harvard.dbmi.avillach.dictionary.concept.model.ContinuousConcept;
import edu.harvard.dbmi.avillach.dictionary.facet.Facet;
import edu.harvard.dbmi.avillach.dictionary.filter.Filter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@SpringBootTest(properties = {"concept.tree.max_depth=1"})
@ActiveProfiles("test")
class ConceptControllerTest {
@MockBean
ConceptService conceptService;
@Autowired
ConceptController subject;
@Test
void shouldListConcepts() {
List<Concept> expected = List.of(
new CategoricalConcept("/foo", "foo", "Foo", "my_dataset", "foo!", List.of(), true, "", null, Map.of()),
new CategoricalConcept("/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of()),
new ContinuousConcept("/foo//baz", "baz", "Baz", "my_dataset", "foo!", true, 0F, 100F, "", Map.of())
);
Filter filter =
new Filter(List.of(new Facet("questionare", "Questionare", "?", "Questionare", 1, null, "category", null)), "foo", List.of());
Mockito.when(conceptService.listConcepts(filter, Pageable.ofSize(10).withPage(1))).thenReturn(expected);
Mockito.when(conceptService.countConcepts(filter)).thenReturn(100L);
Page<Concept> actual = subject.listConcepts(filter, 1, 10).getBody();
Assertions.assertEquals(expected, actual.get().toList());
Assertions.assertEquals(100L, actual.getTotalElements());
}
@Test
void shouldGetConceptDetails() {
CategoricalConcept expected =
new CategoricalConcept("/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of());
Mockito.when(conceptService.conceptDetail("my_dataset", "/foo//bar")).thenReturn(Optional.of(expected));
ResponseEntity<Concept> actual = subject.conceptDetail("my_dataset", "/foo//bar");
Assertions.assertEquals(expected, actual.getBody());
Assertions.assertEquals(HttpStatus.OK, actual.getStatusCode());
}
@Test
void shouldNotGetConceptDetails() {
Mockito.when(conceptService.conceptDetail("my_dataset", "/foo//asdsad")).thenReturn(Optional.empty());
ResponseEntity<Concept> actual = subject.conceptDetail("my_dataset", "/foo//bar");
Assertions.assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
}
@Test
void shouldGetConceptTree() {
Concept fooBar =
new CategoricalConcept("/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of());
Concept fooBaz = new ContinuousConcept("/foo//baz", "baz", "Baz", "my_dataset", "foo!", true, 0F, 100F, "", Map.of());
CategoricalConcept foo =
new CategoricalConcept("/foo", "foo", "Foo", "my_dataset", "foo!", List.of(), true, "", List.of(fooBar, fooBaz), Map.of());
Mockito.when(conceptService.conceptTree("my_dataset", "/foo", 1)).thenReturn(Optional.of(foo));
ResponseEntity<Concept> actual = subject.conceptTree("my_dataset", "/foo", 1);
Assertions.assertEquals(HttpStatus.OK, actual.getStatusCode());
Assertions.assertEquals(foo, actual.getBody());
}
@Test
void shouldGetNotConceptTreeForLargeDepth() {
Concept fooBar =
new CategoricalConcept("/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of());
Concept fooBaz = new ContinuousConcept("/foo//baz", "baz", "Baz", "my_dataset", "foo!", true, 0F, 100F, "", Map.of());
CategoricalConcept foo =
new CategoricalConcept("/foo", "foo", "Foo", "my_dataset", "foo!", List.of(), true, "", List.of(fooBar, fooBaz), Map.of());
Mockito.when(conceptService.conceptTree("my_dataset", "/foo", 1)).thenReturn(Optional.of(foo));
// concept.tree.max_depth=1
ResponseEntity<Concept> actual = subject.conceptTree("my_dataset", "/foo//bar", 2);
Assertions.assertEquals(HttpStatus.BAD_REQUEST, actual.getStatusCode());
}
@Test
void shouldGetNotConceptTreeForNegativeDepth() {
Concept fooBar =
new CategoricalConcept("/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of());
Concept fooBaz = new ContinuousConcept("/foo//baz", "baz", "Baz", "my_dataset", "foo!", true, 0F, 100F, "", Map.of());
CategoricalConcept foo =
new CategoricalConcept("/foo", "foo", "Foo", "my_dataset", "foo!", List.of(), true, "", List.of(fooBar, fooBaz), Map.of());
Mockito.when(conceptService.conceptTree("my_dataset", "/foo", -1)).thenReturn(Optional.of(foo));
// concept.tree.max_depth=1
ResponseEntity<Concept> actual = subject.conceptTree("my_dataset", "/foo//bar", 2);
Assertions.assertEquals(HttpStatus.BAD_REQUEST, actual.getStatusCode());
}
@Test
void shouldNotGetConceptTreeWhenConceptDNE() {
Concept fooBar =
new CategoricalConcept("/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of());
Concept fooBaz = new ContinuousConcept("/foo//baz", "baz", "Baz", "my_dataset", "foo!", true, 0F, 100F, "", Map.of());
CategoricalConcept foo =
new CategoricalConcept("/foo", "foo", "Foo", "my_dataset", "foo!", List.of(), true, "", List.of(fooBar, fooBaz), Map.of());
Mockito.when(conceptService.conceptTree("my_dataset", "/foo", 1)).thenReturn(Optional.of(foo));
ResponseEntity<Concept> actual = subject.conceptTree("my_dataset", "/asdsadasd", 1);
Assertions.assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
}
@Test
void shouldDumpConcepts() {
Concept fooBar = new CategoricalConcept(
"/foo//bar", "bar", "Bar", "my_dataset", "foo!", List.of("a", "b"), true, "", List.of(), Map.of("key", "value")
);
Concept fooBaz = new ContinuousConcept("/foo//baz", "baz", "Baz", "my_dataset", "foo!", true, 0F, 100F, "", Map.of("key", "value"));
List<Concept> concepts = List.of(fooBar, fooBaz);
PageRequest page = PageRequest.of(0, 10);
Mockito.when(conceptService.listDetailedConcepts(new Filter(List.of(), "", List.of()), page)).thenReturn(concepts);
ResponseEntity<Page<Concept>> actual = subject.dumpConcepts(0, 10);
Assertions.assertEquals(concepts, actual.getBody().getContent());
Assertions.assertEquals(HttpStatus.OK, actual.getStatusCode());
}
}