mirror of https://github.com/apache/jclouds.git
Added a 'clear tweets' option to heroku-tweetstore. Using unique container names in unit tests.
This commit is contained in:
parent
b9188872f2
commit
6f75c85313
|
@ -45,6 +45,7 @@ import org.jclouds.demo.paas.service.taskqueue.TaskQueue;
|
|||
import org.jclouds.demo.tweetstore.config.util.CredentialsCollector;
|
||||
import org.jclouds.demo.tweetstore.config.util.PropertiesLoader;
|
||||
import org.jclouds.demo.tweetstore.controller.AddTweetsController;
|
||||
import org.jclouds.demo.tweetstore.controller.ClearTweetsController;
|
||||
import org.jclouds.demo.tweetstore.controller.EnqueueStoresController;
|
||||
import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
|
||||
|
||||
|
@ -138,6 +139,7 @@ public class GuiceServletConfig extends GuiceServletContextListener {
|
|||
serve("/store/*").with(StoreTweetsController.class);
|
||||
serve("/tweets/*").with(AddTweetsController.class);
|
||||
serve("/stores/*").with(EnqueueStoresController.class);
|
||||
serve("/clear/*").with(ClearTweetsController.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.jclouds.demo.tweetstore.controller;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.jclouds.blobstore.BlobStoreContext;
|
||||
import org.jclouds.demo.tweetstore.reference.TweetStoreConstants;
|
||||
import org.jclouds.logging.Logger;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Grab tweets related to me and store them into blobstores
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class ClearTweetsController extends HttpServlet {
|
||||
/** The serialVersionUID */
|
||||
private static final long serialVersionUID = 7215420527854203714L;
|
||||
|
||||
private final Map<String, BlobStoreContext> contexts;
|
||||
private final String container;
|
||||
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
@Inject
|
||||
@VisibleForTesting
|
||||
public ClearTweetsController(Map<String, BlobStoreContext> contexts,
|
||||
@Named(TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER) String container) {
|
||||
this.container = container;
|
||||
this.contexts = contexts;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void clearContainer(String contextName) {
|
||||
BlobStoreContext context = checkNotNull(contexts.get(contextName),
|
||||
"no context for %s in %s", contextName, contexts.keySet());
|
||||
try {
|
||||
context.getBlobStore().clearContainer(container);
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Error clearing tweets in %s/%s", container, context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
if (nullToEmpty(request.getHeader("X-Originator")).equals("admin")) {
|
||||
try {
|
||||
String contextName = checkNotNull(request.getHeader("context"), "missing header context");
|
||||
logger.info("clearing tweets in %s/%s", container, contextName);
|
||||
clearContainer(contextName);
|
||||
logger.debug("done clearing tweets");
|
||||
response.setContentType(MediaType.TEXT_PLAIN);
|
||||
response.getWriter().println("Done!");
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Error clearing tweets");
|
||||
throw new ServletException(e);
|
||||
}
|
||||
} else {
|
||||
response.sendError(401);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,7 +63,7 @@ public class AddTweetsControllerTest {
|
|||
}
|
||||
|
||||
public void testStoreTweets() throws IOException, InterruptedException, ExecutionException {
|
||||
String container = "container";
|
||||
String container = AddTweetsControllerTest.class.getName() + "#container";
|
||||
Map<String, BlobStoreContext> contexts = createServices(container);
|
||||
|
||||
ServiceToStoredTweetStatuses function = new ServiceToStoredTweetStatuses(contexts, container);
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.jclouds.demo.tweetstore.controller;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.jclouds.ContextBuilder;
|
||||
import org.jclouds.blobstore.BlobStoreContext;
|
||||
import org.jclouds.blobstore.TransientApiMetadata;
|
||||
import org.jclouds.blobstore.domain.Blob;
|
||||
import org.jclouds.demo.tweetstore.reference.TweetStoreConstants;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
/**
|
||||
* Tests behavior of {@code AddTweetsController}
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit")
|
||||
public class ClearTweetsControllerTest {
|
||||
|
||||
Map<String, BlobStoreContext> createBlobStores(String container) throws InterruptedException, ExecutionException {
|
||||
TransientApiMetadata transientApiMetadata = TransientApiMetadata.builder().build();
|
||||
Map<String, BlobStoreContext> contexts = ImmutableMap.<String, BlobStoreContext>of(
|
||||
"test1", ContextBuilder.newBuilder(transientApiMetadata).build(BlobStoreContext.class),
|
||||
"test2", ContextBuilder.newBuilder(transientApiMetadata).build(BlobStoreContext.class));
|
||||
for (BlobStoreContext blobstore : contexts.values()) {
|
||||
blobstore.getBlobStore().createContainerInLocation(null, container);
|
||||
Blob blob = blobstore.getAsyncBlobStore().blobBuilder("1").build();
|
||||
blob.getMetadata().getUserMetadata().put(TweetStoreConstants.SENDER_NAME, "frank");
|
||||
blob.setPayload("I love beans!");
|
||||
blobstore.getBlobStore().putBlob(container, blob);
|
||||
}
|
||||
return contexts;
|
||||
}
|
||||
|
||||
public void testClearTweets() throws IOException, InterruptedException, ExecutionException {
|
||||
String container = ClearTweetsControllerTest.class.getName() + "#container";
|
||||
Map<String, BlobStoreContext> contexts = createBlobStores(container);
|
||||
|
||||
ClearTweetsController controller = new ClearTweetsController(contexts,
|
||||
container);
|
||||
controller.clearContainer("test1");
|
||||
controller.clearContainer("test2");
|
||||
|
||||
for (BlobStoreContext context : contexts.values()) {
|
||||
assertEquals(context.getBlobStore().countBlobs(container), 0, context.toString());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -57,20 +57,21 @@ public class StoreTweetsControllerTest {
|
|||
return createMock(Twitter.class);
|
||||
}
|
||||
|
||||
Map<String, BlobStoreContext> createBlobStores() throws InterruptedException, ExecutionException {
|
||||
Map<String, BlobStoreContext> createBlobStores(String container) throws InterruptedException, ExecutionException {
|
||||
TransientApiMetadata transientApiMetadata = TransientApiMetadata.builder().build();
|
||||
Map<String, BlobStoreContext> contexts = ImmutableMap.<String, BlobStoreContext>of(
|
||||
"test1", ContextBuilder.newBuilder(transientApiMetadata).build(BlobStoreContext.class),
|
||||
"test2", ContextBuilder.newBuilder(transientApiMetadata).build(BlobStoreContext.class));
|
||||
for (BlobStoreContext blobstore : contexts.values()) {
|
||||
blobstore.getAsyncBlobStore().createContainerInLocation(null, "favo").get();
|
||||
blobstore.getAsyncBlobStore().createContainerInLocation(null, container).get();
|
||||
}
|
||||
return contexts;
|
||||
}
|
||||
|
||||
public void testStoreTweets() throws IOException, InterruptedException, ExecutionException {
|
||||
Map<String, BlobStoreContext> stores = createBlobStores();
|
||||
StoreTweetsController function = new StoreTweetsController(stores, "favo", createTwitter());
|
||||
String container = StoreTweetsControllerTest.class.getName() + "#container";
|
||||
Map<String, BlobStoreContext> stores = createBlobStores(container);
|
||||
StoreTweetsController function = new StoreTweetsController(stores, container, createTwitter());
|
||||
|
||||
User frank = createMock(User.class);
|
||||
expect(frank.getScreenName()).andReturn("frank").atLeastOnce();
|
||||
|
@ -102,7 +103,7 @@ public class StoreTweetsControllerTest {
|
|||
verify(jimmyStatus);
|
||||
|
||||
for (Entry<String, BlobStoreContext> entry : stores.entrySet()) {
|
||||
BlobMap map = entry.getValue().createBlobMap("favo");
|
||||
BlobMap map = entry.getValue().createBlobMap(container);
|
||||
Blob frankBlob = map.get("1");
|
||||
assertEquals(frankBlob.getMetadata().getName(), "1");
|
||||
assertEquals(frankBlob.getMetadata().getUserMetadata().get(TweetStoreConstants.SENDER_NAME), "frank");
|
||||
|
|
|
@ -41,10 +41,11 @@ import org.testng.annotations.Test;
|
|||
public class KeyToStoredTweetStatusTest {
|
||||
|
||||
BlobMap createMap() throws InterruptedException, ExecutionException {
|
||||
BlobStoreContext context =
|
||||
ContextBuilder.newBuilder(TransientApiMetadata.builder().build()).build(BlobStoreContext.class);
|
||||
context.getBlobStore().createContainerInLocation(null, "test1");
|
||||
return context.createBlobMap("test1");
|
||||
BlobStoreContext context =
|
||||
ContextBuilder.newBuilder(TransientApiMetadata.builder().build()).build(BlobStoreContext.class);
|
||||
String container = KeyToStoredTweetStatusTest.class.getName() + "#container";
|
||||
context.getBlobStore().createContainerInLocation(null, container);
|
||||
return context.createBlobMap(container);
|
||||
}
|
||||
|
||||
public void testStoreTweets() throws IOException, InterruptedException, ExecutionException {
|
||||
|
|
|
@ -60,7 +60,7 @@ public class ServiceToStoredTweetStatusesTest {
|
|||
}
|
||||
|
||||
public void testStoreTweets() throws IOException, InterruptedException, ExecutionException {
|
||||
String container = "container";
|
||||
String container = ServiceToStoredTweetStatusesTest.class.getName() + "#container";
|
||||
Map<String, BlobStoreContext> contexts = createServices(container);
|
||||
|
||||
ServiceToStoredTweetStatuses function = new ServiceToStoredTweetStatuses(contexts, container);
|
||||
|
|
Loading…
Reference in New Issue