@Test
    public void relevant_information_from_deprovisioned_hosts_are_merged_into_readded_host() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        Instant testStart = tester.nodeRepository().clock().instant();

        tester.clock().advance(Duration.ofSeconds(1));
        tester.addHost("id1", "host1", "default", NodeType.host);
        tester.addHost("id2", "host2", "default", NodeType.host);
        assertFalse(tester.nodeRepository().nodes().node("host1").get().history().hasEventAfter(History.Event.Type.deprovisioned, testStart));

        // Set host 1 properties and deprovision it
        try (var lock = tester.nodeRepository().nodes().lockAndGetRequired("host1")) {
            Node host1 = lock.node().withWantToRetire(true, true, Agent.system, tester.nodeRepository().clock().instant());
            host1 = host1.withFirmwareVerifiedAt(tester.clock().instant());
            host1 = host1.with(host1.status().withIncreasedFailCount());
            host1 = host1.with(host1.reports().withReport(Report.basicReport("id", Report.Type.HARD_FAIL, tester.clock().instant(), "Test report")));
            tester.nodeRepository().nodes().write(host1, lock);
        }
        tester.nodeRepository().nodes().removeRecursively("host1");

        // Set host 2 properties and deprovision it
        try (var lock = tester.nodeRepository().nodes().lockAndGetRequired("host2")) {
            Node host2 = lock.node().withWantToRetire(true, false, true, false, false, Agent.system, tester.nodeRepository().clock().instant());
            tester.nodeRepository().nodes().write(host2, lock);
        }
        tester.nodeRepository().nodes().removeRecursively("host2");

        // Host 1 is deprovisioned and unwanted properties are cleared
        Node host1 = tester.nodeRepository().nodes().node("host1").get();
        Node host2 = tester.nodeRepository().nodes().node("host2").get();
        assertEquals(Node.State.deprovisioned, host1.state());
        assertTrue(host1.history().hasEventAfter(History.Event.Type.deprovisioned, testStart));

        // Adding it again preserves some information from the deprovisioned host and removes it
        tester.addHost("id2", "host1", "default", NodeType.host);
        host1 = tester.nodeRepository().nodes().node("host1").get();
        assertEquals("This is the newly added node", "id2", host1.id());
        assertFalse("Not transferred from deprovisioned host", host1.status().wantToRetire());
        assertFalse("Not transferred from deprovisioned host", host1.status().wantToDeprovision());
        assertTrue("Transferred from deprovisioned host", host1.history().hasEventAfter(History.Event.Type.deprovisioned, testStart));
        assertTrue("Transferred from deprovisioned host", host1.status().firmwareVerifiedAt().isPresent());
        assertEquals("Transferred from deprovisioned host", 1, host1.status().failCount());
        assertEquals("Transferred from deprovisioned host", 1, host1.reports().getReports().size());
        assertTrue("Transferred from rebuilt host", host2.status().wantToRetire());
        assertTrue("Transferred from rebuilt host", host2.status().wantToRebuild());
    }
tests basic operation of the node repository

@author bratseth
node-repository/src/test/java/com/yahoo/vespa/hosted/provision/NodeRepositoryTest.java