|
| 1 | +package org.cloudfoundry.multiapps.controller.core.helpers.common; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.when; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.cloudfoundry.multiapps.common.test.TestUtil; |
| 9 | +import org.cloudfoundry.multiapps.common.test.Tester; |
| 10 | +import org.cloudfoundry.multiapps.common.test.Tester.Expectation; |
| 11 | +import org.cloudfoundry.multiapps.common.util.JsonUtil; |
| 12 | +import org.cloudfoundry.multiapps.common.util.YamlParser; |
| 13 | +import org.cloudfoundry.multiapps.controller.core.helpers.v2.ConfigurationFilterParser; |
| 14 | +import org.cloudfoundry.multiapps.controller.core.helpers.v2.ConfigurationReferencesResolver; |
| 15 | +import org.cloudfoundry.multiapps.controller.core.test.MockBuilder; |
| 16 | +import org.cloudfoundry.multiapps.controller.core.util.ApplicationConfiguration; |
| 17 | +import org.cloudfoundry.multiapps.controller.persistence.model.CloudTarget; |
| 18 | +import org.cloudfoundry.multiapps.controller.persistence.model.ConfigurationEntry; |
| 19 | +import org.cloudfoundry.multiapps.controller.persistence.model.filters.ConfigurationFilter; |
| 20 | +import org.cloudfoundry.multiapps.controller.persistence.query.ConfigurationEntryQuery; |
| 21 | +import org.cloudfoundry.multiapps.controller.persistence.services.ConfigurationEntryService; |
| 22 | +import org.cloudfoundry.multiapps.mta.builders.v2.ParametersChainBuilder; |
| 23 | +import org.cloudfoundry.multiapps.mta.handlers.ConfigurationParser; |
| 24 | +import org.cloudfoundry.multiapps.mta.handlers.v2.DescriptorParser; |
| 25 | +import org.cloudfoundry.multiapps.mta.model.DeploymentDescriptor; |
| 26 | +import org.cloudfoundry.multiapps.mta.model.Platform; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.mockito.Answers; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.Mockito; |
| 32 | +import org.mockito.MockitoAnnotations; |
| 33 | + |
| 34 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 35 | + |
| 36 | +public abstract class AbstractConfigurationReferencesResolverTest { |
| 37 | + protected static class ServiceMockConfiguration { |
| 38 | + |
| 39 | + ConfigurationFilter filter; |
| 40 | + List<ConfigurationEntry> configurationEntries; |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + private static Platform platform; |
| 45 | + |
| 46 | + @Mock |
| 47 | + protected ConfigurationEntryService configurationEntryService; |
| 48 | + @Mock(answer = Answers.RETURNS_SELF) |
| 49 | + protected ConfigurationEntryQuery configurationEntryQuery; |
| 50 | + @Mock |
| 51 | + protected ApplicationConfiguration configuration; |
| 52 | + |
| 53 | + protected DeploymentDescriptor descriptor; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + static void initializePlatform() { |
| 57 | + ConfigurationParser parser = new ConfigurationParser(); |
| 58 | + platform = parser.parsePlatformJson(AbstractConfigurationReferencesResolverTest.class.getResourceAsStream("/mta/xs-platform.json")); |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() throws Exception { |
| 63 | + MockitoAnnotations.openMocks(this) |
| 64 | + .close(); |
| 65 | + } |
| 66 | + |
| 67 | + protected void executeTestResolve(Tester tester, String descriptorLocation, String configurationEntriesLocation, |
| 68 | + Expectation expectation) { |
| 69 | + prepareService(); |
| 70 | + prepareConfigurationEntries(configurationEntriesLocation); |
| 71 | + prepareDeploymentDescriptor(descriptorLocation); |
| 72 | + var referencesResolver = getConfigurationResolver(); |
| 73 | + |
| 74 | + tester.test(() -> { |
| 75 | + |
| 76 | + referencesResolver.resolve(descriptor); |
| 77 | + return descriptor; |
| 78 | + |
| 79 | + }, expectation); |
| 80 | + } |
| 81 | + |
| 82 | + protected void prepareService() { |
| 83 | + when(configurationEntryService.createQuery()).thenReturn(configurationEntryQuery); |
| 84 | + } |
| 85 | + |
| 86 | + protected void prepareConfigurationEntries(String configurationEntriesLocation) { |
| 87 | + List<ServiceMockConfiguration> serviceConfigurations = JsonUtil.fromJson(TestUtil.getResourceAsString(configurationEntriesLocation, |
| 88 | + getClass()), |
| 89 | + new TypeReference<List<ServiceMockConfiguration>>() { |
| 90 | + }); |
| 91 | + for (ServiceMockConfiguration config : serviceConfigurations) { |
| 92 | + ConfigurationFilter filter = config.filter; |
| 93 | + ConfigurationEntryQuery configurationEntryQueryMock = getConfigurationEntryQueryMock(filter); |
| 94 | + when(configurationEntryQueryMock.list()).thenReturn(config.configurationEntries); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private ConfigurationEntryQuery getConfigurationEntryQueryMock(ConfigurationFilter filter) { |
| 99 | + return new MockBuilder<>(configurationEntryQuery).on(query -> query.providerNid(filter.getProviderNid())) |
| 100 | + .on(query -> query.providerId(filter.getProviderId())) |
| 101 | + .on(query -> query.version(filter.getProviderVersion())) |
| 102 | + .on(query -> query.target(filter.getTargetSpace())) |
| 103 | + .on(query -> query.requiredProperties(filter.getRequiredContent())) |
| 104 | + .on(query -> query.visibilityTargets(Mockito.any())) |
| 105 | + .build(); |
| 106 | + } |
| 107 | + |
| 108 | + protected void prepareDeploymentDescriptor(String descriptorLocation) { |
| 109 | + Map<String, Object> deploymentDescriptorMap = new YamlParser().convertYamlToMap(getClass().getResourceAsStream(descriptorLocation)); |
| 110 | + this.descriptor = getDescriptorParser().parseDeploymentDescriptor(deploymentDescriptorMap); |
| 111 | + } |
| 112 | + |
| 113 | + protected DescriptorParser getDescriptorParser() { |
| 114 | + return new DescriptorParser(); |
| 115 | + } |
| 116 | + |
| 117 | + protected ConfigurationReferencesResolver getConfigurationResolver() { |
| 118 | + return new ConfigurationReferencesResolver(configurationEntryService, |
| 119 | + new ConfigurationFilterParser(getCloudTarget(), |
| 120 | + getPropertiesChainBuilder(descriptor), |
| 121 | + null), |
| 122 | + null, |
| 123 | + configuration); |
| 124 | + } |
| 125 | + |
| 126 | + protected CloudTarget getCloudTarget() { |
| 127 | + String currentOrg = (String) platform.getParameters() |
| 128 | + .get("org"); |
| 129 | + String currentSpace = (String) platform.getParameters() |
| 130 | + .get("space"); |
| 131 | + return new CloudTarget(currentOrg, currentSpace); |
| 132 | + } |
| 133 | + |
| 134 | + protected ParametersChainBuilder getPropertiesChainBuilder(DeploymentDescriptor descriptor) { |
| 135 | + return new ParametersChainBuilder(descriptor, platform); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments