use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.
the class WebComponentProviderTest method mockRequest.
private VaadinRequest mockRequest(boolean hasConfig) {
VaadinContext context = Mockito.mock(VaadinContext.class);
VaadinService service = Mockito.mock(VaadinService.class);
VaadinRequest request = Mockito.mock(VaadinRequest.class);
Mockito.when(request.getService()).thenReturn(service);
Mockito.when(service.getContext()).thenReturn(context);
WebComponentConfigurationRegistry registry = Mockito.mock(WebComponentConfigurationRegistry.class);
Mockito.when(context.getAttribute(Mockito.eq(WebComponentConfigurationRegistry.class), Mockito.any())).thenReturn(registry);
Mockito.when(registry.hasConfigurations()).thenReturn(hasConfig);
Mockito.when(request.getPathInfo()).thenReturn("/web-component/a-b.js");
return request;
}
use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.
the class WebComponentProviderTest method init.
@Before
public void init() {
MockitoAnnotations.initMocks(this);
// same code as used for local variables in
registry = setUpRegistry();
// some tests
Mockito.when(request.getService()).thenReturn(service);
Mockito.when(session.getService()).thenReturn(service);
Mockito.when(service.getContext()).thenReturn(context);
Mockito.when(context.getAttribute(WebComponentConfigurationRegistry.class)).then(invocationOnMock -> registry);
Mockito.when(context.getAttribute(eq(WebComponentConfigurationRegistry.class), any())).then(invocationOnMock -> registry);
Mockito.doAnswer(invocationOnMock -> registry = (WebComponentConfigurationRegistry) invocationOnMock.getArguments()[0]).when(context).setAttribute(any(WebComponentConfigurationRegistry.class));
VaadinService.setCurrent(service);
Mockito.when(service.getInstantiator()).thenReturn(new MockInstantiator());
Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
VaadinServletService service = Mockito.mock(VaadinServletService.class);
Mockito.doCallRealMethod().when(service).getContextRootRelativePath(Mockito.any());
Mockito.doCallRealMethod().when(service).getContextRootRelativePath(Mockito.any());
provider = new WebComponentProvider();
}
use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.
the class WebComponentProviderTest method setExporters_exportersHasSamePushDeclarations_pushIsSet.
@Test
public void setExporters_exportersHasSamePushDeclarations_pushIsSet() {
WebComponentConfigurationRegistry registry = setupConfigurations(ThemedComponentExporter.class, SameThemedComponentExporter.class);
Assert.assertEquals(PushMode.AUTOMATIC, registry.getEmbeddedApplicationAnnotation(Push.class).get().value());
}
use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.
the class WebComponentProviderTest method setupConfigurations.
@SuppressWarnings({ "unchecked", "rawtypes" })
@SafeVarargs
private final WebComponentConfigurationRegistry setupConfigurations(Class<? extends WebComponentExporter<? extends Component>>... exporters) {
WebComponentConfigurationRegistry registry = setUpRegistry();
final Set<Class<? extends WebComponentExporter<? extends Component>>> set = Stream.of(exporters).collect(Collectors.toSet());
WebComponentExporter.WebComponentConfigurationFactory factory = new WebComponentExporter.WebComponentConfigurationFactory();
Set<WebComponentConfiguration<? extends Component>> configurations = new HashSet<>();
for (Class<? extends WebComponentExporter<? extends Component>> exporter : exporters) configurations.add(factory.create(new DefaultWebComponentExporterFactory(exporter).create()));
registry.setConfigurations(configurations);
return registry;
}
use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.
the class WebComponentProvider method synchronizedHandleRequest.
@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
String pathInfo = request.getPathInfo();
final ComponentInfo componentInfo = new ComponentInfo(pathInfo);
if (!componentInfo.hasExtension()) {
LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request without extension " + "information (.js/.html) with request path {}", pathInfo);
return false;
}
if (componentInfo.getTag() == null) {
LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request for non-custom element with request path {}", pathInfo);
return false;
}
if (componentInfo.isHTML()) {
LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request for html component in npm" + " mode with request path {}", pathInfo);
return false;
}
WebComponentConfigurationRegistry registry = WebComponentConfigurationRegistry.getInstance(request.getService().getContext());
Optional<WebComponentConfiguration<? extends Component>> optionalWebComponentConfiguration = registry.getConfiguration(componentInfo.tag);
if (optionalWebComponentConfiguration.isPresent()) {
WebComponentConfiguration<? extends Component> webComponentConfiguration = optionalWebComponentConfiguration.get();
String generated;
Supplier<String> responder;
response.setContentType(CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8);
responder = () -> generateNPMResponse(webComponentConfiguration.getTag(), request, response);
if (cache == null) {
generated = responder.get();
} else {
generated = cache.computeIfAbsent(componentInfo.tag, moduleTag -> responder.get());
}
IOUtils.write(generated, response.getOutputStream(), StandardCharsets.UTF_8);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No web component for " + Optional.ofNullable(componentInfo.tag).orElse("<null>"));
}
return true;
}
Aggregations