ConfigController { @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 = "messages", required = false) String messagesAsString, HttpServletRequest request, HttpServletResponse response) throws IOException { String originalNamespace = namespace; namespace = namespaceUtil.filterNamespaceName(namespace); 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, appId, clusterName, namespace, dataCenter, clientMessages); if (currentAppRelease != null) { releases.add(currentAppRelease); appClusterNameLoaded = currentAppRelease.getClusterName(); } } if (!namespaceBelongsToAppId(appId, namespace)) { Release publicRelease = this.findPublicConfig(appId, clientIp, clusterName, namespace, dataCenter, clientMessages); if (!Objects.isNull(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)) { 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; } ConfigController(
      final ConfigService configService,
      final AppNamespaceServiceWithCache appNamespaceService,
      final NamespaceUtil namespaceUtil,
      final InstanceConfigAuditUtil instanceConfigAuditUtil,
      final Gson gson); @GetMapping(value = "/{appId}/{clusterName}/{namespace:.+}") 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 = "messages", required = false) String messagesAsString,
                                  HttpServletRequest request, HttpServletResponse response);  }
@Test public void testQueryConfig() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideNewReleaseKey = "2"; HttpServletResponse someResponse = mock(HttpServletResponse.class); when(configService.loadConfig(someAppId, someClientIp, 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, someMessagesAsString, someRequest, someResponse); verify(configService, times(1)).loadConfig(someAppId, someClientIp, 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); }
@Test public void testQueryConfigFile() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideNewReleaseKey = "2"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String someNamespaceName = String.format("%s.%s", defaultClusterName, "properties"); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(someRelease); when(someRelease.getReleaseKey()).thenReturn(someServerSideNewReleaseKey); when(namespaceUtil.filterNamespaceName(someNamespaceName)).thenReturn(defaultNamespaceName); when(namespaceUtil.normalizeNamespace(someAppId, defaultNamespaceName)).thenReturn(defaultNamespaceName); ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); verify(configService, times(1)).loadConfig(someAppId, someClientIp, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages); assertEquals(someAppId, result.getAppId()); assertEquals(someClusterName, result.getCluster()); assertEquals(someNamespaceName, result.getNamespaceName()); assertEquals(someServerSideNewReleaseKey, result.getReleaseKey()); }
@Test public void testQueryConfigFileWithPrivateNamespace() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideNewReleaseKey = "2"; String somePrivateNamespace = "datasource"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String somePrivateNamespaceName = String.format("%s.%s", somePrivateNamespace, "xml"); AppNamespace appNamespace = mock(AppNamespace.class); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, somePrivateNamespace, someDataCenter, someNotificationMessages)).thenReturn(someRelease); when(someRelease.getReleaseKey()).thenReturn(someServerSideNewReleaseKey); when(namespaceUtil.filterNamespaceName(somePrivateNamespaceName)).thenReturn(somePrivateNamespace); when(namespaceUtil.normalizeNamespace(someAppId, somePrivateNamespace)).thenReturn(somePrivateNamespace); when(appNamespaceService.findByAppIdAndNamespace(someAppId, somePrivateNamespace)) .thenReturn(appNamespace); ApolloConfig result = configController.queryConfig(someAppId, someClusterName, somePrivateNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); assertEquals(someAppId, result.getAppId()); assertEquals(someClusterName, result.getCluster()); assertEquals(somePrivateNamespaceName, result.getNamespaceName()); assertEquals(someServerSideNewReleaseKey, result.getReleaseKey()); }
@Test public void testQueryConfigWithReleaseNotFound() throws Exception { String someClientSideReleaseKey = "1"; HttpServletResponse someResponse = mock(HttpServletResponse.class); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(null); ApolloConfig result = configController.queryConfig(someAppId, someClusterName, defaultNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); assertNull(result); verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); }
@Test public void testQueryConfigWithApolloConfigNotModified() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideReleaseKey = someClientSideReleaseKey; HttpServletResponse someResponse = mock(HttpServletResponse.class); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(someRelease); when(someRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey); ApolloConfig result = configController.queryConfig(someAppId, someClusterName, defaultNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); assertNull(result); verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED); }
@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, 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, 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")); }
@Test public void testQueryConfigWithPubicNamespaceAndNoAppOverride() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideReleaseKey = "2"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String somePublicAppId = "somePublicAppId"; String somePublicClusterName = "somePublicClusterName"; AppNamespace somePublicAppNamespace = assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(null); when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName)) .thenReturn(somePublicAppNamespace); when(configService.loadConfig(someAppId, someClientIp, somePublicAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(somePublicRelease); when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey); when(somePublicRelease.getAppId()).thenReturn(somePublicAppId); when(somePublicRelease.getClusterName()).thenReturn(somePublicClusterName); when(somePublicRelease.getNamespaceName()).thenReturn(somePublicNamespaceName); ApolloConfig result = configController .queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); assertEquals(someServerSideReleaseKey, result.getReleaseKey()); assertEquals(someAppId, result.getAppId()); assertEquals(someClusterName, result.getCluster()); assertEquals(somePublicNamespaceName, result.getNamespaceName()); assertEquals("foo", result.getConfigurations().get("apollo.public.bar")); verify(instanceConfigAuditUtil, times(1)).audit(someAppId, someClusterName, someDataCenter, someClientIp, somePublicAppId, somePublicClusterName, somePublicNamespaceName, someServerSideReleaseKey); }
@Test public void testQueryConfigFileWithPublicNamespaceAndNoAppOverride() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideReleaseKey = "2"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String somePublicAppId = "somePublicAppId"; String someNamespace = String.format("%s.%s", somePublicNamespaceName, "properties"); AppNamespace somePublicAppNamespace = assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(null); when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName)) .thenReturn(somePublicAppNamespace); when(configService.loadConfig(someAppId, someClientIp, somePublicAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(somePublicRelease); when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey); when(namespaceUtil.filterNamespaceName(someNamespace)).thenReturn(somePublicNamespaceName); when(namespaceUtil.normalizeNamespace(someAppId, somePublicNamespaceName)).thenReturn(somePublicNamespaceName); when(appNamespaceService.findByAppIdAndNamespace(someAppId, somePublicNamespaceName)).thenReturn(null); ApolloConfig result = configController .queryConfig(someAppId, someClusterName, someNamespace, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); assertEquals(someServerSideReleaseKey, result.getReleaseKey()); assertEquals(someAppId, result.getAppId()); assertEquals(someClusterName, result.getCluster()); assertEquals(someNamespace, result.getNamespaceName()); assertEquals("foo", result.getConfigurations().get("apollo.public.bar")); }
@Test public void testQueryConfigWithPublicNamespaceAndAppOverride() throws Exception { String someAppSideReleaseKey = "1"; String somePublicAppSideReleaseKey = "2"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String somePublicAppId = "somePublicAppId"; AppNamespace somePublicAppNamespace = assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName); when(someRelease.getConfigurations()).thenReturn("{\"apollo.public.foo\": \"foo-override\"}"); when(somePublicRelease.getConfigurations()) .thenReturn("{\"apollo.public.foo\": \"foo\", \"apollo.public.bar\": \"bar\"}"); when(configService.loadConfig(someAppId, someClientIp, someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(someRelease); when(someRelease.getReleaseKey()).thenReturn(someAppSideReleaseKey); when(someRelease.getNamespaceName()).thenReturn(somePublicNamespaceName); when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName)) .thenReturn(somePublicAppNamespace); when(configService.loadConfig(someAppId, someClientIp, somePublicAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(somePublicRelease); when(somePublicRelease.getReleaseKey()).thenReturn(somePublicAppSideReleaseKey); when(somePublicRelease.getAppId()).thenReturn(somePublicAppId); when(somePublicRelease.getClusterName()).thenReturn(someDataCenter); when(somePublicRelease.getNamespaceName()).thenReturn(somePublicNamespaceName); ApolloConfig result = configController .queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someAppSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); assertEquals(Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR) .join(someAppSideReleaseKey, somePublicAppSideReleaseKey), result.getReleaseKey()); assertEquals(someAppId, result.getAppId()); assertEquals(someClusterName, result.getCluster()); assertEquals(somePublicNamespaceName, result.getNamespaceName()); assertEquals("foo-override", result.getConfigurations().get("apollo.public.foo")); assertEquals("bar", result.getConfigurations().get("apollo.public.bar")); verify(instanceConfigAuditUtil, times(1)).audit(someAppId, someClusterName, someDataCenter, someClientIp, someAppId, someClusterName, somePublicNamespaceName, someAppSideReleaseKey); verify(instanceConfigAuditUtil, times(1)).audit(someAppId, someClusterName, someDataCenter, someClientIp, somePublicAppId, someDataCenter, somePublicNamespaceName, somePublicAppSideReleaseKey); }
@Test public void testQueryConfigForNoAppIdPlaceHolder() throws Exception { String someClientSideReleaseKey = "1"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String appId = ConfigConsts.NO_APPID_PLACEHOLDER; ApolloConfig result = configController.queryConfig(appId, someClusterName, defaultNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); verify(configService, never()).loadConfig(appId, someClientIp, someAppId, someClusterName, defaultNamespaceName, someDataCenter, someNotificationMessages); verify(appNamespaceService, never()).findPublicNamespaceByName(defaultNamespaceName); assertNull(result); verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); }
@Test public void testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace() throws Exception { String someClientSideReleaseKey = "1"; String someServerSideReleaseKey = "2"; HttpServletResponse someResponse = mock(HttpServletResponse.class); String somePublicAppId = "somePublicAppId"; AppNamespace somePublicAppNamespace = assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName); String appId = ConfigConsts.NO_APPID_PLACEHOLDER; when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName)) .thenReturn(somePublicAppNamespace); when(configService.loadConfig(appId, someClientIp, somePublicAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(somePublicRelease); when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey); when(namespaceUtil.normalizeNamespace(appId, somePublicNamespaceName)).thenReturn(somePublicNamespaceName); ApolloConfig result = configController.queryConfig(appId, someClusterName, somePublicNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someMessagesAsString, someRequest, someResponse); verify(configService, never()).loadConfig(appId, someClientIp, appId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages); assertEquals(someServerSideReleaseKey, result.getReleaseKey()); assertEquals(appId, result.getAppId()); assertEquals(someClusterName, result.getCluster()); assertEquals(somePublicNamespaceName, result.getNamespaceName()); assertEquals("foo", result.getConfigurations().get("apollo.public.bar")); }