Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/git tests #212

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
SUBSERVICE_BOOKING_ISENABLED: true
SUBSERVICE_BOOKING_SERVICE_HOST: https://dev.booking.host.name
SUBSERVICE_BOOKING_SERVICE_PORT: 443
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish Coverage
if: success()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.github.gestalt.config.git;

import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

class GitConfigSourceBuilderTest {

private final Path localRepoDirectory = Path.of("/local/repo");
private final CredentialsProvider credentials = mock(CredentialsProvider.class);
private final SshSessionFactory sshSessionFactory = mock(SshSessionFactory.class);
private GitConfigSourceBuilder builder;

@BeforeEach
void setUp() {
builder = GitConfigSourceBuilder.builder();
}

@Test
void testSetRepoURI() {
assertNotNull(builder);
builder.setRepoURI("https://example.com/repo.git");
builder.setLocalRepoDirectory(localRepoDirectory);
builder.setConfigFilePath("config.yaml");
builder.setBranch("main");
builder.setCredentials(credentials);
builder.setSshSessionFactory(sshSessionFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.github.gestalt.config.source.ConfigSourcePackage;
import org.github.gestalt.config.tag.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -89,7 +90,6 @@ void hasStream() throws GestaltException, IOException {
}

@Test
@Disabled
void hasStreamWithPassword() throws GestaltException, IOException {
Path configDirectory = Files.createTempDirectory("gitConfigTest");
configDirectory.toFile().deleteOnExit();
Expand All @@ -98,6 +98,9 @@ void hasStreamWithPassword() throws GestaltException, IOException {
String userName = System.getenv("GIT_GESTALT_USER");
String password = System.getenv("GIT_GESTALT_PASSWORD");

Assumptions.assumeTrue(userName != null, "must have GIT_GESTALT_USER defined");
Assumptions.assumeTrue(password != null, "must have GIT_GESTALT_PASSWORD defined");

GitConfigSourceBuilder builder = GitConfigSourceBuilder.builder()
.setRepoURI("https://github.com/gestalt-config/gestalt.git")
.setConfigFilePath("gestalt-git/src/test/resources/default.properties")
Expand All @@ -114,6 +117,31 @@ void hasStreamWithPassword() throws GestaltException, IOException {
Assertions.assertTrue(configData.startsWith("hello=world"));
}

@Test
void hasStreamWithGithubToken() throws GestaltException, IOException {
Path configDirectory = Files.createTempDirectory("gitConfigTest");
configDirectory.toFile().deleteOnExit();

// Must set the git user and password in Env Vars
String githubToken = System.getenv("GITHUB_TOKEN");
Assumptions.assumeTrue(githubToken != null, "must have GITHUB_TOKEN defined");

GitConfigSourceBuilder builder = GitConfigSourceBuilder.builder()
.setRepoURI("https://github.com/gestalt-config/gestalt.git")
.setConfigFilePath("gestalt-git/src/test/resources/default.properties")
.setCredentials(new UsernamePasswordCredentialsProvider(githubToken, ""))
.setLocalRepoDirectory(configDirectory);
ConfigSourcePackage source = builder.build();

Assertions.assertTrue(source.getConfigSource().hasStream());
byte[] data = new byte[256];
source.getConfigSource().loadStream().read(data);

String configData = new String(data, StandardCharsets.UTF_8);

Assertions.assertTrue(configData.startsWith("hello=world"));
}

@Test
@Disabled
void hasStreamSSHWithPassword() throws GestaltException, IOException {
Expand All @@ -123,6 +151,7 @@ void hasStreamSSHWithPassword() throws GestaltException, IOException {
// Must set the git user and password in Env Vars
//String certLocation = System.getenv("GIT_GESTALT_SSH_LOCATION");
String password = System.getenv("GIT_GESTALT_SSH_PASSWORD");
Assumptions.assumeTrue(password != null, "must have GIT_GESTALT_SSH_PASSWORD defined");

Path sshDir = FS.DETECTED.userHome().toPath().resolve(".ssh");

Expand Down Expand Up @@ -271,11 +300,11 @@ void testEquals() throws IOException, GestaltException {
ConfigSourcePackage source = builder.build();
ConfigSourcePackage source2 = builder.build();

Assertions.assertEquals(source, source);
Assertions.assertNotEquals(source, source2);
Assertions.assertNotEquals(source, null);
Assertions.assertEquals(source.getConfigSource(), source.getConfigSource());
Assertions.assertNotEquals(source.getConfigSource(), source2.getConfigSource());
Assertions.assertNotEquals(source.getConfigSource(), null);

Assertions.assertNotEquals(source, 4);
Assertions.assertNotEquals(source.getConfigSource(), 4);

}

Expand All @@ -291,7 +320,7 @@ void testHashCode() throws IOException, GestaltException {
.setLocalRepoDirectory(configDirectory);
ConfigSourcePackage source = builder.build();

Assertions.assertTrue(source.hashCode() != 0);
Assertions.assertTrue(source.getConfigSource().hashCode() != 0);
}

@Test
Expand Down