Search in sources :

Example 26 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class ConfigControllerTest method testQueryConfig.

@Test
public void testQueryConfig() throws Exception {
    String someClientSideReleaseKey = "1";
    String someServerSideNewReleaseKey = "2";
    HttpServletResponse someResponse = mock(HttpServletResponse.class);
    when(configService.loadConfig(someAppId, someClientIp, someClientLabel, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(someRelease);
    when(someRelease.getReleaseKey()).thenReturn(someServerSideNewReleaseKey);
    when(someRelease.getNamespaceName()).thenReturn(defaultNamespaceName);
    ApolloConfig result = configController.queryConfig(someAppId, someClusterName, defaultNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someClientLabel, someMessagesAsString, someRequest, someResponse);
    verify(configService, times(1)).loadConfig(someAppId, someClientIp, someClientLabel, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages);
    assertEquals(someAppId, result.getAppId());
    assertEquals(someClusterName, result.getCluster());
    assertEquals(defaultNamespaceName, result.getNamespaceName());
    assertEquals(someServerSideNewReleaseKey, result.getReleaseKey());
    verify(instanceConfigAuditUtil, times(1)).audit(someAppId, someClusterName, someDataCenter, someClientIp, someAppId, someClusterName, defaultNamespaceName, someServerSideNewReleaseKey);
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 27 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class ConfigControllerTest method testQueryConfigWithAppOwnNamespace.

@Test
public void testQueryConfigWithAppOwnNamespace() throws Exception {
    String someClientSideReleaseKey = "1";
    String someServerSideReleaseKey = "2";
    String someAppOwnNamespaceName = "someAppOwn";
    HttpServletResponse someResponse = mock(HttpServletResponse.class);
    AppNamespace someAppOwnNamespace = assemblePublicAppNamespace(someAppId, someAppOwnNamespaceName);
    when(configService.loadConfig(someAppId, someClientIp, someClientLabel, someAppId, someClusterName, someAppOwnNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(someRelease);
    when(appNamespaceService.findPublicNamespaceByName(someAppOwnNamespaceName)).thenReturn(someAppOwnNamespace);
    when(someRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);
    when(namespaceUtil.filterNamespaceName(someAppOwnNamespaceName)).thenReturn(someAppOwnNamespaceName);
    when(namespaceUtil.normalizeNamespace(someAppId, someAppOwnNamespaceName)).thenReturn(someAppOwnNamespaceName);
    ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someAppOwnNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someClientLabel, someMessagesAsString, someRequest, someResponse);
    assertEquals(someServerSideReleaseKey, result.getReleaseKey());
    assertEquals(someAppId, result.getAppId());
    assertEquals(someClusterName, result.getCluster());
    assertEquals(someAppOwnNamespaceName, result.getNamespaceName());
    assertEquals("foo", result.getConfigurations().get("apollo.bar"));
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Test(org.junit.Test)

Example 28 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class ConfigController method queryConfig.

@GetMapping(value = "/{appId}/{clusterName}/{namespace:.+}")
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName, @PathVariable String namespace, @RequestParam(value = "dataCenter", required = false) String dataCenter, @RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey, @RequestParam(value = "ip", required = false) String clientIp, @RequestParam(value = "label", required = false) String clientLabel, @RequestParam(value = "messages", required = false) String messagesAsString, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String originalNamespace = namespace;
    // strip out .properties suffix
    namespace = namespaceUtil.filterNamespaceName(namespace);
    // fix the character case issue, such as FX.apollo <-> fx.apollo
    namespace = namespaceUtil.normalizeNamespace(appId, namespace);
    if (Strings.isNullOrEmpty(clientIp)) {
        clientIp = tryToGetClientIp(request);
    }
    ApolloNotificationMessages clientMessages = transformMessages(messagesAsString);
    List<Release> releases = Lists.newLinkedList();
    String appClusterNameLoaded = clusterName;
    if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
        Release currentAppRelease = configService.loadConfig(appId, clientIp, clientLabel, appId, clusterName, namespace, dataCenter, clientMessages);
        if (currentAppRelease != null) {
            releases.add(currentAppRelease);
            // we have cluster search process, so the cluster name might be overridden
            appClusterNameLoaded = currentAppRelease.getClusterName();
        }
    }
    // if namespace does not belong to this appId, should check if there is a public configuration
    if (!namespaceBelongsToAppId(appId, namespace)) {
        Release publicRelease = this.findPublicConfig(appId, clientIp, clientLabel, clusterName, namespace, dataCenter, clientMessages);
        if (Objects.nonNull(publicRelease)) {
            releases.add(publicRelease);
        }
    }
    if (releases.isEmpty()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, String.format("Could not load configurations with appId: %s, clusterName: %s, namespace: %s", appId, clusterName, originalNamespace));
        Tracer.logEvent("Apollo.Config.NotFound", assembleKey(appId, clusterName, originalNamespace, dataCenter));
        return null;
    }
    auditReleases(appId, clusterName, dataCenter, clientIp, releases);
    String mergedReleaseKey = releases.stream().map(Release::getReleaseKey).collect(Collectors.joining(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR));
    if (mergedReleaseKey.equals(clientSideReleaseKey)) {
        // Client side configuration is the same with server side, return 304
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        Tracer.logEvent("Apollo.Config.NotModified", assembleKey(appId, appClusterNameLoaded, originalNamespace, dataCenter));
        return null;
    }
    ApolloConfig apolloConfig = new ApolloConfig(appId, appClusterNameLoaded, originalNamespace, mergedReleaseKey);
    apolloConfig.setConfigurations(mergeReleaseConfigurations(releases));
    Tracer.logEvent("Apollo.Config.Found", assembleKey(appId, appClusterNameLoaded, originalNamespace, dataCenter));
    return apolloConfig;
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 29 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project dubbo by alibaba.

the class EmbeddedApolloJunit5 method loadConfigFor.

private String loadConfigFor(String namespace) {
    String filename = String.format("mockdata-%s.properties", namespace);
    final Properties prop = ResourceUtils.readConfigFile(filename, new Properties());
    Map<String, String> configurations = Maps.newHashMap();
    for (String propertyName : prop.stringPropertyNames()) {
        configurations.put(propertyName, prop.getProperty(propertyName));
    }
    ApolloConfig apolloConfig = new ApolloConfig("someAppId", "someCluster", namespace, "someReleaseKey");
    Map<String, String> mergedConfigurations = mergeOverriddenProperties(namespace, configurations);
    apolloConfig.setConfigurations(mergedConfigurations);
    return GSON.toJson(apolloConfig);
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Properties(java.util.Properties)

Example 30 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class ConfigFileController method loadConfig.

private String loadConfig(ConfigFileOutputFormat outputFormat, String appId, String clusterName, String namespace, String dataCenter, String clientIp, String clientLabel, HttpServletRequest request, HttpServletResponse response) throws IOException {
    ApolloConfig apolloConfig = configController.queryConfig(appId, clusterName, namespace, dataCenter, "-1", clientIp, clientLabel, null, request, response);
    if (apolloConfig == null || apolloConfig.getConfigurations() == null) {
        return null;
    }
    String result = null;
    switch(outputFormat) {
        case PROPERTIES:
            Properties properties = new Properties();
            properties.putAll(apolloConfig.getConfigurations());
            result = PropertiesUtil.toString(properties);
            break;
        case JSON:
            result = GSON.toJson(apolloConfig.getConfigurations());
            break;
    }
    return result;
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Properties(java.util.Properties)

Aggregations

ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)57 Test (org.junit.Test)49 Sql (org.springframework.test.context.jdbc.Sql)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)10 Config (com.ctrip.framework.apollo.Config)10 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)10 Properties (java.util.Properties)8 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)7 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)4 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)4 Map (java.util.Map)4 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)3 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3