CandlepinContextListener extends GuiceResteasyBootstrapServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { log.info("Candlepin initializing context."); JSSProviderLoader.addProvider(); I18nManager.getInstance().setDefaultLocale(Locale.US); servletContext = sce.getServletContext(); try { log.info("Candlepin reading configuration."); config = readConfiguration(servletContext); } catch (ConfigurationException e) { log.error("Could not read configuration file. Aborting initialization.", e); throw new RuntimeException(e); } LoggingConfigurator.init(config); servletContext.setAttribute(CONFIGURATION_NAME, config); setCapabilities(config); log.debug("Candlepin stored config on context."); super.contextInitialized(sce); log.info("Candlepin context initialized."); }  @Override void contextInitialized(ServletContextEvent sce); @Override void withInjector(Injector injector); @Override void contextDestroyed(ServletContextEvent event); static final String CONFIGURATION_NAME; }
@Test public void contextInitialized() { when(config.getBoolean(eq(ConfigProperties.ACTIVEMQ_ENABLED))).thenReturn(true); prepareForInitialization(); listener.contextInitialized(evt); verify(hqlistener).contextInitialized(any(Injector.class)); verify(ctx).setAttribute(eq(CandlepinContextListener.CONFIGURATION_NAME), eq(config)); verify(configRead).verify(eq(ctx)); CandlepinCapabilities expected = new CandlepinCapabilities(); CandlepinCapabilities actual = CandlepinCapabilities.getCapabilities(); assertEquals(expected, actual); }
@Test public void blackListsCapabilities() { Set<String> testSet = new HashSet<>(); testSet.add("cores"); testSet.add("ram"); when(config.getSet(eq(ConfigProperties.HIDDEN_CAPABILITIES), isNull())).thenReturn(testSet); prepareForInitialization(); listener.contextInitialized(evt); CandlepinCapabilities expected = new CandlepinCapabilities(); expected.removeAll(testSet); CandlepinCapabilities actual = CandlepinCapabilities.getCapabilities(); assertEquals(expected, actual); }
@Test void keycloakCapabilityPresentWhenKeycloakEnabled() { when(config.getBoolean(eq(ConfigProperties.KEYCLOAK_AUTHENTICATION))).thenReturn(true); prepareForInitialization(); listener.contextInitialized(evt); CandlepinCapabilities expected = new CandlepinCapabilities(); expected.add("keycloak_auth"); CandlepinCapabilities actual = CandlepinCapabilities.getCapabilities(); assertEquals(expected, actual); }
@Test void keycloakCapabilityAbsentWhenKeycloakDisabled() { when(config.getBoolean(eq(ConfigProperties.KEYCLOAK_AUTHENTICATION))).thenReturn(false); prepareForInitialization(); listener.contextInitialized(evt); CandlepinCapabilities actual = CandlepinCapabilities.getCapabilities(); assertFalse(actual.contains("keycloak_auth"), "keycloak_auth present but not expected"); }
@Test public void activeMQDisabled() { when(config.getBoolean(eq(ConfigProperties.ACTIVEMQ_ENABLED))).thenReturn(false); prepareForInitialization(); listener.contextInitialized(evt); verifyNoMoreInteractions(hqlistener); }
@Test public void tharSheBlows() { listener = new CandlepinContextListener() { protected List<Module> getModules(ServletContext context) { return new LinkedList<>(); } protected Configuration readConfiguration(ServletContext context) throws ConfigurationException { throw new ConfigurationException("the ship is sinking"); } }; prepareForInitialization(); assertThrows(RuntimeException.class, () -> listener.contextInitialized(evt)); }
@Test void initializesCrlFile() { prepareForInitialization(); when(config.getString(ConfigProperties.CRL_FILE_PATH)) .thenReturn("/tmp/crlfile.crl"); File crlFile = new File(config.getString(ConfigProperties.CRL_FILE_PATH)); if (crlFile.exists()) { crlFile.delete(); } crlFile.deleteOnExit(); assertFalse(crlFile.exists()); listener.contextInitialized(evt); assertTrue(crlFile.exists()); }