The following class gets the certificate from venafi.. it it isnt available it uses the cache certificate.
@Configuration
public class VenafiConfig (
public static final String CACHE_KEYSTORE_KEY “keystore”;
@value("${venafi.enabled: true}")
public boolean venafiEnabled;
@Autowired
private VenafiProperties venafiProperties;
/
@Bean (name "keystoreService")
@Profile("cloud")
public CloudKeystoreService cloudkeystoreService(Environment env) throws InvalidKeystoreException {
CloudKeystoreService cloudKeystoreService = null;
if (venafiEnabled) {
try {
CachedKeyStore cachedKeyStore;
}
Credential credentials = venafiProperties.getVenafi().get(“domain”);
VenafiCertificateCacheResolver resolver = new VenafiCertificateCacheResolver (credentials.getUri(),
credentials.getUsername (), credentials.getPassword());
resolver.addCertificate (CACHE_KEYSTORE_KEY, credentials.getPolicypath());
cloudKeystoreService = new CloudKeystoreService().addCacheResolver(resolver). LoadCache();
cachedKeyStore= cloudKeystoreService.cache().get (CACHE_KEYSTORE_KEY);
if (null == cachedKeyStore) {
throw new RuntimeException (" Application KeyStore not found in venafi!");
}
log.info("Downloaded certificate from venafi.");
} catch (RuntimeException e) {
Log.warn("Couldnot download certificate from Venafi url.".
+ e.getMessage());
} else {
cloudKeystoreService = getVcapkeystore();
Log.info("***Venafi service is disabled. ***");
}
Return cloudKeystoreService
}
public CloudKeystoreService getVcapkeystore () throws CacheResolutionException, InvalidKeystoreException {
Log.info( Downloading certificate from vcap service...'));
CloudKeystoreService cloudKeystoreService = null;
Credential credentials = venafiProperties.getVenafi().get(“domain”);
Base64KeystoreCacheResolver resolver = new Base64KeystoreCacheResolver();
CachedKeyStore keystore= new CachedKeyStore (CACHE_KEYSTORE_KEY, credentials.getKeystorePwd(), credentials.getKeystoreFile());
resolver.addkeystore (keystore);
cloudKeystoreService = new CloudKeystoreService().addCacheResolver (resolver);
CachedKeyStore cachedKeyStore= cloudKeystoreService.LoadCache().cache().get (CACHE_KEYSTORE_KEY).makeDefault(true);
if (cachedKeyStore == null)
{
throw new RuntimeException ("Application KeyStore not found in vcap service!”);}
Log.info("Downloaded certificate from vcap service");
return cloudKeystoreService;
}
I am writing the following JUnit cases to increase the code coverage :
@Mock
private VenafiProperties venafiProperties;
@Mock
private Environment environment;
private VenafiConfig venafiConfig;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
venafiConfig = new VenafiConfig();
venafiConfig.venafiProperties = venafiProperties;
}
@Test
void testCloudKeystoreServicePbas_WhenVenafiEnabled() {
venafiConfig.venafiEnabled = true;
when(venafiProperties.getVenafi()).thenReturn(/* mocking properties*/);
CloudKeystoreService result = venafiConfig.cloudkeystoreService(environment);
// Assert
assertNotNull(result);
// Add more specific assertions
}
@Test
void testCloudKeystoreServicePbas_WhenVenafiDisabled() {
venafiConfig.venafiEnabled = false;
CloudKeystoreService result = venafiConfig.cloudkeystoreServi(environment);
// Assert
assertNull(result); // Since Venafi is disabled, the result should be null
}
@Test
void testGetVcapkeystore() {
when(venafiProperties.getVenafi()).thenReturn(/* mock Venafi properties */);
CloudKeystoreService result = venafiConfig.getVcapkeystore();
assertNotNull(result);
// Add more specific assertions
}
}
I keep getting a null pointer exception at
CloudKeystoreService result = venafiConfig.cloudkeystoreServi(environment);
How should I proceed with the test cases. I am new to JUnit testing
I have tried setting up mock environment and adding the properties but the funciton throws NP