Skip to content

Commit a2c80a3

Browse files
JAVA-3738 move end to end tests
1 parent 0b1e3a9 commit a2c80a3

File tree

7 files changed

+151
-102
lines changed

7 files changed

+151
-102
lines changed

gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastGradlePlugin.java

-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.gradle.api.Plugin;
44
import org.gradle.api.Project;
5-
import org.gradle.api.tasks.testing.Test;
65

76
/**
87
* Gradle plugin for contrast utilities. The goals for this plugin are defined here <a
@@ -19,18 +18,6 @@ public void apply(final Project target) {
1918
.getTasks()
2019
.register("hello", task -> task.doLast(s -> System.out.println("HelloWorld!")));
2120

22-
target
23-
.getTasks()
24-
.register("testWithContrast")
25-
.configure(
26-
task -> {
27-
target
28-
.getTasks()
29-
.withType(Test.class)
30-
.configureEach(s -> s.dependsOn("installAgent"));
31-
task.doFirst(s -> target.getTasks().withType(Test.class));
32-
});
33-
3421
target.getTasks().register("installAgent", InstallAgentTask.class);
3522
}
3623

gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/EmptyTask.java

-12
This file was deleted.

gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/InstallAgentTask.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static Path retrieveAgent(
184184
return downloadedAgent;
185185
}
186186

187-
/** Use ContrastSDK to download agent creds for running the agent */
187+
/** TODO Use ContrastSDK to download agent creds for running the agent */
188188
private void downloadAgentCredentials(final ContrastSDK connection) {}
189189

