@Override public boolean isEnabled(DbSession dbSession) { Optional<String> value = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.ORGANIZATION_ENABLED); return value.map("true"::equals).orElse(false); }
@Override public void enable(DbSession dbSession) { dbClient.internalPropertiesDao().save(dbSession, InternalProperties.ORGANIZATION_ENABLED, valueOf(true)); } }
@Override public boolean areWorkersPaused() { try (DbSession dbSession = dbClient.openSession(false)) { Optional<String> val = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.COMPUTE_ENGINE_PAUSE); return "true".equals(val.orElse(null)); } }
private void ensureServerIdIsValid(DbSession dbSession) { PropertyDto id = dbClient.propertiesDao().selectGlobalProperty(dbSession, SERVER_ID); checkState(id != null, "Property %s is missing in database", SERVER_ID); checkState(isNotEmpty(id.getValue()), "Property %s is empty in database", SERVER_ID); Optional<String> checksum = dbClient.internalPropertiesDao().selectByKey(dbSession, SERVER_ID_CHECKSUM); checkState(checksum.isPresent(), "Internal property %s is missing in database", SERVER_ID_CHECKSUM); checkState(checksum.get().equals(serverIdChecksum.computeFor(id.getValue())), "Server ID is invalid"); }
@Override public void write(String propertyKey, @Nullable String value) { checkPropertyKey(propertyKey); try (DbSession dbSession = dbClient.openSession(false)) { if (value == null || value.isEmpty()) { dbClient.internalPropertiesDao().saveAsEmpty(dbSession, propertyKey); } else { dbClient.internalPropertiesDao().save(dbSession, propertyKey, value); } dbSession.commit(); } }
@Override public Optional<String> read(String propertyKey) { checkPropertyKey(propertyKey); try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.internalPropertiesDao().selectByKey(dbSession, propertyKey); } }
@Override public void pauseWorkers() { try (DbSession dbSession = dbClient.openSession(false)) { dbClient.internalPropertiesDao().save(dbSession, InternalProperties.COMPUTE_ENGINE_PAUSE, "true"); dbSession.commit(); } }
@Before public void setUp() throws Exception { when(dbClient.openSession(false)).thenReturn(dbSession); when(dbClient.internalPropertiesDao()).thenReturn(internalPropertiesDao); }
@Test public void workers_pause_is_loaded_from_db() { when(dbClient.internalPropertiesDao().selectByKey(any(), eq(InternalProperties.COMPUTE_ENGINE_PAUSE))).thenReturn(Optional.of("true")); assertThat(getUnderTest().areWorkersPaused()).isTrue(); }
public PropertyDbTester verifyInternal(String key, @Nullable String expectedValue) { Optional<String> value = dbClient.internalPropertiesDao().selectByKey(dbSession, key); if (expectedValue == null) { assertThat(value).isEmpty(); } else { assertThat(value).hasValue(expectedValue); } return this; }
public PropertyDbTester insertEmptyInternal(String key) { dbClient.internalPropertiesDao().saveAsEmpty(dbSession, key); dbSession.commit(); return this; } }
public PropertyDbTester insertInternal(String key, String value) { dbClient.internalPropertiesDao().save(dbSession, key, value); dbSession.commit(); return this; }
@Test public void test_workers_paused() { when(dbClient.internalPropertiesDao().selectByKey(any(), eq(InternalProperties.COMPUTE_ENGINE_PAUSE))).thenReturn(Optional.of("true")); CeQueueGlobalSection underTest = new CeQueueGlobalSection(dbClient, workerCountProvider); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Workers Paused", true); } }
@Test public void get_returns_DefaultOrganization_populated_from_DB() { insertOrganization(ORGANIZATION_DTO_1, DATE_1); dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid()); dbSession.commit(); DefaultOrganization defaultOrganization = underTest.get(); assertThat(defaultOrganization.getUuid()).isEqualTo(ORGANIZATION_DTO_1.getUuid()); assertThat(defaultOrganization.getKey()).isEqualTo(ORGANIZATION_DTO_1.getKey()); assertThat(defaultOrganization.getName()).isEqualTo(ORGANIZATION_DTO_1.getName()); assertThat(defaultOrganization.getCreatedAt()).isEqualTo(DATE_1); assertThat(defaultOrganization.getUpdatedAt()).isEqualTo(DATE_1); }
private void insertInternalProperty(String key, String value) { db.getDbClient().internalPropertiesDao().save(db.getSession(), key, value); db.commit(); } }
@Test public void get_fails_with_ISE_if_default_organization_internal_property_is_empty() { dbClient.internalPropertiesDao().saveAsEmpty(dbSession, DEFAULT_ORGANIZATION); dbSession.commit(); expectISENoDefaultOrganizationUuid(); underTest.get(); }
@Test public void get_returns_new_DefaultOrganization_with_each_call_when_cache_is_not_loaded() { insertOrganization(ORGANIZATION_DTO_1, DATE_1); dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid()); dbSession.commit(); assertThat(underTest.get()).isNotSameAs(underTest.get()); }
@Test public void get_fails_with_ISE_if_default_organization_does_not_exist() { dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, "bla"); dbSession.commit(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Default organization with uuid 'bla' does not exist"); underTest.get(); }
@Test public void load_resets_thread_local_when_called_twice() { insertOrganization(ORGANIZATION_DTO_1, DATE_1); dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid()); dbSession.commit(); underTest.load(); DefaultOrganization org1 = underTest.get(); underTest.load(); DefaultOrganization org2 = underTest.get(); assertThat(org1).isNotSameAs(org2); }
private void insertDefaultOrganization() { defaultOrganization = OrganizationTesting.newOrganizationDto().setUuid(defaultOrganizationUuid); try (DbSession dbSession = db.getMyBatis().openSession(false)) { client.organizationDao().insert(dbSession, defaultOrganization, false); client.internalPropertiesDao().save(dbSession, "organization.default", defaultOrganization.getUuid()); dbSession.commit(); } }