Import k8s client deps, support inject k8s API server config
Change-Id: Iaf246a06462b8a878e93ef3f98da399c3600b129
diff --git a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/codec/K8sApiConfigCodecTest.java b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/codec/K8sApiConfigCodecTest.java
new file mode 100644
index 0000000..ece2ef8
--- /dev/null
+++ b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/codec/K8sApiConfigCodecTest.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.k8snode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.core.CoreService;
+import org.onosproject.k8snode.api.DefaultK8sApiConfig;
+import org.onosproject.k8snode.api.K8sApiConfig;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.k8snode.codec.K8sApiConfigJsonMatcher.matchesK8sApiConfig;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for kubernetes API config codec.
+ */
+public class K8sApiConfigCodecTest {
+
+ MockCodecContext context;
+
+ JsonCodec<K8sApiConfig> k8sApiConfigCodec;
+
+ final CoreService mockCoreService = createMock(CoreService.class);
+ private static final String REST_APP_ID = "org.onosproject.rest";
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+ context = new MockCodecContext();
+ k8sApiConfigCodec = new K8sApiConfigCodec();
+
+ assertThat(k8sApiConfigCodec, notNullValue());
+
+ expect(mockCoreService.registerApplication(REST_APP_ID))
+ .andReturn(APP_ID).anyTimes();
+ replay(mockCoreService);
+ context.registerService(CoreService.class, mockCoreService);
+ }
+
+ /**
+ * Tests the kubernetes API config encoding.
+ */
+ @Test
+ public void testK8sApiConfigEncode() {
+ K8sApiConfig config = DefaultK8sApiConfig.builder()
+ .scheme(K8sApiConfig.Scheme.HTTPS)
+ .ipAddress(IpAddress.valueOf("10.10.10.23"))
+ .port(6443)
+ .token("token")
+ .caCertData("caCertData")
+ .clientCertData("clientCertData")
+ .clientKeyData("clientKeyData")
+ .build();
+
+ ObjectNode configJson = k8sApiConfigCodec.encode(config, context);
+ assertThat(configJson, matchesK8sApiConfig(config));
+ }
+
+ /**
+ * Tests the kubernetes API config decoding.
+ *
+ * @throws IOException IO exception
+ */
+ @Test
+ public void testK8sApiConfigDecode() throws IOException {
+ K8sApiConfig config = getK8sApiConfig("K8sApiConfig.json");
+
+ assertEquals("HTTPS", config.scheme().name());
+ assertEquals("10.134.34.223", config.ipAddress().toString());
+ assertEquals(6443, config.port());
+ assertEquals("token", config.token());
+ assertEquals("caCertData", config.caCertData());
+ assertEquals("clientCertData", config.clientCertData());
+ assertEquals("clientKeyData", config.clientKeyData());
+ }
+
+ private K8sApiConfig getK8sApiConfig(String resourceName) throws IOException {
+ InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ K8sApiConfig config = k8sApiConfigCodec.decode((ObjectNode) json, context);
+ assertThat(config, notNullValue());
+ return config;
+ }
+
+ private class MockCodecContext implements CodecContext {
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final CodecManager manager = new CodecManager();
+ private final Map<Class<?>, Object> services = new HashMap<>();
+
+ /**
+ * Constructs a new mock codec context.
+ */
+ public MockCodecContext() {
+ manager.activate();
+ }
+
+ @Override
+ public ObjectMapper mapper() {
+ return mapper;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> JsonCodec<T> codec(Class<T> entityClass) {
+ if (entityClass == K8sApiConfig.class) {
+ return (JsonCodec<T>) k8sApiConfigCodec;
+ }
+ return manager.getCodec(entityClass);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T getService(Class<T> serviceClass) {
+ return (T) services.get(serviceClass);
+ }
+
+ // for registering mock services
+ public <T> void registerService(Class<T> serviceClass, T impl) {
+ services.put(serviceClass, impl);
+ }
+ }
+}
diff --git a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/codec/K8sApiConfigJsonMatcher.java b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/codec/K8sApiConfigJsonMatcher.java
new file mode 100644
index 0000000..df1ac2f
--- /dev/null
+++ b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/codec/K8sApiConfigJsonMatcher.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.k8snode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.k8snode.api.K8sApiConfig;
+
+/**
+ * Hamcrest matcher for kubernetes API config.
+ */
+public final class K8sApiConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final K8sApiConfig k8sApiConfig;
+
+ private static final String SCHEME = "scheme";
+ private static final String IP_ADDRESS = "ipAddress";
+ private static final String PORT = "port";
+ private static final String TOKEN = "token";
+ private static final String CA_CERT_DATA = "caCertData";
+ private static final String CLIENT_CERT_DATA = "clientCertData";
+ private static final String CLIENT_KEY_DATA = "clientKeyData";
+
+ private K8sApiConfigJsonMatcher(K8sApiConfig k8sApiConfig) {
+ this.k8sApiConfig = k8sApiConfig;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check scheme
+ String jsonScheme = jsonNode.get(SCHEME).asText();
+ String scheme = k8sApiConfig.scheme().name();
+ if (!jsonScheme.equals(scheme)) {
+ description.appendText("scheme was " + jsonScheme);
+ return false;
+ }
+
+ // check IP address
+ String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
+ String ipAddress = k8sApiConfig.ipAddress().toString();
+ if (!jsonIpAddress.equals(ipAddress)) {
+ description.appendText("ipAddress was " + jsonIpAddress);
+ return false;
+ }
+
+ // check port
+ int jsonPort = jsonNode.get(PORT).asInt();
+ int port = k8sApiConfig.port();
+ if (jsonPort != port) {
+ description.appendText("port was " + jsonPort);
+ return false;
+ }
+
+ // check token
+ JsonNode jsonToken = jsonNode.get(TOKEN);
+ String token = k8sApiConfig.token();
+ if (jsonToken != null) {
+ if (!jsonToken.asText().equals(token)) {
+ description.appendText("token was " + jsonToken);
+ return false;
+ }
+ }
+
+ // check caCertData
+ JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA);
+ String caCertData = k8sApiConfig.caCertData();
+ if (jsonCaCertData != null) {
+ if (!jsonCaCertData.asText().equals(caCertData)) {
+ description.appendText("caCertData was " + jsonCaCertData);
+ return false;
+ }
+ }
+
+ // check clientCertData
+ JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA);
+ String clientCertData = k8sApiConfig.clientCertData();
+
+ if (jsonClientCertData != null) {
+ if (!jsonClientCertData.asText().equals(clientCertData)) {
+ description.appendText("clientCertData was " + jsonClientCertData);
+ return false;
+ }
+ }
+
+ // check clientKeyData
+ JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA);
+ String clientKeyData = k8sApiConfig.clientKeyData();
+
+ if (jsonClientKeyData != null) {
+ if (!jsonClientKeyData.asText().equals(clientKeyData)) {
+ description.appendText("clientKeyData was " + jsonClientKeyData);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(k8sApiConfig.toString());
+ }
+
+ /**
+ * Factory to allocate an k8sApiConfig matcher.
+ *
+ * @param config k8sApiConfig object we are looking for
+ * @return matcher
+ */
+ public static K8sApiConfigJsonMatcher matchesK8sApiConfig(K8sApiConfig config) {
+ return new K8sApiConfigJsonMatcher(config);
+ }
+}
diff --git a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/impl/K8sApiConfigManagerTest.java b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/impl/K8sApiConfigManagerTest.java
new file mode 100644
index 0000000..90f56c3
--- /dev/null
+++ b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/impl/K8sApiConfigManagerTest.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.k8snode.impl;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cluster.ClusterServiceAdapter;
+import org.onosproject.cluster.LeadershipServiceAdapter;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.k8snode.api.DefaultK8sApiConfig;
+import org.onosproject.k8snode.api.K8sApiConfig;
+import org.onosproject.k8snode.api.K8sApiConfigEvent;
+import org.onosproject.k8snode.api.K8sApiConfigListener;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.k8snode.api.K8sApiConfigEvent.Type.K8S_API_CONFIG_CREATED;
+import static org.onosproject.k8snode.api.K8sApiConfigEvent.Type.K8S_API_CONFIG_REMOVED;
+import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint;
+
+/**
+ * Unit tests for kubernetes API config manager.
+ */
+public class K8sApiConfigManagerTest {
+
+ private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+ private static final String ERR_SIZE = "Number of configs did not match";
+ private static final String ERR_NOT_MATCH = "Config did not match";
+ private static final String ERR_NOT_FOUND = "Config did not exist";
+
+ private K8sApiConfig apiConfig1;
+ private K8sApiConfig apiConfig2;
+ private K8sApiConfig apiConfig3;
+
+ private final TestK8sApiConfigListener testListener = new TestK8sApiConfigListener();
+
+ private K8sApiConfigManager target;
+ private DistributedK8sApiConfigStore configStore;
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+
+ apiConfig1 = DefaultK8sApiConfig.builder()
+ .scheme(K8sApiConfig.Scheme.HTTP)
+ .ipAddress(IpAddress.valueOf("10.10.10.2"))
+ .port(6443)
+ .build();
+ apiConfig2 = DefaultK8sApiConfig.builder()
+ .scheme(K8sApiConfig.Scheme.HTTPS)
+ .ipAddress(IpAddress.valueOf("10.10.10.3"))
+ .port(6443)
+ .token("token")
+ .caCertData("caCertData")
+ .clientCertData("clientCertData")
+ .clientKeyData("clientKeyData")
+ .build();
+ apiConfig3 = DefaultK8sApiConfig.builder()
+ .scheme(K8sApiConfig.Scheme.HTTP)
+ .ipAddress(IpAddress.valueOf("10.10.10.4"))
+ .port(8080)
+ .build();
+
+ configStore = new DistributedK8sApiConfigStore();
+ TestUtils.setField(configStore, "coreService", new TestCoreService());
+ TestUtils.setField(configStore, "storageService", new TestStorageService());
+ TestUtils.setField(configStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+ configStore.activate();
+
+ configStore.createApiConfig(apiConfig2);
+ configStore.createApiConfig(apiConfig3);
+
+ target = new K8sApiConfigManager();
+ target.storageService = new TestStorageService();
+ target.coreService = new TestCoreService();
+ target.clusterService = new TestClusterService();
+ target.leadershipService = new TestLeadershipService();
+ target.configStore = configStore;
+ target.addListener(testListener);
+ target.activate();
+ testListener.events.clear();
+ }
+
+ /**
+ * Clean up unit test.
+ */
+ @After
+ public void tearDown() {
+ target.removeListener(testListener);
+ target.deactivate();
+ configStore.deactivate();
+ configStore = null;
+ target = null;
+ }
+
+ /**
+ * Checks if creating and removing a config work well with proper events.
+ */
+ @Test
+ public void testCreateAndRemoveConfig() {
+ target.createApiConfig(apiConfig1);
+ assertEquals(ERR_SIZE, 3, target.apiConfigs().size());
+ assertNotNull(target.apiConfig(endpoint(apiConfig1)));
+
+ target.removeApiConfig(endpoint(apiConfig1));
+ assertEquals(ERR_SIZE, 2, target.apiConfigs().size());
+ assertNull(target.apiConfig(endpoint(apiConfig1)));
+
+ validateEvents(K8S_API_CONFIG_CREATED, K8S_API_CONFIG_REMOVED);
+ }
+
+ /**
+ * Checks if creating null config fails with proper exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateNullConfig() {
+ target.createApiConfig(null);
+ }
+
+ private static class TestK8sApiConfigListener implements K8sApiConfigListener {
+ private List<K8sApiConfigEvent> events = Lists.newArrayList();
+
+ @Override
+ public void event(K8sApiConfigEvent event) {
+ events.add(event);
+ }
+ }
+
+ /**
+ * Checks if creating a duplicated config fails with proper exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateDuplicateConfig() {
+ target.createApiConfig(apiConfig1);
+ target.createApiConfig(apiConfig1);
+ }
+
+ /**
+ * Checks if removing null config fails with proper exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testRemoveNullConfig() {
+ target.removeApiConfig(null);
+ }
+
+ private void validateEvents(Enum... types) {
+ int i = 0;
+ assertEquals("Number of events did not match", types.length, testListener.events.size());
+ for (Event event : testListener.events) {
+ assertEquals("Incorrect event received", types[i], event.type());
+ i++;
+ }
+ testListener.events.clear();
+ }
+
+ /**
+ * Checks if updating a null config fails with proper exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testUpdateNullConfig() {
+ target.updateApiConfig(null);
+ }
+
+ /**
+ * Checks if updating not existing config fails with proper exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testUpdateNotExistingConfig() {
+ target.updateApiConfig(apiConfig1);
+ }
+
+ /**
+ * Checks if getting all nodes method returns correct set of nodes.
+ */
+ @Test
+ public void testGetAllNodes() {
+ assertEquals(ERR_SIZE, 2, target.apiConfigs().size());
+ assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig2));
+ assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig3));
+ }
+
+ private static class TestCoreService extends CoreServiceAdapter {
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return TEST_APP_ID;
+ }
+ }
+
+ private class TestClusterService extends ClusterServiceAdapter {
+
+ }
+
+ private static class TestLeadershipService extends LeadershipServiceAdapter {
+
+ }
+}
diff --git a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java
index 61c2e2d..75d4433 100644
--- a/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java
+++ b/apps/k8s-node/app/src/test/java/org/onosproject/k8snode/web/K8sNodeWebResourceTest.java
@@ -23,10 +23,14 @@
import org.onlab.packet.IpAddress;
import org.onosproject.codec.CodecService;
import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.k8snode.api.DefaultK8sApiConfig;
import org.onosproject.k8snode.api.DefaultK8sNode;
+import org.onosproject.k8snode.api.K8sApiConfig;
+import org.onosproject.k8snode.api.K8sApiConfigAdminService;
import org.onosproject.k8snode.api.K8sNode;
import org.onosproject.k8snode.api.K8sNodeAdminService;
import org.onosproject.k8snode.api.K8sNodeState;
+import org.onosproject.k8snode.codec.K8sApiConfigCodec;
import org.onosproject.k8snode.codec.K8sNodeCodec;
import org.onosproject.net.DeviceId;
import org.onosproject.rest.resources.ResourceTest;
@@ -52,9 +56,13 @@
public class K8sNodeWebResourceTest extends ResourceTest {
final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class);
- private static final String PATH = "configure";
+ final K8sApiConfigAdminService mockK8sApiConfigAdminService =
+ createMock(K8sApiConfigAdminService.class);
+ private static final String NODE_PATH = "configure/node";
+ private static final String API_PATH = "configure/api";
private K8sNode k8sNode;
+ private K8sApiConfig k8sApiConfig;
/**
* Constructs a kubernetes node resource test instance.
@@ -71,9 +79,11 @@
final CodecManager codecService = new CodecManager();
codecService.activate();
codecService.registerCodec(K8sNode.class, new K8sNodeCodec());
+ codecService.registerCodec(K8sApiConfig.class, new K8sApiConfigCodec());
ServiceDirectory testDirectory =
new TestServiceDirectory()
.add(K8sNodeAdminService.class, mockK8sNodeAdminService)
+ .add(K8sApiConfigAdminService.class, mockK8sApiConfigAdminService)
.add(CodecService.class, codecService);
setServiceDirectory(testDirectory);
@@ -85,6 +95,16 @@
.intgBridge(DeviceId.deviceId("of:00000000000000a1"))
.state(K8sNodeState.INIT)
.build();
+
+ k8sApiConfig = DefaultK8sApiConfig.builder()
+ .scheme(K8sApiConfig.Scheme.HTTPS)
+ .ipAddress(IpAddress.valueOf("10.134.34.223"))
+ .port(6443)
+ .token("tokenMod")
+ .caCertData("caCertData")
+ .clientCertData("clientCertData")
+ .clientKeyData("clientKeyData")
+ .build();
}
/**
@@ -99,7 +119,7 @@
final WebTarget wt = target();
InputStream jsonStream = K8sNodeWebResourceTest.class
.getResourceAsStream("k8s-node-minion-config.json");
- Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -119,7 +139,7 @@
final WebTarget wt = target();
InputStream jsonStream = K8sNodeWebResourceTest.class
.getResourceAsStream("k8s-node-minion-config.json");
- Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -140,7 +160,7 @@
final WebTarget wt = target();
InputStream jsonStream = K8sNodeWebResourceTest.class
.getResourceAsStream("k8s-node-minion-config.json");
- Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
.put(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -160,7 +180,7 @@
final WebTarget wt = target();
InputStream jsonStream = K8sNodeWebResourceTest.class
.getResourceAsStream("k8s-node-minion-config.json");
- Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
.put(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -178,7 +198,7 @@
expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
replay(mockK8sNodeAdminService);
- String location = PATH + "/minion-node";
+ String location = NODE_PATH + "/minion-node";
final WebTarget wt = target();
Response response = wt.path(location).request(
@@ -199,7 +219,7 @@
expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
replay(mockK8sNodeAdminService);
- String location = PATH + "/minion-node";
+ String location = NODE_PATH + "/minion-node";
final WebTarget wt = target();
Response response = wt.path(location).request(
@@ -211,4 +231,132 @@
verify(mockK8sNodeAdminService);
}
+
+ /**
+ * Tests the results of the REST API POST method with creating new configs operation.
+ */
+ @Test
+ public void testCreateConfigsWithCreateOperation() {
+ expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
+ mockK8sApiConfigAdminService.createApiConfig(anyObject());
+ replay(mockK8sApiConfigAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = K8sNodeWebResourceTest.class
+ .getResourceAsStream("k8s-api-config.json");
+ Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .post(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(201));
+
+ verify(mockK8sApiConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API POST method without creating new configs operation.
+ */
+ @Test
+ public void testCreateConfigsWithoutCreateOperation() {
+ expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
+ replay(mockK8sApiConfigAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = K8sNodeWebResourceTest.class
+ .getResourceAsStream("k8s-api-config.json");
+ Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .post(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(201));
+
+ verify(mockK8sApiConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API PUT method with modifying the configs.
+ */
+ @Test
+ public void testUpdateConfigsWithModifyOperation() {
+ expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
+ .andReturn(k8sApiConfig).once();
+ mockK8sApiConfigAdminService.updateApiConfig(anyObject());
+ replay(mockK8sApiConfigAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = K8sNodeWebResourceTest.class
+ .getResourceAsStream("k8s-api-config.json");
+ Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .put(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(200));
+
+ verify(mockK8sApiConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API PUT method without modifying the configs.
+ */
+ @Test
+ public void testUpdateConfigsWithoutModifyOperation() {
+ expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
+ replay(mockK8sApiConfigAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = K8sNodeWebResourceTest.class
+ .getResourceAsStream("k8s-api-config.json");
+ Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .put(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(304));
+
+ verify(mockK8sApiConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API DELETE method with deleting the configs.
+ */
+ @Test
+ public void testDeleteConfigsWithDeletionOperation() {
+ expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
+ .andReturn(k8sApiConfig).once();
+ expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
+ .andReturn(k8sApiConfig).once();
+ replay(mockK8sApiConfigAdminService);
+
+ String location = API_PATH + "/https://test:8663";
+
+ final WebTarget wt = target();
+ Response response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).delete();
+
+ final int status = response.getStatus();
+
+ assertThat(status, is(204));
+
+ verify(mockK8sApiConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API DELETE method without deleting the configs.
+ */
+ @Test
+ public void testDeleteConfigsWithoutDeletionOperation() {
+ expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
+ replay(mockK8sApiConfigAdminService);
+
+ String location = API_PATH + "/https://test:8663";
+
+ final WebTarget wt = target();
+ Response response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).delete();
+
+ final int status = response.getStatus();
+
+ assertThat(status, is(304));
+
+ verify(mockK8sApiConfigAdminService);
+ }
}