StatusResource { @GET @ApiOperation(value = "Status", notes = "Returns status of the server", authorizations = {}) @Produces({ MediaType.APPLICATION_JSON}) @SecurityHole(noAuth = true, anon = true) public StatusDTO status() { StatusCache statusCache = candlepinCache.getStatusCache(); StatusDTO cached = statusCache.getStatus(); if (cached != null) { return cached; } CandlepinCapabilities caps = CandlepinCapabilities.getCapabilities(); RulesSourceEnum rulesSource = this.jsProvider.getRulesSource(); boolean good = true; try { rulesCurator.getUpdatedFromDB(); } catch (Exception e) { log.error("Error checking database connection", e); good = false; } Mode mode = this.modeManager.getCurrentMode(); ModeChangeReason mcr = this.getOldestReason(this.modeManager.getModeChangeReasons()); good = good && (mode == Mode.NORMAL); StatusDTO status; if (keycloakEnabled) { AdapterConfig adapterConfig = keycloakConfig.getAdapterConfig(); status = new KeycloakStatusDTO() .setKeycloakResource(adapterConfig.getResource()) .setKeycloakAuthUrl(adapterConfig.getAuthServerUrl()) .setKeycloakRealm(adapterConfig.getRealm()); } else { status = new StatusDTO(); } status .setResult(good) .setVersion(version) .setRelease(release) .setStandalone(standalone) .setRulesVersion(jsProvider.getRulesVersion()) .setRulesSource(rulesSource != null ? rulesSource.toString() : null) .setMode(mode != null ? mode.toString() : null) .setModeReason(mcr != null ? mcr.toString() : null) .setModeChangeTime(mcr != null ? mcr.getTime() : null) .setManagerCapabilities(caps) .setTimeUTC(new Date()); statusCache.setStatus(status); return status; } @Inject  StatusResource(RulesCurator rulesCurator, Configuration config, JsRunnerProvider jsProvider,
        CandlepinCache candlepinCache, CandlepinModeManager modeManager,
        KeycloakConfiguration keycloakConfig); @GET @ApiOperation(value = "Status", notes = "Returns status of the server", authorizations = {}) @Produces({ MediaType.APPLICATION_JSON}) @SecurityHole(noAuth = true, anon = true) StatusDTO status();  }
@Test public void status() throws Exception { PrintStream ps = new PrintStream(new File(this.getClass() .getClassLoader().getResource("version.properties").toURI())); ps.println("version=${version}"); ps.println("release=${release}"); StatusResource sr = this.createResource(); StatusDTO s = sr.status(); ps.close(); assertNotNull(s); assertEquals("${release}", s.getRelease()); assertEquals("${version}", s.getVersion()); assertTrue(s.getResult()); }
@Test public void unknown() throws Exception { PrintStream ps = new PrintStream(new File(this.getClass() .getClassLoader().getResource("version.properties").toURI())); ps.println("foo"); StatusResource sr = this.createResource(); StatusDTO s = sr.status(); ps.close(); assertNotNull(s); assertEquals("Unknown", s.getRelease()); assertEquals("Unknown", s.getVersion()); assertTrue(s.getResult()); }
@Test public void testDBDown() throws Exception { PrintStream ps = new PrintStream(new File(this.getClass() .getClassLoader().getResource("version.properties").toURI())); ps.println("version=${version}"); ps.println("release=${release}"); when(rulesCurator.getUpdatedFromDB()).thenThrow(new RuntimeException()); StatusResource sr = this.createResource(); StatusDTO s = sr.status(); ps.close(); assertNotNull(s); assertEquals("${release}", s.getRelease()); assertEquals("${version}", s.getVersion()); assertFalse(s.getResult()); }
@Test public void simulateVersionFilter() throws Exception { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger srLogger = lc.getLogger(StatusResource.class); Appender mockapp = mock(Appender.class); srLogger.addAppender(mockapp); srLogger.setLevel(Level.DEBUG); ArgumentCaptor<LoggingEvent> message = ArgumentCaptor.forClass(LoggingEvent.class); PrintStream ps = new PrintStream(new File(this.getClass() .getClassLoader().getResource("version.properties").toURI())); ps.println("version=${version}"); ps.println("release=${release}"); StatusResource sr = this.createResource(); StatusDTO s = sr.status(); ps.close(); verify(mockapp, never()).doAppend(message.capture()); assertEquals("${release}", s.getRelease()); assertEquals("${version}", s.getVersion()); assertTrue(s.getResult()); assertFalse(s.getStandalone()); }
@Test public void keycloakParamsPresentWhenKeycloakActive() { when(config.getBoolean(eq(ConfigProperties.KEYCLOAK_AUTHENTICATION))).thenReturn(true); StatusResource sr = this.createResource(); StatusDTO s = sr.status(); assertTrue("not a keycloak-enabled status", s instanceof KeycloakStatusDTO); KeycloakStatusDTO keycloakStatus = (KeycloakStatusDTO) s; assertEquals("realm", keycloakStatus.getKeycloakRealm()); assertEquals("https: assertEquals("resource", keycloakStatus.getKeycloakResource()); }
@Test public void keycloakParamsMissingWhenKeycloakInactive() { when(config.getBoolean(eq(ConfigProperties.KEYCLOAK_AUTHENTICATION))).thenReturn(false); StatusResource sr = this.createResource(); StatusDTO s = sr.status(); assertFalse("is a keycloak-enabled status", s instanceof KeycloakStatusDTO); }