Skip to content

Commit

Permalink
fixup! Added an ability to list namespace available to ws creation
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <[email protected]>
  • Loading branch information
Sergii authored and sleshchenko committed Oct 9, 2019
1 parent ddad8bf commit e35533f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
10 changes: 10 additions & 0 deletions infrastructures/kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-account</artifactId>
Expand Down Expand Up @@ -222,6 +227,11 @@
<artifactId>org.eclipse.persistence.jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.everrest</groupId>
<artifactId>everrest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public KubernetesNamespaceMetaImpl(String name, Map<String, String> attributes)
}
}

public KubernetesNamespaceMetaImpl(KubernetesNamespaceMeta namespaceMeta) {
this(namespaceMeta.getName(), namespaceMeta.getAttributes());
}

@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.api.server;

import static com.jayway.restassured.RestAssured.given;
import static java.util.Collections.singletonList;
import static org.everrest.assured.JettyHttpServer.ADMIN_USER_NAME;
import static org.everrest.assured.JettyHttpServer.ADMIN_USER_PASSWORD;
import static org.everrest.assured.JettyHttpServer.SECURE_PATH;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import com.google.common.collect.ImmutableMap;
import com.jayway.restassured.response.Response;
import java.util.Collections;
import java.util.List;
import org.eclipse.che.api.core.rest.CheJsonProvider;
import org.eclipse.che.dto.server.DtoFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto.KubernetesNamespaceMetaDto;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory;
import org.everrest.assured.EverrestJetty;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

/**
* Tests for {@link KubernetesNamespaceService}
*
* @author Sergii Leshchenko
*/
@Listeners(value = {EverrestJetty.class, MockitoTestNGListener.class})
public class KubernetesNamespaceServiceTest {

@SuppressWarnings("unused") // is declared for deploying by everrest-assured
private CheJsonProvider jsonProvider = new CheJsonProvider(Collections.emptySet());

@Mock private KubernetesNamespaceFactory namespaceFactory;

@InjectMocks private KubernetesNamespaceService service;

@Test
public void shouldReturnNamespaces() throws Exception {
KubernetesNamespaceMetaImpl namespaceMeta =
new KubernetesNamespaceMetaImpl(
"ws-namespace", ImmutableMap.of("phase", "active", "default", "true"));
when(namespaceFactory.list()).thenReturn(singletonList(namespaceMeta));

final Response response =
given()
.auth()
.basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
.when()
.get(SECURE_PATH + "/kubernetes/namespace");

assertEquals(response.getStatusCode(), 200);
List<KubernetesNamespaceMetaDto> namespaces =
unwrapDtoList(response, KubernetesNamespaceMetaDto.class);
assertEquals(namespaces.size(), 1);
assertEquals(new KubernetesNamespaceMetaImpl(namespaces.get(0)), namespaceMeta);
verify(namespaceFactory).list();
}

private static <T> List<T> unwrapDtoList(Response response, Class<T> dtoClass) {
return DtoFactory.getInstance().createListDtoFromJson(response.body().print(), dtoClass);
}
}

0 comments on commit e35533f

Please sign in to comment.