Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
dgageot committed Nov 29, 2014
1 parent 6f7cf35 commit 8f34118
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="container-fluid" ng-controller="IndexController as index">
<h3>Messages stored on Google Data Store:</h3>

<ul ng-cloak>
<ul id="messages" ng-cloak>
<li ng-repeat="message in index.messages track by $index">
{{message}}
</li>
Expand All @@ -19,7 +19,7 @@ <h3>Messages stored on Google Data Store:</h3>
<div class="form-group">
<input type="text" ng-model="message" class="form-control" id="message" placeholder="Enter a new message">
</div>
<button type="submit" class="btn btn-default" ng-click="index.create(message)">Add</button>
<button id="add" type="submit" class="btn btn-default" ng-click="index.create(message)">Add</button>
</form>
</div>

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/gageot/net/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2014 [email protected]
*
* 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 gageot.net;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;

import java.util.Date;
import java.util.List;

import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static com.google.appengine.api.datastore.KeyFactory.createKey;
import static com.google.appengine.api.datastore.Query.SortDirection.DESCENDING;
import static net.codestory.Fluent.of;

public class Messages {
public List<String> list() {
Query query = new Query("Message", createKey("Messages", "list")).addSort("date", DESCENDING);

List<Entity> messages = getDatastoreService().prepare(query).asList(withLimit(100));

return of(messages).map(message -> (String) message.getProperty("message")).toList();
}

public void create(String message) {
Entity entity = new Entity("Message", createKey("Messages", "list"));
entity.setProperty("message", message);
entity.setProperty("date", new Date());

getDatastoreService().put(entity);
}
}
27 changes: 8 additions & 19 deletions src/main/java/gageot/net/MessagesResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,25 @@
*/
package gageot.net;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import net.codestory.http.annotations.Get;
import net.codestory.http.annotations.Post;

import java.util.Date;
import java.util.List;

import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static com.google.appengine.api.datastore.KeyFactory.createKey;
import static com.google.appengine.api.datastore.Query.SortDirection.DESCENDING;
import static net.codestory.Fluent.of;

public class MessagesResource {
@Get("/message/list")
public List<String> list() {
Query query = new Query("Message", createKey("Messages", "list")).addSort("date", DESCENDING);
private final Messages messages;

List<Entity> messages = getDatastoreService().prepare(query).asList(withLimit(100));
public MessagesResource(Messages messages) {
this.messages = messages;
}

return of(messages).map(message -> (String) message.getProperty("message")).toList();
@Get("/message/list")
public List<String> list() {
return messages.list();
}

@Post("/message")
public void create(String message) {
Entity entity = new Entity("Message", createKey("Messages", "list"));
entity.setProperty("message", message);
entity.setProperty("date", new Date());

getDatastoreService().put(entity);
messages.create(message);
}
}
46 changes: 44 additions & 2 deletions src/test/java/gageot/net/MainWebTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,63 @@
package gageot.net;

import net.codestory.http.WebServer;
import net.codestory.http.injection.Singletons;
import net.codestory.http.routes.Routes;
import net.codestory.simplelenium.SeleniumTest;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MainWebTest extends SeleniumTest {
WebServer webServer = new WebServer().configure(MainWeb.WebConfiguration.class).startOnRandomPort();
WebServer webServer = new WebServer().configure(new MainWeb.WebConfiguration() {
@Override
public void configure(Routes routes) {
super.configure(routes);

routes.setIocAdapter(new Singletons().register(Messages.class, messages)); // Install dependency mock
}
}).startOnRandomPort();

@Override
protected String getDefaultBaseUrl() {
return "http://localhost:" + webServer.port();
}

@Test
public void index() {
public void show_messages() {
messages.create("Message01");
messages.create("Message02");

goTo("/");

find("h2").should().contain("Managed VMs Sample Project");
find("#messages").should().contain("Message01", "Message02");
}

@Test
public void add_message() {
messages.create("FirstMessage");

goTo("/");

find("#message").fill("SecondMessage");
find("#add").click();

find("#messages").should().contain("FirstMessage", "SecondMessage");
}

Messages messages = new Messages() {
private final List<String> messages = new ArrayList<>();

@Override
public List<String> list() {
return messages;
}

@Override
public void create(String message) {
messages.add(message);
}
};
}

0 comments on commit 8f34118

Please sign in to comment.