OpenShiftProjectFactory extends KubernetesNamespaceFactory { @Override public OpenShiftProject get(Workspace workspace) throws InfrastructureException { return doCreateProjectAccess(workspace.getId(), getNamespaceName(workspace)); } @Inject  OpenShiftProjectFactory(
      @Nullable @Named("che.infra.openshift.project") String projectName,
      @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,
      OpenShiftClientFactory clientFactory,
      OpenShiftClientConfigFactory clientConfigFactory,
      OpenShiftStopWorkspaceRoleProvisioner stopWorkspaceRoleProvisioner,
      UserManager userManager,
      PreferenceManager preferenceManager,
      KubernetesSharedPool sharedPool,
      @Nullable @Named("che.infra.openshift.oauth_identity_provider")
          String oAuthIdentityProvider); OpenShiftProject getOrCreate(RuntimeIdentity identity); @Override OpenShiftProject get(Workspace workspace); OpenShiftProject get(RuntimeIdentity identity); @Override void deleteIfManaged(Workspace workspace); OpenShiftProject access(String workspaceId, String projectName);  }
@Test public void shouldReturnDefaultProjectWhenItExistsAndUserDefinedIsNotAllowed() throws Exception { prepareNamespaceToBeFoundByName( "che-default", new ProjectBuilder() .withNewMetadata() .withName("che-default") .withAnnotations( ImmutableMap.of( PROJECT_DISPLAY_NAME_ANNOTATION, "Default Che Project", PROJECT_DESCRIPTION_ANNOTATION, "some description")) .endMetadata() .withNewStatus() .withPhase("Active") .endStatus() .build()); projectFactory = new OpenShiftProjectFactory( "predefined", "", null, "che-default", false, true, clientFactory, configFactory, stopWorkspaceRoleProvisioner, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER); List<KubernetesNamespaceMeta> availableNamespaces = projectFactory.list(); assertEquals(availableNamespaces.size(), 1); KubernetesNamespaceMeta defaultNamespace = availableNamespaces.get(0); assertEquals(defaultNamespace.getName(), "che-default"); assertEquals(defaultNamespace.getAttributes().get(DEFAULT_ATTRIBUTE), "true"); assertEquals( defaultNamespace.getAttributes().get(PROJECT_DISPLAY_NAME_ATTRIBUTE), "Default Che Project"); assertEquals( defaultNamespace.getAttributes().get(PROJECT_DESCRIPTION_ATTRIBUTE), "some description"); assertEquals(defaultNamespace.getAttributes().get(PHASE_ATTRIBUTE), "Active"); }
@Test public void shouldReturnDefaultProjectWhenItDoesNotExistAndUserDefinedIsNotAllowed() throws Exception { throwOnTryToGetProjectByName( "che-default", new KubernetesClientException("forbidden", 403, null)); projectFactory = new OpenShiftProjectFactory( "predefined", "", null, "che-default", false, true, clientFactory, configFactory, stopWorkspaceRoleProvisioner, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER); List<KubernetesNamespaceMeta> availableNamespaces = projectFactory.list(); assertEquals(availableNamespaces.size(), 1); KubernetesNamespaceMeta defaultNamespace = availableNamespaces.get(0); assertEquals(defaultNamespace.getName(), "che-default"); assertEquals(defaultNamespace.getAttributes().get(DEFAULT_ATTRIBUTE), "true"); assertNull( defaultNamespace .getAttributes() .get(PHASE_ATTRIBUTE)); }
@Test public void shouldReturnListOfExistingProjectsAlongWithDefaultIfUserDefinedIsAllowed() throws Exception { prepareListedProjects( Arrays.asList( createProject("my-for-ws", "Project for Workspaces", "some description", "Active"), createProject("default", "Default Che Project", "some description", "Active"))); projectFactory = new OpenShiftProjectFactory( "predefined", "", null, "default", true, true, clientFactory, configFactory, stopWorkspaceRoleProvisioner, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER); List<KubernetesNamespaceMeta> availableNamespaces = projectFactory.list(); assertEquals(availableNamespaces.size(), 2); KubernetesNamespaceMeta forWS = availableNamespaces.get(0); assertEquals(forWS.getName(), "my-for-ws"); assertEquals( forWS.getAttributes().get(PROJECT_DISPLAY_NAME_ATTRIBUTE), "Project for Workspaces"); assertEquals(forWS.getAttributes().get(PROJECT_DESCRIPTION_ATTRIBUTE), "some description"); assertEquals(forWS.getAttributes().get(PHASE_ATTRIBUTE), "Active"); assertNull(forWS.getAttributes().get(DEFAULT_ATTRIBUTE)); KubernetesNamespaceMeta defaultNamespace = availableNamespaces.get(1); assertEquals(defaultNamespace.getName(), "default"); assertEquals( defaultNamespace.getAttributes().get(PROJECT_DISPLAY_NAME_ATTRIBUTE), "Default Che Project"); assertEquals( defaultNamespace.getAttributes().get(PROJECT_DESCRIPTION_ATTRIBUTE), "some description"); assertEquals(defaultNamespace.getAttributes().get(PHASE_ATTRIBUTE), "Active"); assertEquals(defaultNamespace.getAttributes().get(DEFAULT_ATTRIBUTE), "true"); }
@Test public void shouldReturnListOfExistingProjectsAlongWithNonExistingDefaultIfUserDefinedIsAllowed() throws Exception { prepareListedProjects(singletonList(createProject("my-for-ws", "", "", "Active"))); projectFactory = new OpenShiftProjectFactory( "predefined", "", null, "default", true, true, clientFactory, configFactory, stopWorkspaceRoleProvisioner, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER); List<KubernetesNamespaceMeta> availableNamespaces = projectFactory.list(); assertEquals(availableNamespaces.size(), 2); KubernetesNamespaceMeta forWS = availableNamespaces.get(0); assertEquals(forWS.getName(), "my-for-ws"); assertEquals(forWS.getAttributes().get(PHASE_ATTRIBUTE), "Active"); assertNull(forWS.getAttributes().get(DEFAULT_ATTRIBUTE)); KubernetesNamespaceMeta defaultNamespace = availableNamespaces.get(1); assertEquals(defaultNamespace.getName(), "default"); assertEquals(defaultNamespace.getAttributes().get(DEFAULT_ATTRIBUTE), "true"); assertNull( defaultNamespace .getAttributes() .get(PHASE_ATTRIBUTE)); }