190190
/** Create ContrastSDK for connecting to TeamServer */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.contrastsecurity.gradle.plugin;
2+
3+
/** Utility class for retrieving Contrast API credentials for testing from Environment vars */
4+
public class EnvironmentUtils {
5+
6+
public static String getUsername() {
7+
return System.getenv("CONTRAST__API__USER_NAME");
8+
}
9+
10+
public static String getApiUrl() {
11+
return System.getenv("CONTRAST__API__URL");
12+
}
13+
14+
public static String getServiceKey() {
15+
return System.getenv("CONTRAST__API__SERVICE_KEY");
16+
}
17+
18+
public static String getApiKey() {
19+
return System.getenv("CONTRAST__API__API_KEY");
20+
}
21+
22+
public static String getOrgUuid() {
23+
return System.getenv("CONTRAST__API__ORGANIZATION_ID");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
package com.contrastsecurity.gradle.plugin;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
43
import static org.junit.jupiter.api.Assertions.assertTrue;
54

6-
import java.io.File;
7-
import java.io.FileWriter;
85
import java.io.IOException;
9-
import java.io.Writer;
10-
import java.util.Collection;
11-
import java.util.HashSet;
126
import org.gradle.testkit.runner.BuildResult;
137
import org.gradle.testkit.runner.GradleRunner;
14-
import org.gradle.testkit.runner.TaskOutcome;
15-
import org.junit.jupiter.api.Disabled;
168
import org.junit.jupiter.api.Test;
17-
import org.junit.jupiter.api.io.TempDir;
189

1910
/** Simple placeholder tests verifying tasks in the plugin run successfully */
20-
public class FunctionalTests {
21-
22-
@TempDir File projectDir;
23-
24-
private File getBuildFile() {
25-
return new File(projectDir, "build.gradle");
26-
}
27-
28-
private File getSettingsFile() {
29-
return new File(projectDir, "settings.gradle");
30-
}
31-
32-
private File fakeAgentFile() {
33-
return new File(projectDir, "agent.jar");
34-
}
11+
public class FunctionalTests extends GradleRunnerTest {
3512

3613
@Test
3714
void canRunTasks() throws IOException {
3815
writeString(getSettingsFile(), "");
39-
writeString(getBuildFile(), "plugins {" + " id('com.contrastsecurity.java')" + "}");
16+
writeString(getBuildFile(), writeConfig());
4017

4118
// Run the build
4219
final GradleRunner helloRunner = GradleRunner.create();
@@ -49,46 +26,4 @@ void canRunTasks() throws IOException {
4926
// Verify the result
5027
assertTrue(result.getOutput().contains("hello"));
5128
}
52-
53-
/** TODO parameterize test to use creds env vars and move to end to end tests */
54-
@Test
55-
@Disabled
56-
void attachesToTests() throws IOException {
57-
writeString(getSettingsFile(), "");
58-
writeString(getBuildFile(), "plugins {" + " id('com.contrastsecurity.java')" + "}\n");
59-
60-
final GradleRunner testRunner = GradleRunner.create();
61-
testRunner.forwardOutput();
62-
testRunner.withPluginClasspath();
63-
testRunner.withArguments("installAgent");
64-
testRunner.withProjectDir(projectDir);
65-
final BuildResult result = testRunner.build();
66-
67-
result
68-
.getTasks()
69-
.forEach(
70-
buildTask -> {
71-
assertEquals(buildTask.getOutcome(), TaskOutcome.SUCCESS);
72-
});
73-
74-
for (final String arg : AGENT_ARGS) {
75-
assertTrue(result.getOutput().contains(arg));
76-
}
77-
}
78-
79-
private void writeString(File file, String string) throws IOException {
80-
try (Writer writer = new FileWriter(file)) {
81-
writer.write(string);
82-
}
83-
}
84-
85-
private static final Collection<String> AGENT_ARGS = new HashSet<>();
86-
87-
static {
88-
AGENT_ARGS.add("-javaagent:gradle-plugin/src/test/resources/contrast-agent.jar");
89-
AGENT_ARGS.add("-Dcontrast.override.appname=extTest");
90-
AGENT_ARGS.add("-Dcontrast.server=server");
91-
AGENT_ARGS.add("-Dcontrast.env=qa");
92-
AGENT_ARGS.add("-Dcontrast.override.appversion=0.0.1");
93-
}
9429
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.contrastsecurity.gradle.plugin;
2+
3+
import java.io.File;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.io.Writer;
7+
import org.junit.jupiter.api.io.TempDir;
8+
9+
/**
10+
* Super class for Tests using the {@link org.gradle.testkit.runner.GradleRunner} to provide build
11+
* files and setting files
12+
*/
13+
public class GradleRunnerTest {
14+
@TempDir public File projectDir;
15+
16+
public File getBuildFile() {
17+
return new File(projectDir, "build.gradle");
18+
}
19+
20+
public File getSettingsFile() {
21+
return new File(projectDir, "settings.gradle");
22+
}
23+
24+
public void writeString(File file, String string) throws IOException {
25+
try (Writer writer = new FileWriter(file)) {
26+
writer.write(string);
27+
}
28+
}
29+
30+
public String writeConfig() {
31+
final String testConfig = "plugins { id('com.contrastsecurity.java') }\n";
32+
33+
return testConfig;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,112 @@
11
package com.contrastsecurity.gradle.plugin.e2e;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertNotNull;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

7+
import com.contrastsecurity.gradle.plugin.EnvironmentUtils;
8+
import com.contrastsecurity.gradle.plugin.GradleRunnerTest;
69
import com.contrastsecurity.gradle.plugin.InstallAgentTask;
710
import com.contrastsecurity.sdk.ContrastSDK;
811
import com.contrastsecurity.sdk.UserAgentProduct;
12+
import java.io.IOException;
913
import java.nio.file.Path;
14+
import java.util.Collection;
15+
import java.util.HashSet;
1016
import org.gradle.testfixtures.ProjectBuilder;
17+
import org.gradle.testkit.runner.BuildResult;
18+
import org.gradle.testkit.runner.GradleRunner;
19+
import org.gradle.testkit.runner.TaskOutcome;
1120
import org.junit.jupiter.api.Test;
1221

13-
public class EndToEndTests {
22+
public class EndToEndTests extends GradleRunnerTest {
1423

1524
@Test
1625
void verify_retrieval_of_agent_from_teamserver() {
17-
final String username = System.getenv("CONTRAST__API__USER_NAME");
18-
final String apiUrl = System.getenv("CONTRAST__API__URL");
19-
final String serviceKey = System.getenv("CONTRAST__API__SERVICE_KEY");
20-
final String apiKey = System.getenv("CONTRAST__API__API_KEY");
21-
final String orgUuid = System.getenv("CONTRAST__API__ORGANIZATION_ID");
2226

2327
final ContrastSDK connection =
24-
new ContrastSDK.Builder(username, serviceKey, apiKey)
25-
.withApiUrl(apiUrl)
28+
new ContrastSDK.Builder(
29+
EnvironmentUtils.getUsername(),
30+
EnvironmentUtils.getServiceKey(),
31+
EnvironmentUtils.getApiKey())
32+
.withApiUrl(EnvironmentUtils.getApiUrl())
2633
.withUserAgentProduct(UserAgentProduct.of("contrast-gradle-plugin"))
2734
.build();
2835
final Path agentPath =
29-
InstallAgentTask.retrieveAgent(connection, null, orgUuid, ProjectBuilder.builder().build());
36+
InstallAgentTask.retrieveAgent(
37+
connection, null, EnvironmentUtils.getOrgUuid(), ProjectBuilder.builder().build());
3038
assertNotNull(agentPath);
3139
assertTrue(agentPath.endsWith("contrast.jar"));
3240
}
41+
42+
@Test
43+
void verify_attaches_agent_to_tests() throws IOException {
44+
writeString(getSettingsFile(), "");
45+
String config = writeContrastBuildFile();
46+
writeString(getBuildFile(), config);
47+
48+
final GradleRunner testRunner = GradleRunner.create();
49+
testRunner.forwardOutput();
50+
testRunner.withPluginClasspath();
51+
testRunner.withArguments("installAgent");
52+
testRunner.withProjectDir(projectDir);
53+
final BuildResult result = testRunner.build();
54+
55+
result
56+
.getTasks()
57+
.forEach(
58+
buildTask -> {
59+
assertEquals(buildTask.getOutcome(), TaskOutcome.SUCCESS);
60+
});
61+
62+
for (final String arg : AGENT_ARGS) {
63+
assertTrue(result.getOutput().contains(arg));
64+
}
65+
}
66+
67+
private String writeContrastBuildFile() {
68+
return "plugins { id('com.contrastsecurity.java') }\n"
69+
+ "contrastConfiguration {\n"
70+
+ " username = "
71+
+ "'"
72+
+ EnvironmentUtils.getUsername()
73+
+ "'"
74+
+ "\n"
75+
+ " apiKey = "
76+
+ "'"
77+
+ EnvironmentUtils.getApiKey()
78+
+ "'"
79+
+ "\n"
80+
+ " serviceKey = "
81+
+ "'"
82+
+ EnvironmentUtils.getServiceKey()
83+
+ "'"
84+
+ "\n"
85+
+ " apiUrl = "
86+
+ "'"
87+
+ EnvironmentUtils.getApiUrl()
88+
+ "'"
89+
+ "\n"
90+
+ " orgUuid = "
91+
+ "'"
92+
+ EnvironmentUtils.getOrgUuid()
93+
+ "'"
94+
+ "\n"
95+
+ " appName = 'gradle-end-to-end-test'\n"
96+
+ " serverName = 'server1'\n"
97+
+ " appVersion = '0.0.1'\n"
98+
+ " attachToTests = true\n"
99+
+ "}\n"
100+
+ "tasks.register('fakeTask', org.gradle.api.tasks.testing.Test) { \nSystem.out.println('test') \n}";
101+
}
102+
103+
private static final Collection<String> AGENT_ARGS = new HashSet<>();
104+
105+
static {
106+
AGENT_ARGS.add("-javaagent:");
107+
AGENT_ARGS.add("-Dcontrast.override.appname=gradle-end-to-end-test");
108+
AGENT_ARGS.add("-Dcontrast.server=server");
109+
AGENT_ARGS.add("-Dcontrast.env=qa");
110+
AGENT_ARGS.add("-Dcontrast.override.appversion=0.0.1");
111+
}
33112
}

0 commit comments

Comments
 (0)