Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ protected void initialize() {
if (legacyMode) {
super.initialize();
} else {
// Unlike v4 resource, v2 Resource enabled flag has never been used. Keep this unchanged to avoid unexpected behavior or breaking changes.
reactable
.dependencies(Resource.class)
.stream()
.filter(Resource::isEnabled)
.forEach(resource -> {
log.debug("Loading resource {} for {}", resource.getName(), reactable);
final io.gravitee.resource.api.Resource resourceInstance = resourceLoader.load(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,11 @@ void should_not_ignore_when_resource_is_disabled() {
final ResourcePlugin resourcePlugin = mock(ResourcePlugin.class);
resource.setEnabled(false);

when(resourcePlugin.resource()).thenReturn(Fake.class);
when(reactable.dependencies(Resource.class)).thenReturn(Set.of(resource));
when(resourcePluginManager.get(resource.getType())).thenReturn(resourcePlugin);

cut.initialize();

assertThat(cut.containsResource(resource.getName())).isTrue();
assertThat(cut.getResource(resource.getName())).isExactlyInstanceOf(Fake.class);
assertThat(cut.containsResource(resource.getName())).isFalse();
assertThat(cut.getResource(resource.getName())).isNull();
}

@Test
Expand Down Expand Up @@ -240,6 +237,46 @@ void should_initialize_resource_that_use_deployment_context() {
assertThat(((FakeWithDeploymentContext) r).getDeploymentContext()).isNotNull().isSameAs(deploymentContext);
}

@Test
void should_initialize_enabled_resource_and_not_load_disabled_resource() {
final Resource enabledResource = buildResource();
enabledResource.setName("enabled-resource");
enabledResource.setEnabled(true);

final Resource disabledResource = buildResource();
disabledResource.setName("disabled-resource");
disabledResource.setType("test-disabled");
disabledResource.setEnabled(false);

final FakeConfiguration fakeConfiguration = new FakeConfiguration();
final ResourcePlugin resourcePlugin = mock(ResourcePlugin.class);

when(resourcePlugin.resource()).thenReturn(Fake.class);
when(reactable.dependencies(Resource.class)).thenReturn(Set.of(enabledResource, disabledResource));
when(resourcePluginManager.get(anyString())).thenReturn(resourcePlugin);
when(resourcePlugin.configuration()).thenReturn(FakeConfiguration.class);
when(resourceConfigurationFactory.create(FakeConfiguration.class, enabledResource.getConfiguration())).thenReturn(
fakeConfiguration
);

cut.initialize();

// Only enabled resource should be loaded due to .filter(Resource::isEnabled)
assertThat(cut.containsResource(enabledResource.getName())).isTrue();
assertThat(cut.containsResource(disabledResource.getName())).isFalse();

final Object enabledR = cut.getResource(enabledResource.getName());
assertThat(enabledR).isExactlyInstanceOf(Fake.class);
assertThat(((Fake) enabledR).configuration()).isSameAs(fakeConfiguration);

final Object disabledR = cut.getResource(disabledResource.getName());
assertThat(disabledR).isNull();

// Verify only the enabled resource was loaded
verify(resourcePluginManager).get(enabledResource.getType());
verify(resourcePluginManager, never()).get(disabledResource.getType());
}

private Resource buildResource() {
final Resource resource = new Resource();
resource.setType("test");
Expand Down