You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/12/17 11:53:41 UTC
[02/17] camel git commit: CAMEL-9748: camel-openstack nova
http://git-wip-us.apache.org/repos/asf/camel/blob/e725ee77/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/KeypairProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/KeypairProducerTest.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/KeypairProducerTest.java
new file mode 100644
index 0000000..1c05caa
--- /dev/null
+++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/KeypairProducerTest.java
@@ -0,0 +1,121 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.openstack.nova;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.camel.component.openstack.nova.producer.KeypairProducer;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.mockito.ArgumentCaptor;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.openstack4j.model.compute.Keypair;
+import org.openstack4j.openstack.compute.domain.NovaKeypair;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class KeypairProducerTest extends NovaProducerTestSupport {
+ private static final String KEYPAIR_NAME = "keypairName";
+
+ @Mock
+ private Keypair osTestKeypair;
+
+ private Keypair dummyKeypair;
+
+ @Before
+ public void setUp() {
+ producer = new KeypairProducer(endpoint, client);
+ dummyKeypair = createDummyKeypair();
+
+ when(keypairService.get(Matchers.anyString())).thenReturn(osTestKeypair);
+ when(keypairService.create(Matchers.anyString(), Matchers.anyString())).thenReturn(osTestKeypair);
+
+ List<org.openstack4j.model.compute.Keypair> getAllList = new ArrayList<>();
+ getAllList.add(osTestKeypair);
+ getAllList.add(osTestKeypair);
+ doReturn(getAllList).when(keypairService).list();
+
+ when(osTestKeypair.getName()).thenReturn(dummyKeypair.getName());
+ when(osTestKeypair.getPublicKey()).thenReturn(dummyKeypair.getPublicKey());
+ }
+
+ @Test
+ public void createKeypair() throws Exception {
+ final String fingerPrint = "fp";
+ final String privatecKey = "prk";
+ when(osTestKeypair.getName()).thenReturn(KEYPAIR_NAME);
+ when(osTestKeypair.getPublicKey()).thenReturn(dummyKeypair.getPublicKey());
+ when(osTestKeypair.getFingerprint()).thenReturn(fingerPrint);
+ when(osTestKeypair.getPrivateKey()).thenReturn(privatecKey);
+
+ msg.setHeader(NovaConstants.OPERATION, NovaConstants.CREATE);
+ msg.setHeader(NovaConstants.NAME, KEYPAIR_NAME);
+
+ producer.process(exchange);
+
+ ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class);
+ ArgumentCaptor<String> keypairCaptor = ArgumentCaptor.forClass(String.class);
+ verify(keypairService).create(nameCaptor.capture(), keypairCaptor.capture());
+
+ assertEquals(KEYPAIR_NAME, nameCaptor.getValue());
+ assertNull(keypairCaptor.getValue());
+
+ Keypair result = msg.getBody(Keypair.class);
+ assertEquals(fingerPrint, result.getFingerprint());
+ assertEquals(privatecKey, result.getPrivateKey());
+ assertEquals(dummyKeypair.getName(), result.getName());
+ assertEquals(dummyKeypair.getPublicKey(), result.getPublicKey());
+
+ }
+
+ @Test
+ public void createKeypairFromExisting() throws Exception {
+ msg.setHeader(NovaConstants.OPERATION, NovaConstants.CREATE);
+ msg.setHeader(NovaConstants.NAME, KEYPAIR_NAME);
+ String key = "existing public key string";
+ when(osTestKeypair.getPublicKey()).thenReturn(key);
+ msg.setBody(key);
+
+ producer.process(exchange);
+
+ ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class);
+ ArgumentCaptor<String> keypairCaptor = ArgumentCaptor.forClass(String.class);
+ verify(keypairService).create(nameCaptor.capture(), keypairCaptor.capture());
+
+ assertEquals(KEYPAIR_NAME, nameCaptor.getValue());
+ assertEquals(key, keypairCaptor.getValue());
+
+ Keypair result = msg.getBody(Keypair.class);
+ assertEquals(dummyKeypair.getName(), result.getName());
+ assertEquals(dummyKeypair.getFingerprint(), result.getFingerprint());
+ assertEquals(dummyKeypair.getPrivateKey(), result.getPrivateKey());
+ assertEquals(key, result.getPublicKey());
+ }
+
+ private Keypair createDummyKeypair(){
+ return new NovaKeypair().create(KEYPAIR_NAME, "string contains private key");
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e725ee77/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/NovaProducerTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/NovaProducerTestSupport.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/NovaProducerTestSupport.java
new file mode 100644
index 0000000..c2cb7cd
--- /dev/null
+++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/NovaProducerTestSupport.java
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.openstack.nova;
+
+import static org.mockito.Mockito.when;
+
+import org.apache.camel.component.openstack.AbstractProducerTestSupport;
+
+import org.junit.Before;
+
+import org.mockito.Mock;
+import org.openstack4j.api.compute.ComputeService;
+import org.openstack4j.api.compute.FlavorService;
+import org.openstack4j.api.compute.KeypairService;
+import org.openstack4j.api.compute.ServerService;
+
+public class NovaProducerTestSupport extends AbstractProducerTestSupport {
+
+ @Mock
+ protected NovaEndpoint endpoint;
+
+ @Mock
+ protected ComputeService computeService;
+
+ @Mock
+ FlavorService flavorService;
+
+ @Mock
+ ServerService serverService;
+
+ @Mock
+ KeypairService keypairService;
+
+ @Before
+ public void setUpComputeService(){
+ when(client.compute()).thenReturn(computeService);
+ when(computeService.flavors()).thenReturn(flavorService);
+ when(computeService.servers()).thenReturn(serverService);
+ when(computeService.keypairs()).thenReturn(keypairService);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e725ee77/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/ServerProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/ServerProducerTest.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/ServerProducerTest.java
new file mode 100644
index 0000000..9e7315e
--- /dev/null
+++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/nova/ServerProducerTest.java
@@ -0,0 +1,155 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.openstack.nova;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.camel.component.openstack.nova.producer.ServerProducer;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.openstack4j.api.Builders;
+import org.openstack4j.model.common.ActionResponse;
+import org.openstack4j.model.compute.Action;
+import org.openstack4j.model.compute.Server;
+import org.openstack4j.model.compute.ServerCreate;
+import org.openstack4j.openstack.compute.domain.NovaServerCreate;
+
+import java.util.UUID;
+
+public class ServerProducerTest extends NovaProducerTestSupport {
+ @Mock
+ private org.openstack4j.model.compute.Server testOSServer;
+
+ private ServerCreate dummyServer;
+
+ @Before
+ public void setUp() {
+ producer = new ServerProducer(endpoint, client);
+
+ when(serverService.boot(any(NovaServerCreate.class))).thenReturn(testOSServer);
+
+ dummyServer = createDummyServer();
+ initServerMock();
+ }
+
+ @Test
+ public void createServer() throws Exception {
+ when(endpoint.getOperation()).thenReturn(NovaConstants.CREATE);
+ final String expectedFlavorID = UUID.randomUUID().toString();
+ when(testOSServer.getId()).thenReturn(expectedFlavorID);
+ msg.setBody(dummyServer);
+ producer.process(exchange);
+ final Server created = msg.getBody(Server.class);
+ checkCreatedServer(dummyServer, created);
+ }
+
+ @Test
+ public void createServerWithHeaders() throws Exception {
+ final String expectedFlavorID = UUID.randomUUID().toString();
+ when(testOSServer.getId()).thenReturn(expectedFlavorID);
+
+ msg.setHeader(NovaConstants.OPERATION, NovaConstants.CREATE);
+ msg.setHeader(NovaConstants.NAME, dummyServer.getName());
+ msg.setHeader(NovaConstants.FLAVOR_ID, dummyServer.getFlavorRef());
+ msg.setHeader(NovaConstants.IMAGE_ID, dummyServer.getImageRef());
+
+ producer.process(exchange);
+
+ final Server created = msg.getBody(Server.class);
+
+ checkCreatedServer(dummyServer, created);
+ }
+
+ @Test
+ public void serverAction() throws Exception {
+ when(serverService.action(anyString(), any(Action.class))).thenReturn(ActionResponse.actionSuccess());
+ when(endpoint.getOperation()).thenReturn(NovaConstants.ACTION);
+ String id = "myID";
+ msg.setHeader(NovaConstants.ACTION, Action.PAUSE);
+ msg.setHeader(NovaConstants.ID, id);
+ producer.process(exchange);
+
+ ArgumentCaptor<Action> actionArgumentCaptor = ArgumentCaptor.forClass(Action.class);
+ ArgumentCaptor<String> idArgumentCaptor = ArgumentCaptor.forClass(String.class);
+ verify(serverService).action(idArgumentCaptor.capture(), actionArgumentCaptor.capture());
+
+ assertEquals(id, idArgumentCaptor.getValue());
+ assertTrue(actionArgumentCaptor.getValue() == Action.PAUSE);
+ assertFalse(msg.isFault());
+ assertNull(msg.getBody());
+
+ //test fail
+ final String failReason = "fr";
+ when(serverService.action(anyString(), any(Action.class))).thenReturn(ActionResponse.actionFailed(failReason, 401));
+ producer.process(exchange);
+ assertTrue(msg.isFault());
+ assertTrue(msg.getBody(String.class).contains(failReason));
+ }
+
+ @Test
+ public void createSnapshot() throws Exception {
+ String id = "myID";
+ String snapshotName = "mySnapshot";
+
+ msg.setHeader(NovaConstants.OPERATION, NovaConstants.CREATE_SNAPSHOT);
+ msg.setHeader(NovaConstants.NAME, snapshotName);
+ msg.setHeader(NovaConstants.ID, id);
+ producer.process(exchange);
+
+ ArgumentCaptor<String> snapshot = ArgumentCaptor.forClass(String.class);
+ ArgumentCaptor<String> idCaptor = ArgumentCaptor.forClass(String.class);
+ verify(serverService).createSnapshot(idCaptor.capture(), snapshot.capture());
+
+ assertEquals(id, idCaptor.getValue());
+ assertEquals(snapshotName, snapshot.getValue());
+ }
+
+ private void initServerMock() {
+ when(testOSServer.getId()).thenReturn(UUID.randomUUID().toString());
+ when(testOSServer.getName()).thenReturn(dummyServer.getName());
+ when(testOSServer.getFlavorId()).thenReturn(dummyServer.getFlavorRef());
+ when(testOSServer.getImageId()).thenReturn(dummyServer.getImageRef());
+ }
+
+ private ServerCreate createDummyServer() {
+ return Builders.server()
+ .name("MyCoolServer")
+ .flavor("flavorID")
+ .image("imageID").build();
+ }
+
+ private void checkCreatedServer(ServerCreate old, Server created) {
+ assertEquals(old.getName(), created.getName());
+ assertEquals(old.getFlavorRef(), created.getFlavorId());
+ assertEquals(old.getImageRef(), created.getImageId());
+
+ assertNotNull(created.getId());
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e725ee77/components/pom.xml
----------------------------------------------------------------------
diff --git a/components/pom.xml b/components/pom.xml
index d61dba1..a241f5d 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -199,6 +199,7 @@
<module>camel-ognl</module>
<module>camel-olingo2</module>
<module>camel-openshift</module>
+ <module>camel-openstack</module>
<module>camel-optaplanner</module>
<module>camel-paho</module>
<module>camel-paxlogging</module>
http://git-wip-us.apache.org/repos/asf/camel/blob/e725ee77/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index c3b5c88..1f39f7e 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -473,6 +473,7 @@
<openjpa-version>2.4.1</openjpa-version>
<openshift-java-client-version>2.7.0.Final</openshift-java-client-version>
<openshift-client-version>1.4.27</openshift-client-version>
+ <openstack4j-version>3.0.2</openstack4j-version>
<optaplanner-version>6.5.0.Final</optaplanner-version>
<oro-bundle-version>2.0.8_6</oro-bundle-version>
<oscache-bundle-version>2.4_5</oscache-bundle-version>
@@ -1518,6 +1519,11 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
+ <artifactId>camel-openstack</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
<artifactId>camel-optaplanner</artifactId>
<version>${project.version}</version>
</dependency>