|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.test; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | +import static org.mockito.Mockito.any; |
| 13 | +import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import java.lang.reflect.Parameter; |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 22 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 23 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 24 | + |
| 25 | +import com.typesafe.config.Config; |
| 26 | +import io.jooby.Environment; |
| 27 | +import io.jooby.Jooby; |
| 28 | +import io.jooby.Server; |
| 29 | +import io.jooby.ServerOptions; |
| 30 | + |
| 31 | +class JoobyExtensionTest { |
| 32 | + |
| 33 | + private ExtensionContext context; |
| 34 | + private ExtensionContext.Store store; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + context = mock(ExtensionContext.class); |
| 39 | + store = mock(ExtensionContext.Store.class); |
| 40 | + |
| 41 | + when(context.getRequiredTestClass()).thenReturn((Class) Object.class); |
| 42 | + when(context.getStore(any())).thenReturn(store); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void shouldSupportJoobyParameter() throws ParameterResolutionException { |
| 47 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 48 | + Parameter parameter = mock(Parameter.class); |
| 49 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 50 | + when(parameter.getType()).thenReturn((Class) Jooby.class); |
| 51 | + |
| 52 | + JoobyExtension extension = new JoobyExtension(); |
| 53 | + assertTrue(extension.supportsParameter(parameterContext, context)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldSupportEnvironmentParameter() throws ParameterResolutionException { |
| 58 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 59 | + Parameter parameter = mock(Parameter.class); |
| 60 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 61 | + when(parameter.getType()).thenReturn((Class) Environment.class); |
| 62 | + |
| 63 | + JoobyExtension extension = new JoobyExtension(); |
| 64 | + assertTrue(extension.supportsParameter(parameterContext, context)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldSupportConfigParameter() throws ParameterResolutionException { |
| 69 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 70 | + Parameter parameter = mock(Parameter.class); |
| 71 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 72 | + when(parameter.getType()).thenReturn((Class) Config.class); |
| 73 | + |
| 74 | + JoobyExtension extension = new JoobyExtension(); |
| 75 | + assertTrue(extension.supportsParameter(parameterContext, context)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void shouldSupportServerPathParameter() throws ParameterResolutionException { |
| 80 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 81 | + Parameter parameter = mock(Parameter.class); |
| 82 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 83 | + when(parameter.getType()).thenReturn((Class) String.class); |
| 84 | + when(parameter.isNamePresent()).thenReturn(true); |
| 85 | + when(parameter.getName()).thenReturn("serverPath"); |
| 86 | + |
| 87 | + JoobyExtension extension = new JoobyExtension(); |
| 88 | + assertTrue(extension.supportsParameter(parameterContext, context)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void shouldSupportServerPortParameter() throws ParameterResolutionException { |
| 93 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 94 | + Parameter parameter = mock(Parameter.class); |
| 95 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 96 | + when(parameter.getType()).thenReturn((Class) int.class); |
| 97 | + when(parameter.isNamePresent()).thenReturn(true); |
| 98 | + when(parameter.getName()).thenReturn("serverPort"); |
| 99 | + |
| 100 | + JoobyExtension extension = new JoobyExtension(); |
| 101 | + assertTrue(extension.supportsParameter(parameterContext, context)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void shouldNotSupportUnknownParameter() throws ParameterResolutionException { |
| 106 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 107 | + Parameter parameter = mock(Parameter.class); |
| 108 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 109 | + when(parameter.getType()).thenReturn((Class) Object.class); |
| 110 | + |
| 111 | + JoobyExtension extension = new JoobyExtension(); |
| 112 | + assertFalse(extension.supportsParameter(parameterContext, context)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void shouldThrowExceptionWhenParameterNameIsMissing() { |
| 117 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 118 | + Parameter parameter = mock(Parameter.class); |
| 119 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 120 | + when(parameter.getType()).thenReturn((Class) String.class); |
| 121 | + when(parameter.isNamePresent()).thenReturn(false); |
| 122 | + |
| 123 | + JoobyExtension extension = new JoobyExtension(); |
| 124 | + |
| 125 | + IllegalStateException ex = |
| 126 | + assertThrows( |
| 127 | + IllegalStateException.class, |
| 128 | + () -> extension.supportsParameter(parameterContext, context)); |
| 129 | + assertTrue(ex.getMessage().contains("parameter names to be present")); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void shouldResolveJoobyParameter() throws ParameterResolutionException { |
| 134 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 135 | + Parameter parameter = mock(Parameter.class); |
| 136 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 137 | + when(parameter.getType()).thenReturn((Class) Jooby.class); |
| 138 | + |
| 139 | + Jooby app = new Jooby(); |
| 140 | + when(store.get("application", Jooby.class)).thenReturn(app); |
| 141 | + |
| 142 | + JoobyExtension extension = new JoobyExtension(); |
| 143 | + assertEquals(app, extension.resolveParameter(parameterContext, context)); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void shouldResolveEnvironmentParameter() throws ParameterResolutionException { |
| 148 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 149 | + Parameter parameter = mock(Parameter.class); |
| 150 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 151 | + when(parameter.getType()).thenReturn((Class) Environment.class); |
| 152 | + |
| 153 | + Jooby app = new Jooby(); |
| 154 | + Environment env = mock(Environment.class); |
| 155 | + app.setEnvironment(env); |
| 156 | + |
| 157 | + when(store.get("application", Jooby.class)).thenReturn(app); |
| 158 | + |
| 159 | + JoobyExtension extension = new JoobyExtension(); |
| 160 | + assertEquals(env, extension.resolveParameter(parameterContext, context)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void shouldResolveConfigParameter() throws ParameterResolutionException { |
| 165 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 166 | + Parameter parameter = mock(Parameter.class); |
| 167 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 168 | + when(parameter.getType()).thenReturn((Class) Config.class); |
| 169 | + |
| 170 | + Jooby app = new Jooby(); |
| 171 | + Environment env = mock(Environment.class); |
| 172 | + Config config = mock(Config.class); |
| 173 | + when(env.getConfig()).thenReturn(config); |
| 174 | + app.setEnvironment(env); |
| 175 | + |
| 176 | + when(store.get("application", Jooby.class)).thenReturn(app); |
| 177 | + |
| 178 | + JoobyExtension extension = new JoobyExtension(); |
| 179 | + assertEquals(config, extension.resolveParameter(parameterContext, context)); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void shouldResolveServerPathParameter() throws ParameterResolutionException { |
| 184 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 185 | + Parameter parameter = mock(Parameter.class); |
| 186 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 187 | + when(parameter.getType()).thenReturn((Class) String.class); |
| 188 | + when(parameter.isNamePresent()).thenReturn(true); |
| 189 | + when(parameter.getName()).thenReturn("serverPath"); |
| 190 | + |
| 191 | + Jooby app = new Jooby(); |
| 192 | + app.setContextPath("/app"); |
| 193 | + |
| 194 | + Server server = mock(Server.class); |
| 195 | + ServerOptions options = new ServerOptions(); |
| 196 | + options.setPort(8080); |
| 197 | + when(server.getOptions()).thenReturn(options); |
| 198 | + |
| 199 | + when(store.get("application", Jooby.class)).thenReturn(app); |
| 200 | + when(store.get("server", Server.class)).thenReturn(server); |
| 201 | + |
| 202 | + JoobyExtension extension = new JoobyExtension(); |
| 203 | + assertEquals( |
| 204 | + "http://localhost:8080/app", extension.resolveParameter(parameterContext, context)); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + void shouldResolveServerPortParameter() throws ParameterResolutionException { |
| 209 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 210 | + Parameter parameter = mock(Parameter.class); |
| 211 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 212 | + when(parameter.getType()).thenReturn((Class) int.class); |
| 213 | + when(parameter.isNamePresent()).thenReturn(true); |
| 214 | + when(parameter.getName()).thenReturn("serverPort"); |
| 215 | + |
| 216 | + Server server = mock(Server.class); |
| 217 | + ServerOptions options = new ServerOptions(); |
| 218 | + options.setPort(9090); |
| 219 | + when(server.getOptions()).thenReturn(options); |
| 220 | + |
| 221 | + when(store.get("server", Server.class)).thenReturn(server); |
| 222 | + |
| 223 | + JoobyExtension extension = new JoobyExtension(); |
| 224 | + assertEquals(9090, extension.resolveParameter(parameterContext, context)); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + void shouldThrowExceptionWhenServiceNotFoundInStoreOrParent() { |
| 229 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 230 | + Parameter parameter = mock(Parameter.class); |
| 231 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 232 | + when(parameter.getType()).thenReturn((Class) Jooby.class); |
| 233 | + |
| 234 | + when(store.get("application", Jooby.class)).thenReturn(null); |
| 235 | + when(context.getParent()).thenReturn(Optional.empty()); |
| 236 | + |
| 237 | + JoobyExtension extension = new JoobyExtension(); |
| 238 | + |
| 239 | + IllegalStateException ex = |
| 240 | + assertThrows( |
| 241 | + IllegalStateException.class, |
| 242 | + () -> extension.resolveParameter(parameterContext, context)); |
| 243 | + assertTrue(ex.getMessage().contains("Not found: Jooby")); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + void shouldResolveServiceFromParentContextIfMissingInCurrent() |
| 248 | + throws ParameterResolutionException { |
| 249 | + ParameterContext parameterContext = mock(ParameterContext.class); |
| 250 | + Parameter parameter = mock(Parameter.class); |
| 251 | + when(parameterContext.getParameter()).thenReturn(parameter); |
| 252 | + when(parameter.getType()).thenReturn((Class) Jooby.class); |
| 253 | + |
| 254 | + ExtensionContext parentContext = mock(ExtensionContext.class); |
| 255 | + ExtensionContext.Store parentStore = mock(ExtensionContext.Store.class); |
| 256 | + |
| 257 | + when(parentContext.getRequiredTestClass()).thenReturn((Class) Object.class); |
| 258 | + |
| 259 | + when(store.get("application", Jooby.class)).thenReturn(null); |
| 260 | + when(context.getParent()).thenReturn(Optional.of(parentContext)); |
| 261 | + when(parentContext.getStore(any())).thenReturn(parentStore); |
| 262 | + |
| 263 | + Jooby app = new Jooby(); |
| 264 | + when(parentStore.get("application", Jooby.class)).thenReturn(app); |
| 265 | + |
| 266 | + JoobyExtension extension = new JoobyExtension(); |
| 267 | + assertEquals(app, extension.resolveParameter(parameterContext, context)); |
| 268 | + } |
| 269 | +} |
0 commit comments