Skip to content

Commit

Permalink
APIM-7687 fix: enable trigger condition of policy in debug tab
Browse files Browse the repository at this point in the history
  • Loading branch information
vikrantgravitee committed Dec 5, 2024
1 parent f9acaa3 commit db12cee
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.gravitee.gateway.policy.PolicyManifest;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

/**
* A manager of Policy factories. It allows to select a particular policy factory depending on specific criteria tested against the {@link PolicyManifest}.
Expand Down Expand Up @@ -52,6 +53,23 @@ public PolicyFactoryManager(Class<? extends PolicyFactory> defaultPolicyFactoryC
defaultPolicyFactory = extractDefaultPolicyFactory(policyFactories);
}

/**
* Build the manager with a set of {@link PolicyFactory} and select {@param HttpFactoryPolicy} as fall back factory, you can exclude a PolicyFactory class thanks to {@param excludedPolicyFactoryClass}
* {@param defaultPolicyFactoryClass} the class that will operate as the default policy factory
* {@param excludedPolicyFactoryClass} the class to exclude from the {@param policiesFactories}. This one is particularly useful in the case of the Debug Mode, where we want to exclude the {@link HttpPolicyFactory} from the Spring injected list of {@link PolicyFactory}
* {@param policyFactories}
*/
public PolicyFactoryManager(
Class<? extends PolicyFactory> debugPolicyFactoryClass,
Class<? extends PolicyFactory> excludedPolicyFactoryClass,
Set<PolicyFactory> policyFactories
) {
this.defaultPolicyFactoryClass = debugPolicyFactoryClass;
this.policyFactories =
policyFactories.stream().filter(factory -> !factory.getClass().equals(excludedPolicyFactoryClass)).collect(Collectors.toSet());
defaultPolicyFactory = extractDefaultPolicyFactory(policyFactories);
}

/**
* Get the most appropriate {@link PolicyFactory} depending on the {@link PolicyManifest}
* @param manifest to select the appropriate {@link PolicyFactory}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.gravitee.gateway.debug.handlers.api.DebugApiReactorHandlerFactory;
import io.gravitee.gateway.debug.organization.event.DebugOrganizationEventListener;
import io.gravitee.gateway.debug.organization.reactor.DebugOrganizationReactorFactory;
import io.gravitee.gateway.debug.policy.impl.DebugPolicyFactory;
import io.gravitee.gateway.debug.policy.impl.PolicyDebugDecoratorFactoryCreator;
import io.gravitee.gateway.debug.reactor.DebugReactor;
import io.gravitee.gateway.debug.reactor.processor.DebugResponseProcessorChainFactory;
Expand Down Expand Up @@ -293,12 +294,12 @@ public DebugReactorEventListener debugReactorEventListener(

@Bean
public PolicyFactoryManager debugPolicyFactoryManager(Set<PolicyFactory> policyFactories) {
return new PolicyFactoryManager(policyFactories);
return new PolicyFactoryManager(DebugPolicyFactory.class, HttpPolicyFactory.class, policyFactories);
}

@Bean
public PolicyFactory debugPolicyFactory(final PolicyPluginFactory policyPluginFactory) {
return new HttpPolicyFactory(configuration, policyPluginFactory, new DebugExpressionLanguageConditionFilter());
return new DebugPolicyFactory(configuration, policyPluginFactory, new DebugExpressionLanguageConditionFilter());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.debug.policy.impl;

import io.gravitee.gateway.policy.PolicyPluginFactory;
import io.gravitee.gateway.reactive.core.condition.ExpressionLanguageConditionFilter;
import io.gravitee.gateway.reactive.policy.HttpConditionalPolicy;
import io.gravitee.gateway.reactive.policy.HttpPolicyFactory;
import io.gravitee.node.api.configuration.Configuration;

/**
* {@code DebugPolicyFactory} extends {@link HttpPolicyFactory} to provide a customizable point
* for adding debugging functionality for Debug Console. It inherits the behavior of
* the default policy factory.
*/
public class DebugPolicyFactory extends HttpPolicyFactory {

public DebugPolicyFactory(
Configuration configuration,
PolicyPluginFactory policyPluginFactory,
ExpressionLanguageConditionFilter<HttpConditionalPolicy> filter
) {
super(configuration, policyPluginFactory, filter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.debug.policy.impl;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.gravitee.gateway.policy.PolicyPluginFactory;
import io.gravitee.gateway.reactive.core.condition.ExpressionLanguageConditionFilter;
import io.gravitee.gateway.reactive.debug.policy.condition.DebugExpressionLanguageConditionFilter;
import io.gravitee.gateway.reactive.policy.HttpConditionalPolicy;
import io.gravitee.node.api.configuration.Configuration;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class DebugPolicyFactoryTest {

@Mock
private PolicyPluginFactory mockPolicyPluginFactory;

@Mock
private ExpressionLanguageConditionFilter<HttpConditionalPolicy> mockFilter;

@Mock
private Configuration configuration;

@InjectMocks
private DebugPolicyFactory debugPolicyFactory;

@Test
public void testConstructor_ShouldInstantiateCorrectly() {
DebugPolicyFactory debugPolicyFactory = new DebugPolicyFactory(
configuration,
mockPolicyPluginFactory,
new DebugExpressionLanguageConditionFilter()
);
// assert that the DebugPolicyFactory object is created successfully
assertNotNull(debugPolicyFactory, "DebugPolicyFactory should be instantiated correctly");
}
}

0 comments on commit db12cee

Please sign in to comment.