| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.osgi; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMBridge; |
| 19 | import com.datastax.driver.osgi.api.MailboxException; |
| 20 | import com.datastax.driver.osgi.api.MailboxMessage; |
| 21 | import com.datastax.driver.osgi.api.MailboxService; |
| 22 | import org.ops4j.pax.exam.Configuration; |
| 23 | import org.ops4j.pax.exam.Option; |
| 24 | import org.ops4j.pax.exam.options.CompositeOption; |
| 25 | import org.ops4j.pax.exam.options.MavenArtifactProvisionOption; |
| 26 | import org.ops4j.pax.exam.options.UrlProvisionOption; |
| 27 | import org.ops4j.pax.exam.testng.listener.PaxExam; |
| 28 | import org.testng.annotations.Listeners; |
| 29 | import org.testng.annotations.Test; |
| 30 | |
| 31 | import javax.inject.Inject; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Collection; |
| 34 | import java.util.GregorianCalendar; |
| 35 | |
| 36 | import static com.datastax.driver.osgi.VersionProvider.projectVersion; |
| 37 | import static org.ops4j.pax.exam.CoreOptions.*; |
| 38 | import static org.testng.Assert.assertEquals; |
| 39 | |
| 40 | @Listeners({CCMBridgeListener.class, PaxExam.class}) |
| 41 | @Test(groups = "short") |
| 42 | public class MailboxServiceIT { |
| 43 | @Inject |
| 44 | MailboxService service; |
| 45 | |
| 46 | private UrlProvisionOption driverBundle() { |
| 47 | return driverBundle(false); |
| 48 | } |
| 49 | |
| 50 | private UrlProvisionOption driverBundle(boolean useShaded) { |
| 51 | String classifier = useShaded ? "-shaded" : ""; |
| 52 | return bundle("reference:file:../../driver-core/target/cassandra-driver-core-" + projectVersion() + classifier + ".jar"); |
| 53 | } |
| 54 | |
| 55 | private MavenArtifactProvisionOption guavaBundle() { |
| 56 | return mavenBundle("com.google.guava", "guava", "14.0.1"); |
| 57 | } |
| 58 | |
| 59 | private CompositeOption nettyBundles() { |
| 60 | final String nettyVersion = "4.0.27.Final"; |
| 61 | return new CompositeOption() { |
| 62 | |
| 63 | @Override |
| 64 | public Option[] getOptions() { |
| 65 | return options( |
| 66 | mavenBundle("io.netty", "netty-buffer", nettyVersion), |
| 67 | mavenBundle("io.netty", "netty-codec", nettyVersion), |
| 68 | mavenBundle("io.netty", "netty-common", nettyVersion), |
| 69 | mavenBundle("io.netty", "netty-handler", nettyVersion), |
| 70 | mavenBundle("io.netty", "netty-transport", nettyVersion) |
| 71 | ); |
| 72 | } |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | private CompositeOption defaultOptions() { |
| 77 | return new CompositeOption() { |
| 78 | |
| 79 | @Override |
| 80 | public Option[] getOptions() { |
| 81 | return options( |
| 82 | systemProperty("cassandra.contactpoints").value(CCMBridge.IP_PREFIX + 1), |
| 83 | bundle("reference:file:target/classes"), |
| 84 | mavenBundle("com.codahale.metrics", "metrics-core", "3.0.2"), |
| 85 | mavenBundle("org.slf4j", "slf4j-api", "1.7.5"), |
| 86 | mavenBundle("org.slf4j", "slf4j-simple", "1.7.5").noStart(), |
| 87 | systemPackages("org.testng", "org.junit", "org.junit.runner", "org.junit.runner.manipulation", |
| 88 | "org.junit.runner.notification", "com.jcabi.manifests") |
| 89 | ); |
| 90 | } |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | @Configuration |
| 95 | public Option[] shadedConfig() { |
| 96 | return options( |
| 97 | driverBundle(true), |
| 98 | guavaBundle(), |
| 99 | defaultOptions() |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | @Configuration |
| 104 | public Option[] defaultConfig() { |
| 105 | return options( |
| 106 | driverBundle(), |
| 107 | guavaBundle(), |
| 108 | nettyBundles(), |
| 109 | defaultOptions() |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | @Configuration |
| 114 | public Option[] guava15Config() { |
| 115 | return options( |
| 116 | driverBundle(), |
| 117 | nettyBundles(), |
| 118 | guavaBundle().version("15.0"), |
| 119 | defaultOptions() |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | @Configuration |
| 124 | public Option[] guava16Config() { |
| 125 | return options( |
| 126 | driverBundle(), |
| 127 | nettyBundles(), |
| 128 | guavaBundle().version("16.0.1"), |
| 129 | defaultOptions() |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | @Configuration |
| 134 | public Option[] guava17Config() { |
| 135 | return options( |
| 136 | driverBundle(), |
| 137 | nettyBundles(), |
| 138 | guavaBundle().version("17.0"), |
| 139 | defaultOptions() |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | @Configuration |
| 144 | public Option[] guava18Config() { |
| 145 | return options( |
| 146 | driverBundle(), |
| 147 | nettyBundles(), |
| 148 | guavaBundle().version("18.0"), |
| 149 | defaultOptions() |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * <p> |
| 155 | * Exercises a 'mailbox' service provided by an OSGi bundle that depends on the driver. Ensures that |
| 156 | * queries can be made through the service with the current given configuration. |
| 157 | * </p> |
| 158 | * <p/> |
| 159 | * The following configurations are tried (defined via methods with the @Configuration annotation): |
| 160 | * <ol> |
| 161 | * <li>Default bundle (Driver with all of it's dependencies)</li> |
| 162 | * <li>Shaded bundle (Driver with netty shaded)</li> |
| 163 | * <li>With Guava 15</li> |
| 164 | * <li>With Guava 16</li> |
| 165 | * <li>With Guava 17</li> |
| 166 | * <li>With Guava 18</li> |
| 167 | * </ol> |
| 168 | * |
| 169 | * @test_category packaging |
| 170 | * @expected_result Can create, retrieve and delete data using the mailbox service. |
| 171 | * @jira_ticket JAVA-620 |
| 172 | * @since 2.0.10, 2.1.5 |
| 173 | */ |
| 174 | public void service_api_functional() throws MailboxException { |
| 175 | // Insert some data into mailbox for a particular user. |
| 176 | String recipient = "user@datastax.com"; |
| 177 | |
| 178 | try { |
| 179 | Collection<MailboxMessage> inMessages = new ArrayList<MailboxMessage>(); |
| 180 | for (int i = 0; i < 30; i++) { |
| 181 | MailboxMessage message = new MailboxMessage(recipient, new GregorianCalendar(2015, 1, i).getTime(), recipient, "" + i); |
| 182 | inMessages.add(message); |
| 183 | service.sendMessage(message); |
| 184 | } |
| 185 | |
| 186 | Collection<MailboxMessage> messages = service.getMessages(recipient); |
| 187 | |
| 188 | assertEquals(messages, inMessages); |
| 189 | } finally { |
| 190 | service.clearMailbox(recipient); |
| 191 | } |
| 192 | } |
| 193 | } |