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 |
|
package info.magnolia.rest.registry; |
35 |
|
|
36 |
|
import static info.magnolia.rest.registry.EndpointDefinitionRegistryEventType.*; |
37 |
|
import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf; |
38 |
|
import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException; |
39 |
|
import static java.util.Collections.*; |
40 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
41 |
|
import static org.hamcrest.Matchers.contains; |
42 |
|
import static org.junit.Assert.*; |
43 |
|
import static org.mockito.Mockito.*; |
44 |
|
|
45 |
|
import info.magnolia.config.registry.DefinitionMetadata; |
46 |
|
import info.magnolia.config.registry.DefinitionMetadataBuilder; |
47 |
|
import info.magnolia.config.registry.DefinitionProvider; |
48 |
|
import info.magnolia.config.registry.Registry; |
49 |
|
import info.magnolia.event.RecordingEventBus; |
50 |
|
import info.magnolia.module.ModuleRegistry; |
51 |
|
import info.magnolia.registry.RegistrationException; |
52 |
|
import info.magnolia.rest.EndpointDefinition; |
53 |
|
|
54 |
|
import java.util.Collections; |
55 |
|
import java.util.Set; |
56 |
|
|
57 |
|
import org.junit.Before; |
58 |
|
import org.junit.Test; |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (56) |
Complexity: 6 |
Complexity Density: 0.12 |
|
60 |
|
public class EndpointDefinitionRegistryTest { |
61 |
|
private EndpointDefinitionRegistry registry; |
62 |
|
private RecordingEventBus eventBus; |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
64 |
9 |
@Before... |
65 |
|
public void setUp() throws Exception { |
66 |
9 |
ModuleRegistry moduleRegistry = mock(ModuleRegistry.class); |
67 |
9 |
eventBus = new RecordingEventBus(); |
68 |
9 |
registry = new EndpointDefinitionRegistry(moduleRegistry, eventBus); |
69 |
|
|
70 |
9 |
EndpointDefinition endpoint1 = newEndpointDefinition("endpoint1", true); |
71 |
9 |
EndpointDefinition endpoint2 = newEndpointDefinition("endpoint2", true); |
72 |
9 |
EndpointDefinition empty = mock(EndpointDefinition.class); |
73 |
9 |
registry.register(mockDefinitionProvider("endpoint1", endpoint1)); |
74 |
9 |
registry.register(mockDefinitionProvider("endpoint2", endpoint2)); |
75 |
9 |
DefinitionProvider<EndpointDefinition> invalidProvider = mockDefinitionProvider("empty", empty); |
76 |
9 |
when(invalidProvider.isValid()).thenReturn(false); |
77 |
9 |
registry.register(invalidProvider); |
78 |
|
} |
79 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
80 |
|
@Test... |
81 |
|
public void getAllDefinitions() throws Exception { |
82 |
|
|
83 |
|
int size = registry.getAllDefinitions().size(); |
84 |
|
|
85 |
|
|
86 |
|
assertEquals(2, size); |
87 |
|
} |
88 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
89 |
|
@Test... |
90 |
|
public void getProvider() throws Exception { |
91 |
|
|
92 |
|
DefinitionProvider<EndpointDefinition> provider = registry.getProvider("endpoint1"); |
93 |
|
|
94 |
|
|
95 |
|
assertTrue(provider.isValid()); |
96 |
|
assertThat(provider.get().getName(), equalTo("endpoint1")); |
97 |
|
} |
98 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
99 |
|
@Test... |
100 |
|
public void getNonExistingProvider() throws Exception { |
101 |
|
assertThat(() -> { |
102 |
|
registry.getProvider("endpoint99"); |
103 |
|
}, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class))); |
104 |
|
} |
105 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
106 |
|
@Test... |
107 |
|
public void getInvalidProvider() throws Exception { |
108 |
|
DefinitionProvider<EndpointDefinition> provider = registry.getProvider("empty"); |
109 |
|
assertFalse(provider.isValid()); |
110 |
|
} |
111 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
112 |
|
@Test... |
113 |
|
@Deprecated |
114 |
|
public void getExistingWithDeprecatedApi() throws Exception { |
115 |
|
|
116 |
|
EndpointDefinition res = registry.getEndpointDefinition("endpoint1"); |
117 |
|
|
118 |
|
|
119 |
|
assertNotNull(res); |
120 |
|
assertThat(res.getName(), equalTo("endpoint1")); |
121 |
|
} |
122 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
123 |
|
@Test... |
124 |
|
@Deprecated |
125 |
|
public void getNonExistingWithDeprecatedApi() throws Exception { |
126 |
|
assertThat(() -> { |
127 |
|
registry.getEndpointDefinition("endpoint99"); |
128 |
|
}, throwsAnException(instanceOf(RegistrationException.class))); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void unregisterAndRegisterWhenEmpty() throws Exception { |
133 |
|
|
134 |
1 |
registry.unregisterAndRegister(registry.getAllMetadata(), emptyList()); |
135 |
1 |
DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint3", newEndpointDefinition("endpoint3", true)); |
136 |
1 |
DefinitionMetadata metadata3 = provider.getMetadata(); |
137 |
1 |
eventBus.getSentEvents().clear(); |
138 |
|
|
139 |
|
|
140 |
1 |
Set<DefinitionMetadata> registeredIds = registry.unregisterAndRegister(emptyList(), singletonList(provider)); |
141 |
|
|
142 |
|
|
143 |
1 |
assertThat(registeredIds, contains(metadata3)); |
144 |
1 |
assertEquals(1, eventBus.getSentEvents().size()); |
145 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
146 |
1 |
assertEquals(REGISTERED, event.getType()); |
147 |
1 |
assertEquals("endpoint3", event.getEndpointName()); |
148 |
1 |
assertEquals(provider, event.getEndpointDefinitionProvider()); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
public void register() throws Exception { |
153 |
|
|
154 |
1 |
DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint4", newEndpointDefinition("endpoint4", true)); |
155 |
1 |
eventBus.getSentEvents().clear(); |
156 |
|
|
157 |
|
|
158 |
1 |
registry.register(provider); |
159 |
|
|
160 |
|
|
161 |
1 |
assertTrue(registry.getProvider("endpoint4").isValid()); |
162 |
1 |
assertEquals(1, eventBus.getSentEvents().size()); |
163 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
164 |
1 |
assertEquals(REGISTERED, event.getType()); |
165 |
1 |
assertEquals("endpoint4", event.getEndpointName()); |
166 |
1 |
assertEquals(provider, event.getEndpointDefinitionProvider()); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
169 |
1 |
@Test... |
170 |
|
public void unregister() throws Exception { |
171 |
|
|
172 |
1 |
DefinitionProvider<EndpointDefinition> providerToRemove = registry.getProvider("endpoint2"); |
173 |
1 |
eventBus.getSentEvents().clear(); |
174 |
|
|
175 |
|
|
176 |
1 |
registry.unregisterAndRegister(singletonList(providerToRemove.getMetadata()), Collections.emptyList()); |
177 |
|
|
178 |
|
|
179 |
1 |
assertThat(() -> { |
180 |
1 |
registry.getProvider("endpoint2"); |
181 |
|
}, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class))); |
182 |
1 |
assertEquals(1, eventBus.getSentEvents().size()); |
183 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
184 |
1 |
assertEquals(UNREGISTERED, event.getType()); |
185 |
1 |
assertEquals("endpoint2", event.getEndpointName()); |
186 |
|
} |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
188 |
20 |
private static EndpointDefinition newEndpointDefinition(String name, boolean enabled) {... |
189 |
20 |
ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition(); |
190 |
20 |
endpointDefinition.setName(name); |
191 |
20 |
endpointDefinition.setEnabled(enabled); |
192 |
20 |
return endpointDefinition; |
193 |
|
} |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
195 |
29 |
private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(String endpointName, EndpointDefinition endpointDefinition) {... |
196 |
29 |
DefinitionProvider provider = mock(DefinitionProvider.class); |
197 |
29 |
DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId() |
198 |
|
.type(EndpointDefinitionRegistry.TYPE) |
199 |
|
.module("module") |
200 |
|
.name(endpointName) |
201 |
|
.relativeLocation("test/" + endpointName) |
202 |
|
.build(); |
203 |
29 |
when(provider.isValid()).thenReturn(true); |
204 |
29 |
when(provider.getMetadata()).thenReturn(metadata); |
205 |
29 |
when(provider.get()).thenReturn(endpointDefinition); |
206 |
29 |
return provider; |
207 |
|
} |
208 |
|
} |