http://git-wip-us.apache.org/repos/asf/camel/blob/8ad60f0f/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraProducerTest.java b/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraProducerTest.java new file mode 100644 index 0000000..1bf4df8 --- /dev/null +++ b/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraProducerTest.java @@ -0,0 +1,314 @@ +/** + * 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.gora; + +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Message; +import org.apache.camel.component.gora.utils.GoraUtils; +import org.apache.gora.persistency.Persistent; +import org.apache.gora.persistency.StateManager; +import org.apache.gora.query.Query; +import org.apache.gora.query.impl.QueryBase; +import org.apache.gora.store.DataStore; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.internal.verification.VerificationModeFactory; +import org.mockito.stubbing.OngoingStubbing; +import org.mockito.verification.VerificationMode; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.*; + + +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.atMost; +import static org.mockito.Mockito.verify; +import static org.mockito.internal.verification.VerificationModeFactory.times; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; + +/** + * GORA Producer Tests + * + * TODO: <b>NOTE:</b> Query methods does not yet has tests + * + * @author ipolyzos + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(GoraUtils.class) +public class GoraProducerTest extends GoraTestSupport { + + /** + * Mock CamelExchange + */ + private Exchange mockCamelExchange; + + /** + * Mock Gora Endpoint + */ + private GoraEndpoint mockGoraEndpoint; + + /** + * Mock Gora Configuration + */ + private GoraConfiguration mockGoraConfiguration; + + /** + * Mock Camel Message + */ + private Message mockCamelMessage; + + /** + * Mock Gora DataStore + */ + private DataStore mockDatastore; + + @Before + public void setUp() { + + //setup mocks + mockCamelExchange = mock(Exchange.class); + mockGoraEndpoint = mock(GoraEndpoint.class); + mockGoraConfiguration = mock(GoraConfiguration.class); + mockCamelMessage = mock(Message.class); + mockDatastore = mock(DataStore.class); + + //setup default conditions + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelExchange.getPattern()).thenReturn(ExchangePattern.InOnly); + } + + @Test(expected = RuntimeException.class) + public void processShouldThrowExceptionIfOperationIsNull() throws Exception { + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + } + + @Test(expected = RuntimeException.class) + public void shouldThrowExceptionIfOperationIsUnknown() throws Exception { + + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("dah"); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atMost(1)).getIn(); + verify(mockCamelMessage, atMost(1)).getHeader(GoraAttribute.GORA_OPERATION.value); + } + + @Test + public void shouldInvokeDastorePut() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("PUT"); + + final Long sampleKey = new Long(2); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_KEY.value)).thenReturn(sampleKey); + + final Persistent sampleValue = mock(Persistent.class); + when(mockCamelMessage.getBody(Persistent.class)).thenReturn(sampleValue); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_KEY.value); + verify(mockCamelMessage, atLeastOnce()).getBody(Persistent.class); + verify(mockDatastore, atMost(1)).put(sampleKey,sampleValue); + } + + @Test + public void shouldInvokeDastoreGet() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("get"); + + final Long sampleKey = new Long(2); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_KEY.value)).thenReturn(sampleKey); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_KEY.value); + verify(mockDatastore, atMost(1)).get(sampleKey); + } + + @Test + public void shouldInvokeDatastoreDelete() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("dEletE"); + + final Long sampleKey = new Long(2); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_KEY.value)).thenReturn(sampleKey); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_KEY.value); + verify(mockDatastore, atMost(1)).delete(sampleKey); + } + + @Test + public void shouldInvokeDastoreSchemaExists() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("schemaExists"); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockDatastore, atMost(1)).schemaExists(); + } + + @Test + public void shouldInvokeDastoreCreateSchema() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("createSchema"); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockDatastore, atMost(1)).createSchema(); + } + + @Test + public void shouldInvokeDastoreGetSchemaName() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("GetSchemANamE"); + + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockDatastore, atMost(1)).getSchemaName(); + } + + @Test + public void shouldInvokeDatastoreDeleteSchema() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("DeleteSChEmA"); + + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockDatastore, atMost(1)).deleteSchema(); + } + + @Test + public void shouldInvokeDatastoreQuery() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("query"); + + final Map<String, Object> mockProperties = mock(Map.class); + when(mockCamelMessage.getHeaders()).thenReturn(mockProperties); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + mockStatic(GoraUtils.class); + + final Query mockQuery = mock(QueryBase.class); + when(GoraUtils.constractQueryFromPropertiesMap(mockProperties, mockDatastore, mockGoraConfiguration)).thenReturn(mockQuery); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockQuery, atLeastOnce()).execute(); + verifyStatic(times(1)); + } + + @Test + public void shouldInvokeDatastoreDeleteByQuery() throws Exception { + + when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage); + when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("deleteByQuery"); + + final Map<String, Object> mockProperties = mock(Map.class); + when(mockCamelMessage.getHeaders()).thenReturn(mockProperties); + + final Message outMessage = mock(Message.class); + when(mockCamelExchange.getOut()).thenReturn(outMessage); + + mockStatic(GoraUtils.class); + + final Query mockQuery = mock(QueryBase.class); + when(GoraUtils.constractQueryFromPropertiesMap(mockProperties, mockDatastore, mockGoraConfiguration)).thenReturn(mockQuery); + + final GoraProducer producer = new GoraProducer(mockGoraEndpoint,mockGoraConfiguration,mockDatastore); + producer.process(mockCamelExchange); + + verify(mockCamelExchange, atLeastOnce()).getIn(); + verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value); + verify(mockDatastore, atMost(1)).deleteByQuery(mockQuery); + verifyStatic(times(1)); + } + +}
http://git-wip-us.apache.org/repos/asf/camel/blob/8ad60f0f/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraTestSupport.java b/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraTestSupport.java new file mode 100644 index 0000000..939763d --- /dev/null +++ b/components/camel-gora/src/test/java/org/apache/camel/component/gora/GoraTestSupport.java @@ -0,0 +1,29 @@ +/** + * 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.gora; + +import org.apache.camel.test.junit4.CamelTestSupport; + +/** + * GORA Test Support + * + * @author ipolyzos + */ +public class GoraTestSupport extends CamelTestSupport { + //TODO +} http://git-wip-us.apache.org/repos/asf/camel/blob/8ad60f0f/components/camel-gora/src/test/java/org/apache/camel/component/gora/utils/GoraUtilsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-gora/src/test/java/org/apache/camel/component/gora/utils/GoraUtilsTest.java b/components/camel-gora/src/test/java/org/apache/camel/component/gora/utils/GoraUtilsTest.java new file mode 100644 index 0000000..c2c4ecef --- /dev/null +++ b/components/camel-gora/src/test/java/org/apache/camel/component/gora/utils/GoraUtilsTest.java @@ -0,0 +1,56 @@ +/** + * 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.gora.utils; + +import org.apache.camel.component.gora.GoraAttribute; +import org.apache.camel.component.gora.GoraConfiguration; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * GORA Utils Tests + * + * @author ipolyzos + */ +public class GoraUtilsTest { + + @Test + public void configurationExistShouldSucceedtIfMethodExist() throws Exception { + + final GoraConfiguration conf = new GoraConfiguration(); + assertTrue(GoraUtils.configurationExist(GoraAttribute.GORA_QUERY_LIMIT,conf)); + } + + @Test(expected = NoSuchMethodException.class) + public void configurationExistShouldThrowExceptionIfMethodDoesNotExist() throws Exception { + + final GoraConfiguration conf = new GoraConfiguration(); + GoraUtils.configurationExist(GoraAttribute.GORA_KEY,conf); + } + + @Test + public void getAttributeAsLongShouldSReturnTheCorrectValue() throws Exception { + + final GoraConfiguration conf = new GoraConfiguration(); + conf.setLimit(3L); + assertEquals(new Long(3), GoraUtils.getAttributeAsLong(GoraAttribute.GORA_QUERY_LIMIT, conf)); + } +}