first commit
This commit is contained in:
parent
1a1d011466
commit
be8bed47d3
|
@ -16,6 +16,18 @@
|
|||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-releasetrain</artifactId>
|
||||
<version>Moore-SR1</version>
|
||||
<scope>import</scope>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -26,6 +38,10 @@
|
|||
<artifactId>spring-vault-core</artifactId>
|
||||
<version>${spring.vault.core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-keyvalue</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
|
@ -38,7 +54,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.vault.core.version>2.1.1.RELEASE</spring.vault.core.version>
|
||||
<spring.vault.core.version>2.2.0.RELEASE</spring.vault.core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,7 +1,12 @@
|
|||
package com.baeldung.springvault;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.vault.repository.mapping.Secret;
|
||||
|
||||
@Secret(backend = "credentials", value = "myapp")
|
||||
public class Credentials {
|
||||
|
||||
@Id
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.springvault;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CredentialsRepository extends CrudRepository<Credentials, String> {
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.springvault;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -19,6 +20,9 @@ public class CredentialsService {
|
|||
@Autowired
|
||||
private VaultTemplate vaultTemplate;
|
||||
|
||||
@Autowired
|
||||
private CredentialsRepository credentialsRepository;
|
||||
|
||||
/**
|
||||
* To Secure Credentials
|
||||
* @param credentials
|
||||
|
@ -41,4 +45,14 @@ public class CredentialsService {
|
|||
return response.getData();
|
||||
}
|
||||
|
||||
public Credentials saveCredentials(Credentials credentials) {
|
||||
|
||||
return credentialsRepository.save(credentials);
|
||||
}
|
||||
|
||||
public Optional<Credentials> findById(String username) {
|
||||
|
||||
return credentialsRepository.findById(username);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,23 +1,26 @@
|
|||
package com.baeldung.springvault;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* These tests are requiring the {@code vault} command to be installed and available in the executing
|
||||
* platform. So, if you intend to run them in your environment, the please install the vault and then
|
||||
|
@ -33,6 +36,9 @@ public class VaultIntegrationTest {
|
|||
@Autowired
|
||||
private CredentialsService credentialsService;
|
||||
|
||||
@MockBean
|
||||
private CredentialsRepository credentialsRepository;
|
||||
|
||||
/**
|
||||
* Test to secure credentials.
|
||||
*
|
||||
|
@ -72,4 +78,41 @@ public class VaultIntegrationTest {
|
|||
assertEquals("password", credentials.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenCredentials_whenSave_thenReturnCredentials() {
|
||||
// Given
|
||||
Credentials credentials = new Credentials("login", "password");
|
||||
Mockito.when(credentialsRepository.save(credentials))
|
||||
.thenReturn(credentials);
|
||||
|
||||
// When
|
||||
Credentials savedCredentials = credentialsService.saveCredentials(credentials);
|
||||
|
||||
// Then
|
||||
assertNotNull(savedCredentials);
|
||||
assertEquals(savedCredentials.getUsername(), credentials.getUsername());
|
||||
assertEquals(savedCredentials.getPassword(), credentials.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenId_whenFindById_thenReturnCredentials() {
|
||||
// Given
|
||||
Credentials credentials = new Credentials("login", "p@ssw@rd");
|
||||
Mockito.when(credentialsRepository.findById("login"))
|
||||
.thenReturn(Optional.of(credentials));
|
||||
|
||||
// When
|
||||
Optional<Credentials> returnedCredentials = credentialsService.findById("login");
|
||||
|
||||
// Then
|
||||
assertNotNull(returnedCredentials);
|
||||
assertNotNull(returnedCredentials.get());
|
||||
assertEquals(returnedCredentials.get()
|
||||
.getUsername(), credentials.getUsername());
|
||||
assertEquals(returnedCredentials.get()
|
||||
.getPassword(), credentials.getPassword());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue