Skip to content
Permalink
Browse files
Merge pull request #10 from myrle-krantz/develop
Fixes necessary to make provisioner work in integration test with rhythm, identity, and portfolio
  • Loading branch information
myrle-krantz committed Jun 8, 2017
2 parents bf97a4a + c0324a0 commit 1234f16277b59317c00661057a9b4e75d74af4aa
Showing 19 changed files with 696 additions and 95 deletions.
@@ -1,3 +1,18 @@
/*
* Copyright 2017 The Mifos Initiative.
*
* 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.mifos.provisioner.api.v1.domain;

import io.mifos.core.test.domain.ValidationTest;
@@ -1,3 +1,18 @@
/*
* Copyright 2017 The Mifos Initiative.
*
* 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.mifos.provisioner.api.v1.domain;

import io.mifos.core.test.domain.ValidationTest;
@@ -1,3 +1,18 @@
/*
* Copyright 2017 The Mifos Initiative.
*
* 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.mifos.provisioner.api.v1.domain;

import io.mifos.core.test.domain.ValidationTest;
@@ -17,6 +17,7 @@

import io.mifos.core.test.env.TestEnvironment;
import io.mifos.provisioner.api.v1.client.Provisioner;
import io.mifos.provisioner.config.ProvisionerActiveMQProperties;
import io.mifos.provisioner.config.ProvisionerServiceConfig;
import org.junit.BeforeClass;
import org.junit.ClassRule;
@@ -36,7 +37,11 @@

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = {AbstractServiceTest.TestConfiguration.class})
classes = {AbstractServiceTest.TestConfiguration.class},
properties = {
ProvisionerActiveMQProperties.ACTIVEMQ_BROKER_URL_PROP + "=" + ProvisionerActiveMQProperties.ACTIVEMQ_BROKER_URL_DEFAULT,
ProvisionerActiveMQProperties.ACTIVEMQ_CONCURRENCY_PROP + "=" + ProvisionerActiveMQProperties.ACTIVEMQ_CONCURRENCY_DEFAULT}
)
public class AbstractServiceTest {
private static final String APP_NAME = "provisioner-v1";
private static final String CLIENT_ID = "sillyRabbit";
@@ -22,26 +22,24 @@
import io.mifos.provisioner.api.v1.domain.Tenant;

class Fixture {
static final String TENANT_NAME = "Comp Test";

static Tenant getCompTestTenant() {
final Tenant compTestTenant = new Tenant();
compTestTenant.setIdentifier(TestEnvironment.getRandomTenantName());
compTestTenant.setName(TENANT_NAME);
compTestTenant.setName("Comp Test");
compTestTenant.setDescription("Component Test Tenant");

final CassandraConnectionInfo cassandraConnectionInfo = new CassandraConnectionInfo();
compTestTenant.setCassandraConnectionInfo(cassandraConnectionInfo);
cassandraConnectionInfo.setClusterName("Test Cluster");
cassandraConnectionInfo.setContactPoints("127.0.0.1:9142");
cassandraConnectionInfo.setKeyspace("comp_test");
cassandraConnectionInfo.setKeyspace(compTestTenant.getIdentifier());
cassandraConnectionInfo.setReplicas("3");
cassandraConnectionInfo.setReplicationType("Simple");

final DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo();
compTestTenant.setDatabaseConnectionInfo(databaseConnectionInfo);
databaseConnectionInfo.setDriverClass("org.mariadb.jdbc.Driver");
databaseConnectionInfo.setDatabaseName("comp_test");
databaseConnectionInfo.setDatabaseName(compTestTenant.getIdentifier());
databaseConnectionInfo.setHost("localhost");
databaseConnectionInfo.setPort("3306");
databaseConnectionInfo.setUser("root");
@@ -15,14 +15,14 @@
*/
package io.mifos.provisioner.tenant;

