|
| 1 | +/* |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 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 | + |
| 17 | +package com.example.guestbook; |
| 18 | + |
| 19 | +import static com.example.guestbook.GuestbookTestUtilities.cleanDatastore; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import com.google.appengine.api.datastore.DatastoreService; |
| 24 | +import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 25 | +import com.google.appengine.api.datastore.Entity; |
| 26 | +import com.google.appengine.api.datastore.KeyFactory; |
| 27 | +import com.google.appengine.api.datastore.PreparedQuery; |
| 28 | +import com.google.appengine.api.datastore.Query; |
| 29 | +import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; |
| 30 | +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; |
| 31 | + |
| 32 | +import com.googlecode.objectify.ObjectifyService; |
| 33 | +import com.googlecode.objectify.util.Closeable; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.junit.runners.JUnit4; |
| 39 | +import org.mockito.Mock; |
| 40 | +import org.mockito.MockitoAnnotations; |
| 41 | + |
| 42 | +import java.io.PrintWriter; |
| 43 | +import java.io.StringWriter; |
| 44 | + |
| 45 | +import javax.servlet.http.HttpServletRequest; |
| 46 | +import javax.servlet.http.HttpServletResponse; |
| 47 | + |
| 48 | +/** |
| 49 | + * Unit tests for {@link com.example.guestbook.SignGuestbookServlet}. |
| 50 | + */ |
| 51 | +@RunWith(JUnit4.class) |
| 52 | +public class SignGuestbookServletTest { |
| 53 | + private static final String FAKE_URL = "fakey.org/sign"; |
| 54 | + private static final String FAKE_NAME = "Fake"; |
| 55 | + |
| 56 | + private final LocalServiceTestHelper helper = |
| 57 | + new LocalServiceTestHelper( |
| 58 | + // Set no eventual consistency, that way queries return all results. |
| 59 | + // https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests |
| 60 | + new LocalDatastoreServiceTestConfig() |
| 61 | + .setDefaultHighRepJobPolicyUnappliedJobPercentage(0)); |
| 62 | + |
| 63 | + private final String testPhrase = "Noew is the time"; |
| 64 | + |
| 65 | + @Mock private HttpServletRequest mockRequest; |
| 66 | + |
| 67 | + @Mock |
| 68 | + private HttpServletResponse mockResponse; |
| 69 | + |
| 70 | + private StringWriter stringWriter; |
| 71 | + private SignGuestbookServlet servletUnderTest; |
| 72 | + private Closeable closeable; |
| 73 | + private DatastoreService ds; |
| 74 | + |
| 75 | + @Before |
| 76 | + public void setUp() throws Exception { |
| 77 | + |
| 78 | + MockitoAnnotations.initMocks(this); |
| 79 | + helper.setUp(); |
| 80 | + ds = DatastoreServiceFactory.getDatastoreService(); |
| 81 | + |
| 82 | + // Set up some fake HTTP requests |
| 83 | + when(mockRequest.getRequestURI()).thenReturn(FAKE_URL); |
| 84 | + when(mockRequest.getParameter("guestbookName")).thenReturn( "default" ); |
| 85 | + when(mockRequest.getParameter("content")).thenReturn( testPhrase ); |
| 86 | + |
| 87 | + stringWriter = new StringWriter(); |
| 88 | + when(mockResponse.getWriter()).thenReturn(new PrintWriter(stringWriter)); |
| 89 | + |
| 90 | + servletUnderTest = new SignGuestbookServlet(); |
| 91 | + |
| 92 | + ObjectifyService.register(Guestbook.class); |
| 93 | + ObjectifyService.register(Greeting.class); |
| 94 | + |
| 95 | + closeable = ObjectifyService.begin(); |
| 96 | + |
| 97 | + cleanDatastore(ds, "default"); |
| 98 | + } |
| 99 | + |
| 100 | + @After public void tearDown() { |
| 101 | + cleanDatastore(ds, "default"); |
| 102 | + helper.tearDown(); |
| 103 | + closeable.close(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void doPost_userNotLoggedIn() throws Exception { |
| 108 | + servletUnderTest.doPost(mockRequest, mockResponse); |
| 109 | + |
| 110 | + Query query = new Query("Greeting") |
| 111 | + .setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey()); |
| 112 | + PreparedQuery pq = ds.prepare(query); |
| 113 | + |
| 114 | + Entity greeting = pq.asSingleEntity(); // Should only be one at this point. |
| 115 | + assertEquals(greeting.getProperty("content"), testPhrase); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments