public void recover(RMState state)
      throws GeneralSecurityException, IOException {
    LOG.info("Recovering CA Certificate and Private Key");
    X509Certificate caCert = state.getProxyCAState().getCaCert();
    PrivateKey caPrivateKey = state.getProxyCAState().getCaPrivateKey();
    proxyCA.init(caCert, caPrivateKey);
    wasRecovered = true;
  }
@Test
  public void testRecover() throws Exception {
    ProxyCA proxyCA = spy(new ProxyCA());
    RMContext rmContext = mock(RMContext.class);
    RMStateStore rmStateStore = mock(RMStateStore.class);
    when(rmContext.getStateStore()).thenReturn(rmStateStore);
    ProxyCAManager proxyCAManager = new ProxyCAManager(proxyCA, rmContext);
    proxyCAManager.init(new YarnConfiguration());
    Assert.assertEquals(proxyCA, proxyCAManager.getProxyCA());
    verify(rmContext, times(0)).getStateStore();
    verify(rmStateStore, times(0)).storeProxyCACert(any(), any());
    verify(proxyCA, times(0)).init();
    Assert.assertNull(proxyCA.getCaCert());
    Assert.assertNull(proxyCA.getCaKeyPair());

    RMStateStore.RMState rmState = mock(RMStateStore.RMState.class);
    RMStateStore.ProxyCAState proxyCAState =
        mock(RMStateStore.ProxyCAState.class);
    // We need to use a real certificate + private key because of validation
    // so just grab them from another ProxyCA
    ProxyCA otherProxyCA = new ProxyCA();
    otherProxyCA.init();
    X509Certificate certificate = otherProxyCA.getCaCert();
    when(proxyCAState.getCaCert()).thenReturn(certificate);
    PrivateKey privateKey = otherProxyCA.getCaKeyPair().getPrivate();
    when(proxyCAState.getCaPrivateKey()).thenReturn(privateKey);
    when(rmState.getProxyCAState()).thenReturn(proxyCAState);
    proxyCAManager.recover(rmState);
    verify(proxyCA, times(1)).init(certificate, privateKey);
    Assert.assertEquals(certificate, proxyCA.getCaCert());
    Assert.assertEquals(privateKey, proxyCA.getCaKeyPair().getPrivate());

    proxyCAManager.start();
    verify(rmContext, times(1)).getStateStore();
    verify(rmStateStore, times(1)).storeProxyCACert(proxyCA.getCaCert(),
        proxyCA.getCaKeyPair().getPrivate());
    verify(proxyCA, times(0)).init();
    Assert.assertEquals(certificate, proxyCA.getCaCert());
    Assert.assertEquals(privateKey, proxyCA.getCaKeyPair().getPrivate());
  }