KubernetesNamespaceFactory { @VisibleForTesting String normalizeNamespaceName(String namespaceName) { namespaceName = namespaceName .replaceAll("[^-a-zA-Z0-9]", "-") .replaceAll("-+", "-") .replaceAll("^-|-$", ""); return namespaceName.substring( 0, Math.min( namespaceName.length(), METADATA_NAME_MAX_LENGTH)); } @Inject  KubernetesNamespaceFactory(
      @Nullable @Named("che.infra.kubernetes.namespace") String legacyNamespaceName,
      @Nullable @Named("che.infra.kubernetes.service_account_name") String serviceAccountName,
      @Nullable @Named("che.infra.kubernetes.workspace_sa_cluster_roles") String clusterRoleNames,
      @Nullable @Named("che.infra.kubernetes.namespace.default") String defaultNamespaceName,
      @Named("che.infra.kubernetes.namespace.allow_user_defined")
          boolean allowUserDefinedNamespaces,
      @Named("che.infra.kubernetes.namespace.creation_allowed") boolean namespaceCreationAllowed,
      KubernetesClientFactory clientFactory,
      UserManager userManager,
      PreferenceManager preferenceManager,
      KubernetesSharedPool sharedPool); KubernetesNamespace access(String workspaceId, String namespace); void checkIfNamespaceIsAllowed(String namespaceName); List<KubernetesNamespaceMeta> list(); KubernetesNamespace getOrCreate(RuntimeIdentity identity); KubernetesNamespace get(RuntimeIdentity identity); KubernetesNamespace get(Workspace workspace); String evaluateLegacyNamespaceName(NamespaceResolutionContext resolutionCtx); String evaluateNamespaceName(NamespaceResolutionContext resolutionCtx); void deleteIfManaged(Workspace workspace);  }
@Test(dataProvider = "invalidUsernames") public void normalizeTest(String raw, String expected) { namespaceFactory = new KubernetesNamespaceFactory( "", "", "", "che-<userid>", false, true, clientFactory, userManager, preferenceManager, pool); assertEquals(expected, namespaceFactory.normalizeNamespaceName(raw)); }
@Test public void normalizeLengthTest() { namespaceFactory = new KubernetesNamespaceFactory( "", "", "", "che-<userid>", false, true, clientFactory, userManager, preferenceManager, pool); assertEquals( 63, namespaceFactory .normalizeNamespaceName( "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong") .length()); }