-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
221 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tests/Aspire.CommunityToolkit.Hosting.Java.Tests/ContainerResourceCreationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
namespace Aspire.CommunityToolkit.Hosting.Java.Tests; | ||
public class ContainerResourceCreationTests | ||
{ | ||
[Fact] | ||
public void AddJavaAppBuilderShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = null!; | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp("java", new JavaAppContainerResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddSpringAppBuilderShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = null!; | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddSpringApp("spring", new JavaAppContainerResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddJavaAppNameShouldNotBeNullOrWhiteSpace() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp(null!, new JavaAppContainerResourceOptions())); | ||
Assert.Throws<ArgumentException>(() => builder.AddJavaApp("", new JavaAppContainerResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddSpringAppNameShouldNotBeNullOrWhiteSpace() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddSpringApp(null!, new JavaAppContainerResourceOptions())); | ||
Assert.Throws<ArgumentException>(() => builder.AddSpringApp("", new JavaAppContainerResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddJavaAppContainerImageNameShouldNotBeNullOrWhiteSpace() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp("java", new JavaAppContainerResourceOptions { ContainerImageName = null! })); | ||
Assert.Throws<ArgumentException>(() => builder.AddJavaApp("java", new JavaAppContainerResourceOptions { ContainerImageName = "" })); | ||
} | ||
|
||
[Fact] | ||
public void AddJavaAppContainerResourceOptionsShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp("java", null!)); | ||
} | ||
|
||
[Fact] | ||
public void AddSpringAppContainerResourceOptionsShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddSpringApp("spring", null!)); | ||
} | ||
|
||
[Fact] | ||
public async Task AddJavaAppContainerDetailsSetOnResource() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
var options = new JavaAppContainerResourceOptions | ||
{ | ||
ContainerImageName = "java-app", | ||
ContainerRegistry = "docker.io", | ||
ContainerImageTag = "latest", | ||
Port = 8080, | ||
TargetPort = 8080, | ||
OtelAgentPath = "path/to/otel", | ||
Args = ["arg1", "arg2"] | ||
}; | ||
|
||
builder.AddJavaApp("java", options); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<JavaAppContainerResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
Assert.Equal("java", resource.Name); | ||
|
||
Assert.True(resource.TryGetLastAnnotation(out ContainerImageAnnotation? imageAnnotations)); | ||
Assert.Equal(options.ContainerImageName, imageAnnotations.Image); | ||
Assert.Equal(options.ContainerRegistry, imageAnnotations.Registry); | ||
Assert.Equal(options.ContainerImageTag, imageAnnotations.Tag); | ||
|
||
Assert.True(resource.TryGetLastAnnotation(out EndpointAnnotation? httpEndpointAnnotations)); | ||
Assert.Equal(options.Port, httpEndpointAnnotations.Port); | ||
Assert.Equal(options.TargetPort, httpEndpointAnnotations.TargetPort); | ||
|
||
Assert.True(resource.TryGetLastAnnotation(out CommandLineArgsCallbackAnnotation? argsAnnotations)); | ||
CommandLineArgsCallbackContext context = new([]); | ||
await argsAnnotations.Callback(context); | ||
Assert.All(options.Args, arg => Assert.Contains(arg, context.Args)); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
tests/Aspire.CommunityToolkit.Hosting.Java.Tests/ExecutableResourceCreationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
namespace Aspire.CommunityToolkit.Hosting.Java.Tests; | ||
|
||
public class ExecutableResourceCreationTests | ||
{ | ||
[Fact] | ||
public void AddJavaAppBuilderShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = null!; | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp("java", Environment.CurrentDirectory, new JavaAppExecutableResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddSpringAppBuilderShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = null!; | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddSpringApp("spring", Environment.CurrentDirectory, new JavaAppExecutableResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddJavaAppNameShouldNotBeNullOrWhiteSpace() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp(null!, Environment.CurrentDirectory, new JavaAppExecutableResourceOptions())); | ||
Assert.Throws<ArgumentException>(() => builder.AddJavaApp("", Environment.CurrentDirectory, new JavaAppExecutableResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddSpringAppNameShouldNotBeNullOrWhiteSpace() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddSpringApp(null!, Environment.CurrentDirectory, new JavaAppExecutableResourceOptions())); | ||
Assert.Throws<ArgumentException>(() => builder.AddSpringApp("", Environment.CurrentDirectory, new JavaAppExecutableResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddJavaAppWorkingDirectoryShouldNotBeNullOrWhiteSpace() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp("java", null!, new JavaAppExecutableResourceOptions())); | ||
Assert.Throws<ArgumentException>(() => builder.AddJavaApp("java", "", new JavaAppExecutableResourceOptions())); | ||
} | ||
|
||
[Fact] | ||
public void AddJavaAppExecutableResourceOptionsShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddJavaApp("java", Environment.CurrentDirectory, null!)); | ||
} | ||
|
||
[Fact] | ||
public void AddSpringAppContainerResourceOptionsShouldNotBeNull() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => builder.AddSpringApp("spring", Environment.CurrentDirectory, null!)); | ||
} | ||
|
||
[Fact] | ||
public async Task AddJavaAppContainerDetailsSetOnResource() | ||
{ | ||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(); | ||
|
||
var options = new JavaAppExecutableResourceOptions | ||
{ | ||
ApplicationName = "test.jar", | ||
Args = ["--test"], | ||
OtelAgentPath = "otel-agent", | ||
Port = 8080 | ||
}; | ||
|
||
builder.AddJavaApp("java", Environment.CurrentDirectory, options); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<JavaAppExecutableResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
Assert.Equal("java", resource.Name); | ||
|
||
Assert.Equal(Environment.CurrentDirectory, resource.WorkingDirectory); | ||
Assert.Equal("java", resource.Command); | ||
|
||
Assert.True(resource.TryGetLastAnnotation(out EndpointAnnotation? httpEndpointAnnotations)); | ||
Assert.Equal(options.Port, httpEndpointAnnotations.Port); | ||
|
||
Assert.True(resource.TryGetLastAnnotation(out CommandLineArgsCallbackAnnotation? argsAnnotations)); | ||
CommandLineArgsCallbackContext context = new([]); | ||
await argsAnnotations.Callback(context); | ||
Assert.All(options.Args, arg => Assert.Contains(arg, context.Args)); | ||
Assert.Contains("-jar", context.Args); | ||
Assert.Contains(options.ApplicationName, context.Args); | ||
} | ||
} |