import com.google.gson.Gson;
import io.mifos.anubis.api.v1.client.Anubis;
import io.mifos.anubis.api.v1.domain.AllowedOperation;
import io.mifos.anubis.api.v1.domain.ApplicationSignatureSet;
import io.mifos.anubis.api.v1.domain.PermittableEndpoint;
import io.mifos.anubis.api.v1.domain.Signature;
import io.mifos.anubis.provider.SystemRsaKeyProvider;
import io.mifos.anubis.test.v1.SystemSecurityEnvironment;
import io.mifos.anubis.token.TokenSerializationResult;
import io.mifos.core.api.context.AutoSeshat;
import io.mifos.core.api.util.ApiConstants;
import io.mifos.core.api.util.ApiFactory;
@@ -33,14 +33,17 @@
import io.mifos.identity.api.v1.domain.CallEndpointSet;
import io.mifos.identity.api.v1.domain.Permission;
import io.mifos.identity.api.v1.domain.PermittableGroup;
import io.mifos.identity.api.v1.events.ApplicationSignatureEvent;
import io.mifos.permittedfeignclient.api.v1.client.ApplicationPermissionRequirements;
import io.mifos.permittedfeignclient.api.v1.domain.ApplicationPermission;
import io.mifos.provisioner.ProvisionerCassandraInitializer;
import io.mifos.provisioner.ProvisionerMariaDBInitializer;
import io.mifos.provisioner.api.v1.client.Provisioner;
import io.mifos.provisioner.api.v1.domain.*;
import io.mifos.provisioner.config.ProvisionerActiveMQProperties;
import io.mifos.provisioner.config.ProvisionerConstants;
import io.mifos.provisioner.config.ProvisionerServiceConfig;
import io.mifos.provisioner.internal.listener.IdentityListener;
import io.mifos.provisioner.internal.service.applications.ApplicationCallContextProvider;
import io.mifos.provisioner.internal.util.TokenProvider;
import org.junit.*;
@@ -66,7 +69,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
@@ -75,7 +77,11 @@
* @author Myrle Krantz
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = {
ProvisionerActiveMQProperties.ACTIVEMQ_BROKER_URL_PROP + "=" + ProvisionerActiveMQProperties.ACTIVEMQ_BROKER_URL_DEFAULT,
ProvisionerActiveMQProperties.ACTIVEMQ_CONCURRENCY_PROP + "=" + ProvisionerActiveMQProperties.ACTIVEMQ_CONCURRENCY_DEFAULT}
)
public class TestTenantApplicationAssignment {
private static final String APP_NAME = "provisioner-v1";
private static final String CLIENT_ID = "sillyRabbit";
@@ -135,6 +141,12 @@ public TokenProvider tokenProviderSpy(final @Qualifier("tokenProvider") TokenPro
@Autowired
protected SystemRsaKeyProvider systemRsaKeyProvider;

@Autowired
protected IdentityListener identityListener;

@Autowired
protected Gson gson;

private AutoSeshat autoSeshat;

public TestTenantApplicationAssignment() {
@@ -164,17 +176,6 @@ public void after() throws InterruptedException {
autoSeshat.close();
}

private static class TokenChecker implements Answer<TokenSerializationResult> {
TokenSerializationResult result = null;

@Override
public TokenSerializationResult answer(final InvocationOnMock invocation) throws Throwable {
result = (TokenSerializationResult) invocation.callRealMethod();
Assert.assertNotNull(result);
return result;
}
}

private class VerifyIsisInitializeContext implements Answer<ApplicationSignatureSet> {

private final String keyTimestamp;
@@ -211,6 +212,10 @@ public ApplicationSignatureSet answer(final InvocationOnMock invocation) throws
return ret;
}

String getKeyTimestamp() {
return keyTimestamp;
}

boolean isValidSecurityContext() {
return validSecurityContext;
}
@@ -314,13 +319,103 @@ boolean isValidSecurityContext() {
}
}

private class VerifyIsisCreatePermittableGroup implements Answer<Void> {

private boolean validSecurityContext = true;
private final String tenantIdentifier;
private int callCount = 0;

private VerifyIsisCreatePermittableGroup(final String tenantIdentifier) {
this.tenantIdentifier = tenantIdentifier;
}

@Override
public Void answer(final InvocationOnMock invocation) throws Throwable {
final boolean validSecurityContextForThisCall = systemSecurityEnvironment.isValidSystemSecurityContext("identity", "1", tenantIdentifier);
validSecurityContext = validSecurityContext && validSecurityContextForThisCall;
callCount++;

final PermittableGroup arg = invocation.getArgumentAt(0, PermittableGroup.class);
identityListener.onCreatePermittableGroup(tenantIdentifier, arg.getIdentifier());
return null;
}

boolean isValidSecurityContext() {
return validSecurityContext;
}

int getCallCount() {
return callCount;
}
}

private class VerifyIsisSetApplicationSignature implements Answer<Void> {

private boolean validSecurityContext = false;
private final String tenantIdentifier;

private VerifyIsisSetApplicationSignature(final String tenantIdentifier) {
this.tenantIdentifier = tenantIdentifier;
}

@Override
public Void answer(final InvocationOnMock invocation) throws Throwable {
validSecurityContext = systemSecurityEnvironment.isValidSystemSecurityContext("identity", "1", tenantIdentifier);

final String applicationIdentifier = invocation.getArgumentAt(0, String.class);
final String keyTimestamp = invocation.getArgumentAt(1, String.class);
identityListener.onSetApplicationSignature(tenantIdentifier,
gson.toJson(new ApplicationSignatureEvent(applicationIdentifier, keyTimestamp)));
return null;
}

boolean isValidSecurityContext() {
return validSecurityContext;
}
}

private class VerifyIsisCreateApplicationPermission implements Answer<Void> {

private boolean validSecurityContext = true;
private final String tenantIdentifier;
private final String applicationIdentifier;
private int callCount = 0;

private VerifyIsisCreateApplicationPermission(final String tenantIdentifier, final String applicationIdentifier) {
this.tenantIdentifier = tenantIdentifier;
this.applicationIdentifier = applicationIdentifier;
}

@Override
public Void answer(final InvocationOnMock invocation) throws Throwable {
final boolean validSecurityContextForThisCall = systemSecurityEnvironment.isValidSystemSecurityContext("identity", "1", tenantIdentifier);
validSecurityContext = validSecurityContext && validSecurityContextForThisCall;
callCount++;

final String callApplicationIdentifier = invocation.getArgumentAt(0, String.class);
Assert.assertEquals(this.applicationIdentifier, callApplicationIdentifier);
return null;
}



boolean isValidSecurityContext() {
return validSecurityContext;
}

int getCallCount() {
return callCount;
}
}

@Test
public void testTenantApplicationAssignment() throws InterruptedException {
public void testTenantApplicationAssignment() throws Exception {
//Create io.mifos.provisioner.tenant
final Tenant tenant = Fixture.getCompTestTenant();
provisioner.createTenant(tenant);



//Create identity service application
final Application identityServiceApp = new Application();
identityServiceApp.setName("identity-v1");
@@ -348,9 +443,6 @@ public void testTenantApplicationAssignment() throws InterruptedException {
}
doAnswer(verifyInitializeContextAndReturnSignature).when(identityServiceMock).initialize(anyString());

final TokenChecker tokenChecker = new TokenChecker();
doAnswer(tokenChecker).when(tokenProviderSpy).createToken(tenant.getIdentifier(), "identity-v1", 2L, TimeUnit.MINUTES);

{
final IdentityManagerInitialization identityServiceAdminInitialization
= provisioner.assignIdentityManager(tenant.getIdentifier(), identityServiceAssigned);
@@ -360,9 +452,6 @@ public void testTenantApplicationAssignment() throws InterruptedException {
Assert.assertNotNull(identityServiceAdminInitialization.getAdminPassword());
}

verify(applicationCallContextProviderSpy, atMost(2)).getApplicationCallContext(tenant.getIdentifier(), "identity-v1");


//Create horus application.
final Application officeApp = new Application();
officeApp.setName("office-v1");
@@ -397,43 +486,47 @@ public void testTenantApplicationAssignment() throws InterruptedException {
final VerifyCreateSignatureSetContext verifyCreateSignatureSetContext;
final VerifyAnubisPermittablesContext verifyAnubisPermittablesContext;
final VerifyAnputRequiredPermissionsContext verifyAnputRequiredPermissionsContext;
final VerifyIsisCreatePermittableGroup verifyIsisCreatePermittableGroup;
final VerifyIsisSetApplicationSignature verifyIsisSetApplicationSignature;
final VerifyIsisCreateApplicationPermission verifyIsisCreateApplicationPermission;
try (final AutoTenantContext ignored = new AutoTenantContext(tenant.getIdentifier())) {
verifyAnubisInitializeContext = new VerifyAnubisInitializeContext("office", tenant.getIdentifier());
verifyCreateSignatureSetContext = new VerifyCreateSignatureSetContext(keysInApplicationSignature, "office", tenant.getIdentifier());
verifyAnubisPermittablesContext = new VerifyAnubisPermittablesContext(Arrays.asList(xxPermittableEndpoint, xxPermittableEndpoint, xyPermittableEndpoint, xyGetPermittableEndpoint, mPermittableEndpoint), tenant.getIdentifier());
verifyAnputRequiredPermissionsContext = new VerifyAnputRequiredPermissionsContext(Arrays.asList(forFooPermission, forBarPermission), tenant.getIdentifier());
verifyIsisCreatePermittableGroup = new VerifyIsisCreatePermittableGroup(tenant.getIdentifier());
verifyIsisSetApplicationSignature = new VerifyIsisSetApplicationSignature(tenant.getIdentifier());
verifyIsisCreateApplicationPermission = new VerifyIsisCreateApplicationPermission(tenant.getIdentifier(), "office-v1");
}
doAnswer(verifyAnubisInitializeContext).when(anubisMock).initializeResources();
doAnswer(verifyCreateSignatureSetContext).when(anubisMock).createSignatureSet(anyString(), anyObject());
doAnswer(verifyAnubisPermittablesContext).when(anubisMock).getPermittableEndpoints();
doAnswer(verifyAnputRequiredPermissionsContext).when(anputMock).getRequiredPermissions();

doAnswer(verifyIsisCreatePermittableGroup).when(identityServiceMock).createPermittableGroup(new PermittableGroup("x", Arrays.asList(xxPermittableEndpoint, xyPermittableEndpoint, xyGetPermittableEndpoint)));
doAnswer(verifyIsisCreatePermittableGroup).when(identityServiceMock).createPermittableGroup(new PermittableGroup("m", Collections.singletonList(mPermittableEndpoint)));
doAnswer(verifyIsisSetApplicationSignature).when(identityServiceMock).setApplicationSignature(
"office-v1",
verifyInitializeContextAndReturnSignature.getKeyTimestamp(),
new Signature(keysInApplicationSignature.getPublicKeyMod(), keysInApplicationSignature.getPublicKeyExp()));
doAnswer(verifyIsisCreateApplicationPermission).when(identityServiceMock).createApplicationPermission("office-v1", new Permission("x", AllowedOperation.ALL));
doAnswer(verifyIsisCreateApplicationPermission).when(identityServiceMock).createApplicationPermission("office-v1", new Permission("m", Collections.singleton(AllowedOperation.READ)));
doAnswer(verifyIsisCreateApplicationPermission).when(identityServiceMock).createApplicationCallEndpointSet("office-v1", new CallEndpointSet("forPurposeFoo", Collections.singletonList("x")));
doAnswer(verifyIsisCreateApplicationPermission).when(identityServiceMock).createApplicationCallEndpointSet("office-v1", new CallEndpointSet("forPurposeBar", Collections.singletonList("m")));

{
provisioner.assignApplications(tenant.getIdentifier(), Collections.singletonList(officeAssigned));
Thread.sleep(1500L); //Application assigning is asynchronous and I have no message queue.
}

verify(applicationCallContextProviderSpy).getApplicationCallContext(tenant.getIdentifier(), "office-v1");
verify(applicationCallContextProviderSpy, never()).getApplicationCallContext(eq(Fixture.TENANT_NAME), Mockito.anyString());
verify(tokenProviderSpy).createToken(tenant.getIdentifier(), "office-v1", 2L, TimeUnit.MINUTES);

try (final AutoTenantContext ignored = new AutoTenantContext(tenant.getIdentifier())) {
verify(identityServiceMock).setApplicationSignature(
"office-v1",
systemSecurityEnvironment.tenantKeyTimestamp(),
new Signature(keysInApplicationSignature.getPublicKeyMod(), keysInApplicationSignature.getPublicKeyExp()));
verify(identityServiceMock).createPermittableGroup(new PermittableGroup("x", Arrays.asList(xxPermittableEndpoint, xyPermittableEndpoint, xyGetPermittableEndpoint)));
verify(identityServiceMock).createPermittableGroup(new PermittableGroup("m", Collections.singletonList(mPermittableEndpoint)));
verify(identityServiceMock).createApplicationPermission("office-v1", new Permission("x", AllowedOperation.ALL));
verify(identityServiceMock).createApplicationPermission("office-v1", new Permission("m", Collections.singleton(AllowedOperation.READ)));
verify(identityServiceMock).createApplicationCallEndpointSet("office-v1", new CallEndpointSet("forPurposeFoo", Collections.singletonList("x")));
verify(identityServiceMock).createApplicationCallEndpointSet("office-v1", new CallEndpointSet("forPurposeBar", Collections.singletonList("m")));
Thread.sleep(1500L); //Application assigning is asynchronous and I have no message queue.
}

Assert.assertTrue(verifyAnubisInitializeContext.isValidSecurityContext());
Assert.assertTrue(verifyCreateSignatureSetContext.isValidSecurityContext());
Assert.assertTrue(verifyAnubisPermittablesContext.isValidSecurityContext());
Assert.assertTrue(verifyAnputRequiredPermissionsContext.isValidSecurityContext());
Assert.assertEquals(2, verifyIsisCreatePermittableGroup.getCallCount());
Assert.assertTrue(verifyIsisCreatePermittableGroup.isValidSecurityContext());
Assert.assertTrue(verifyIsisSetApplicationSignature.isValidSecurityContext());
Assert.assertEquals(4, verifyIsisCreateApplicationPermission.getCallCount());
Assert.assertTrue(verifyIsisCreateApplicationPermission.isValidSecurityContext());
}
}

0 comments on commit 1234f16

Please sign in to comment.