hsheinblatt commented on code in PR #1361: URL: https://github.com/apache/knox/pull/1361#discussion_r3901311560
########## gateway-server/src/test/java/org/apache/knox/gateway/services/factory/DelegationPolicyServiceFactoryTest.java: ########## @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.knox.gateway.services.factory; + +import org.apache.commons.io.FileUtils; +import org.apache.knox.gateway.config.impl.GatewayConfigImpl; +import org.apache.knox.gateway.database.DatabaseType; +import org.apache.knox.gateway.services.GatewayServices; +import org.apache.knox.gateway.services.Service; +import org.apache.knox.gateway.services.ServiceType; +import org.apache.knox.gateway.services.knoxidf.delegation.EmptyDelegationPolicyService; +import org.apache.knox.gateway.services.knoxidf.delegation.JdbcDelegationPolicyService; +import org.apache.knox.gateway.services.knoxidf.delegation.PolicyCheckRequest; +import org.apache.knox.gateway.services.knoxidf.delegation.PolicyDecision; +import org.apache.knox.gateway.services.knoxidf.delegation.DelegationPolicyService; +import org.apache.knox.gateway.services.security.AliasService; +import org.apache.knox.gateway.services.topology.TopologyService; +import org.apache.knox.gateway.topology.Topology; +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.sql.DriverManager; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class DelegationPolicyServiceFactoryTest { + + private final DelegationPolicyServiceFactory factory = new DelegationPolicyServiceFactory(); + private final Map<String, String> options = new HashMap<>(); + private File tempDir; + private Service createdService; + + @BeforeClass + public static void setUpClass() throws Exception { + java.util.Locale.setDefault(java.util.Locale.US); + DriverManager.getConnection("jdbc:derby:memory:DelegationPolicyServiceFactoryTest_knoxidf;create=true").close(); + DriverManager.getConnection("jdbc:derby:memory:DelegationPolicyServiceFactoryTest_knoxidf_admin;create=true").close(); + } + + @After + public void tearDown() throws Exception { + if (createdService != null) { + createdService.stop(); + } + if (tempDir != null) { + FileUtils.forceDelete(tempDir); + } + } + + // ------------------------------------------------------------------ + // Empty (no KNOXIDF) cases + // ------------------------------------------------------------------ + + @Test + public void shouldSelectEmptyWhenNoTopologies() throws Exception { + final GatewayServices gws = servicesWithTopology(/* no topologies */); + final GatewayConfigImpl config = EasyMock.createNiceMock(GatewayConfigImpl.class); + EasyMock.replay(config); + createdService = factory.create(gws, ServiceType.DELEGATION_POLICY_SERVICE, config, options, ""); + assertTrue(createdService instanceof EmptyDelegationPolicyService); + } + + @Test + public void shouldSelectEmptyWhenNoKnoxIdfRole() throws Exception { + final GatewayServices gws = servicesWithTopology(topologyWithRole("KNOXSSO")); + final GatewayConfigImpl config = EasyMock.createNiceMock(GatewayConfigImpl.class); + EasyMock.replay(config); + createdService = factory.create(gws, ServiceType.DELEGATION_POLICY_SERVICE, config, options, ""); + assertTrue(createdService instanceof EmptyDelegationPolicyService); + } + + @Test + public void shouldHonorExplicitEmptyImplEvenWhenKnoxIdfIsDeployed() throws Exception { + final GatewayServices gws = servicesWithTopology(topologyWithRole("KNOXIDF")); + final GatewayConfigImpl config = EasyMock.createNiceMock(GatewayConfigImpl.class); + EasyMock.replay(config); + createdService = factory.create(gws, ServiceType.DELEGATION_POLICY_SERVICE, config, options, + EmptyDelegationPolicyService.class.getName()); + assertTrue(createdService instanceof EmptyDelegationPolicyService); + } + + // ------------------------------------------------------------------ + // JDBC when KNOXIDF deployed + // ------------------------------------------------------------------ + + @Test + public void shouldSelectJdbcWhenKnoxIdfDeployed() throws Exception { + tempDir = org.apache.knox.test.TestUtils.createTempDir(getClass().getName()); + final AliasService aliasService = EasyMock.createNiceMock(AliasService.class); + EasyMock.replay(aliasService); + + final TopologyService topologyService = EasyMock.createNiceMock(TopologyService.class); + EasyMock.expect(topologyService.getTopologies()).andReturn( + Collections.singletonList(topologyWithRole("KNOXIDF"))).anyTimes(); + EasyMock.replay(topologyService); + + final GatewayServices gws = EasyMock.createNiceMock(GatewayServices.class); + EasyMock.expect(gws.getService(ServiceType.TOPOLOGY_SERVICE)).andReturn(topologyService).anyTimes(); + EasyMock.expect(gws.getService(ServiceType.ALIAS_SERVICE)).andReturn(aliasService).anyTimes(); + EasyMock.replay(gws); + + final GatewayConfigImpl config = EasyMock.createNiceMock(GatewayConfigImpl.class); + EasyMock.expect(config.getDatabaseType()).andReturn(DatabaseType.DERBY.type()).anyTimes(); + EasyMock.expect(config.getDatabaseName()) + .andReturn("memory:" + getClass().getSimpleName() + "_knoxidf").anyTimes(); + EasyMock.expect(config.getDelegationServiceTokenTtlSec()).andReturn(3600).anyTimes(); + EasyMock.replay(config); Review Comment: added -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
