|
| 1 | +/* |
| 2 | + * Copyright 2016 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.appengine; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +import com.google.appengine.api.datastore.DatastoreService; |
| 23 | +import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 24 | +import com.google.appengine.api.datastore.Entity; |
| 25 | +import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; |
| 26 | +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.junit.runners.JUnit4; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.MockitoAnnotations; |
| 34 | + |
| 35 | +import java.io.PrintWriter; |
| 36 | +import java.io.StringWriter; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Date; |
| 39 | + |
| 40 | +import javax.servlet.http.HttpServletRequest; |
| 41 | +import javax.servlet.http.HttpServletResponse; |
| 42 | + |
| 43 | +/** |
| 44 | + * Unit tests for {@link IndexesServlet}. |
| 45 | + */ |
| 46 | +@RunWith(JUnit4.class) |
| 47 | +public class IndexesServletTest { |
| 48 | + |
| 49 | + private final LocalServiceTestHelper helper = |
| 50 | + new LocalServiceTestHelper( |
| 51 | + // Set no eventual consistency, that way queries return all results. |
| 52 | + // https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests |
| 53 | + new LocalDatastoreServiceTestConfig() |
| 54 | + .setDefaultHighRepJobPolicyUnappliedJobPercentage(0)); |
| 55 | + |
| 56 | + @Mock private HttpServletRequest mockRequest; |
| 57 | + @Mock private HttpServletResponse mockResponse; |
| 58 | + private StringWriter responseWriter; |
| 59 | + private IndexesServlet servletUnderTest; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() throws Exception { |
| 63 | + MockitoAnnotations.initMocks(this); |
| 64 | + helper.setUp(); |
| 65 | + |
| 66 | + // Set up a fake HTTP response. |
| 67 | + responseWriter = new StringWriter(); |
| 68 | + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); |
| 69 | + |
| 70 | + servletUnderTest = new IndexesServlet(); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void tearDown() { |
| 75 | + helper.tearDown(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void doGet_emptyDatastore_writesNoWidgets() throws Exception { |
| 80 | + servletUnderTest.doGet(mockRequest, mockResponse); |
| 81 | + |
| 82 | + assertThat(responseWriter.toString()) |
| 83 | + .named("IndexesServlet response") |
| 84 | + .isEqualTo("Got 0 widgets.\n"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void doGet_repeatedPropertyEntities_writesWidgets() throws Exception { |
| 89 | + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); |
| 90 | + // [START exploding_index_example_3] |
| 91 | + Entity widget = new Entity("Widget"); |
| 92 | + widget.setProperty("x", Arrays.asList(1, 2, 3, 4)); |
| 93 | + widget.setProperty("y", Arrays.asList("red", "green", "blue")); |
| 94 | + widget.setProperty("date", new Date()); |
| 95 | + datastore.put(widget); |
| 96 | + // [END exploding_index_example_3] |
| 97 | + |
| 98 | + servletUnderTest.doGet(mockRequest, mockResponse); |
| 99 | + |
| 100 | + assertThat(responseWriter.toString()) |
| 101 | + .named("IndexesServlet response") |
| 102 | + .isEqualTo("Got 1 widgets.\n"); |
| 103 | + } |
| 104 | +} |
0 commit comments