<commit_before>// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.maintenance;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.session.PrepareParams;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.flags.InMemoryFlagSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class TenantsMaintainerTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void deleteTenantWithNoApplications() throws IOException {
        ManualClock clock = new ManualClock("2020-06-01T00:00:00");
        MaintainerTester tester = new MaintainerTester(clock, temporaryFolder);
        TenantRepository tenantRepository = tester.tenantRepository();
        ApplicationRepository applicationRepository = tester.applicationRepository();
        File applicationPackage = new File("src/test/apps/hosted");

        TenantName shouldBeDeleted = TenantName.from("to-be-deleted");
        TenantName shouldNotBeDeleted = TenantName.from("should-not-be-deleted");

        tenantRepository.addTenant(shouldBeDeleted);
        tenantRepository.addTenant(shouldNotBeDeleted);
        tenantRepository.addTenant(TenantRepository.HOSTED_VESPA_TENANT);

        tester.deployApp(applicationPackage, prepareParams(shouldNotBeDeleted));
        assertNotNull(tenantRepository.getTenant(shouldBeDeleted));
        assertNotNull(tenantRepository.getTenant(shouldNotBeDeleted));

        clock.advance(TenantsMaintainer.defaultTtlForUnusedTenant.plus(Duration.ofDays(1)));
        new TenantsMaintainer(applicationRepository, tester.curator(), new InMemoryFlagSource(), Duration.ofDays(1), clock).run();
        tenantRepository.updateTenants();

        // One tenant should now have been deleted
        assertNull(tenantRepository.getTenant(shouldBeDeleted));
        assertNotNull(tenantRepository.getTenant(shouldNotBeDeleted));

        // System tenants should not be deleted
        assertNotNull(tenantRepository.getTenant(TenantName.defaultName()));
        assertNotNull(tenantRepository.getTenant(TenantRepository.HOSTED_VESPA_TENANT));

        // Add tenant again and deploy
        tenantRepository.addTenant(shouldBeDeleted);
        tester.deployApp(applicationPackage, prepareParams(shouldBeDeleted));
    }

    private PrepareParams.Builder prepareParams(TenantName tenantName) {
        return new PrepareParams.Builder().applicationId(applicationId(tenantName));
    }

    private ApplicationId applicationId(TenantName tenantName) {
        return ApplicationId.from(tenantName, ApplicationName.from("foo"), InstanceName.defaultName());
    }

}<commit_msg>Remove unnecessary and misleading method call<commit_after>// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.maintenance;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.session.PrepareParams;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.flags.InMemoryFlagSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class TenantsMaintainerTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void deleteTenantWithNoApplications() throws IOException {
        ManualClock clock = new ManualClock("2020-06-01T00:00:00");
        MaintainerTester tester = new MaintainerTester(clock, temporaryFolder);
        TenantRepository tenantRepository = tester.tenantRepository();
        ApplicationRepository applicationRepository = tester.applicationRepository();
        File applicationPackage = new File("src/test/apps/hosted");

        TenantName shouldBeDeleted = TenantName.from("to-be-deleted");
        TenantName shouldNotBeDeleted = TenantName.from("should-not-be-deleted");

        tenantRepository.addTenant(shouldBeDeleted);
        tenantRepository.addTenant(shouldNotBeDeleted);
        tenantRepository.addTenant(TenantRepository.HOSTED_VESPA_TENANT);

        tester.deployApp(applicationPackage, prepareParams(shouldNotBeDeleted));
        assertNotNull(tenantRepository.getTenant(shouldBeDeleted));
        assertNotNull(tenantRepository.getTenant(shouldNotBeDeleted));

        clock.advance(TenantsMaintainer.defaultTtlForUnusedTenant.plus(Duration.ofDays(1)));
        new TenantsMaintainer(applicationRepository, tester.curator(), new InMemoryFlagSource(), Duration.ofDays(1), clock).run();

        // One tenant should now have been deleted
        assertNull(tenantRepository.getTenant(shouldBeDeleted));
        assertNotNull(tenantRepository.getTenant(shouldNotBeDeleted));

        // System tenants should not be deleted
        assertNotNull(tenantRepository.getTenant(TenantName.defaultName()));
        assertNotNull(tenantRepository.getTenant(TenantRepository.HOSTED_VESPA_TENANT));

        // Add tenant again and deploy
        tenantRepository.addTenant(shouldBeDeleted);
        tester.deployApp(applicationPackage, prepareParams(shouldBeDeleted));
    }

    private PrepareParams.Builder prepareParams(TenantName tenantName) {
        return new PrepareParams.Builder().applicationId(applicationId(tenantName));
    }

    private ApplicationId applicationId(TenantName tenantName) {
        return ApplicationId.from(tenantName, ApplicationName.from("foo"), InstanceName.defaultName());
    }

}
{'fraction_non_alphanumeric': 0.09175449473031617, 'fraction_numerical': 0.006819590824550527, 'mean_word_length': 4.792639138240575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149172', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1748, 'n_words': 312}