NamespaceUtil { public String normalizeNamespace(String appId, String namespaceName) { AppNamespace appNamespace = appNamespaceServiceWithCache.findByAppIdAndNamespace(appId, namespaceName); if (appNamespace != null) { return appNamespace.getName(); } appNamespace = appNamespaceServiceWithCache.findPublicNamespaceByName(namespaceName); if (appNamespace != null) { return appNamespace.getName(); } return namespaceName; } NamespaceUtil(final AppNamespaceServiceWithCache appNamespaceServiceWithCache); String filterNamespaceName(String namespaceName); String normalizeNamespace(String appId, String namespaceName);  }
@Test public void testNormalizeNamespaceWithPrivateNamespace() throws Exception { String someAppId = "someAppId"; String someNamespaceName = "someNamespaceName"; String someNormalizedNamespaceName = "someNormalizedNamespaceName"; AppNamespace someAppNamespace = mock(AppNamespace.class); when(someAppNamespace.getName()).thenReturn(someNormalizedNamespaceName); when(appNamespaceServiceWithCache.findByAppIdAndNamespace(someAppId, someNamespaceName)).thenReturn (someAppNamespace); assertEquals(someNormalizedNamespaceName, namespaceUtil.normalizeNamespace(someAppId, someNamespaceName)); verify(appNamespaceServiceWithCache, times(1)).findByAppIdAndNamespace(someAppId, someNamespaceName); verify(appNamespaceServiceWithCache, never()).findPublicNamespaceByName(someNamespaceName); }
@Test public void testNormalizeNamespaceWithPublicNamespace() throws Exception { String someAppId = "someAppId"; String someNamespaceName = "someNamespaceName"; String someNormalizedNamespaceName = "someNormalizedNamespaceName"; AppNamespace someAppNamespace = mock(AppNamespace.class); when(someAppNamespace.getName()).thenReturn(someNormalizedNamespaceName); when(appNamespaceServiceWithCache.findByAppIdAndNamespace(someAppId, someNamespaceName)).thenReturn(null); when(appNamespaceServiceWithCache.findPublicNamespaceByName(someNamespaceName)).thenReturn(someAppNamespace); assertEquals(someNormalizedNamespaceName, namespaceUtil.normalizeNamespace(someAppId, someNamespaceName)); verify(appNamespaceServiceWithCache, times(1)).findByAppIdAndNamespace(someAppId, someNamespaceName); verify(appNamespaceServiceWithCache, times(1)).findPublicNamespaceByName(someNamespaceName); }
@Test public void testNormalizeNamespaceFailed() throws Exception { String someAppId = "someAppId"; String someNamespaceName = "someNamespaceName"; when(appNamespaceServiceWithCache.findByAppIdAndNamespace(someAppId, someNamespaceName)).thenReturn(null); when(appNamespaceServiceWithCache.findPublicNamespaceByName(someNamespaceName)).thenReturn(null); assertEquals(someNamespaceName, namespaceUtil.normalizeNamespace(someAppId, someNamespaceName)); verify(appNamespaceServiceWithCache, times(1)).findByAppIdAndNamespace(someAppId, someNamespaceName); verify(appNamespaceServiceWithCache, times(1)).findPublicNamespaceByName(someNamespaceName); }