Java tutorial
/* * This file is a component of thundr, a software library from 3wks. * Read more: http://www.3wks.com.au/thundr * Copyright (C) 2014 3wks, <thundr@3wks.com.au> * * 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 com.threewks.thundr.bigmetrics.controller; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.text.WordUtils; import org.junit.Before; import org.junit.Test; import com.threewks.thundr.bigmetrics.BigMetricsService; import com.threewks.thundr.bigmetrics.bigquery.BigQueryService; import com.threewks.thundr.view.string.StringView; import jodd.util.StringPool; public class EventQueueControllerTest { private EventQueueController controller; private BigMetricsService bigMetricsService; private BigQueryService bigQueryService; private String xAppEngineTaskName; private String tableId; private Map<String, Object> data; @Before public void before() { xAppEngineTaskName = "Test Header"; tableId = "testTable"; data = new LinkedHashMap<>(); data.put("id", 1); data.put("firstName", "Homer"); data.put("lastName", "Simpson"); // mocks bigMetricsService = mock(BigMetricsService.class); bigQueryService = mock(BigQueryService.class); controller = new EventQueueController(bigMetricsService, bigQueryService); } @Test public void shouldAddEventAndReturn200StatusCode() { StringView view = controller.addEvent(xAppEngineTaskName, tableId, data); assertThat(view.content(), is((CharSequence) "OK")); assertThat(view.getStatusCode(), is(200)); verify(bigQueryService).insert(xAppEngineTaskName, tableId, data); } @Test public void shouldReturnErrorMessageOnFailureWith500StatusCode() { doThrow(new RuntimeException("Test Error")).when(bigQueryService).insert(anyString(), anyString(), anyMapOf(String.class, Object.class)); StringView view = controller.addEvent(xAppEngineTaskName, tableId, data); assertThat(view.content(), is((CharSequence) "Failed to process event for tableId: testTable - Error Message: Test Error")); assertThat(view.getStatusCode(), is(500)); verify(bigQueryService).insert(xAppEngineTaskName, tableId, data); } @Test public void shouldCorrectlyFormatHeader() { // Check logic, by using Thundr code String originalHeader = "X-AppEngine-TaskName"; String parameterHeader = "xAppengineTaskname"; String capitalised = WordUtils.capitalizeFully(originalHeader, '-'); String withoutDashes = capitalised.replaceAll(StringPool.DASH, StringPool.EMPTY); String formattedHeader = StringUtils.uncapitalize(withoutDashes); assertThat(formattedHeader, is(parameterHeader)); } @Test public void shouldEnsureViewsExists() { StringView view = controller.ensureViews(); assertThat(view.content(), is((CharSequence) "OK")); verify(bigMetricsService).ensureViewsExist(); } @Test public void shouldReturnInternalServerErrorWhenFailToEnsureViewsExist() { doThrow(new RuntimeException("intentional")).when(bigMetricsService).ensureViewsExist(); StringView view = controller.ensureViews(); assertThat(view.getStatusCode(), is(500)); assertThat(view.content(), is("Failed to ensure big metrics views are up to date: intentional")); } @Test public void shouldEnsureTablesExists() { StringView view = controller.ensureTables(); assertThat(view.content(), is((CharSequence) "OK")); verify(bigMetricsService).ensureTablesExist(); } @Test public void shouldReturnInternalServerErrorWhenFailToEnsureTablesExist() { doThrow(new RuntimeException("intentional")).when(bigMetricsService).ensureTablesExist(); StringView view = controller.ensureTables(); assertThat(view.getStatusCode(), is(500)); assertThat(view.content(), is("Failed to ensure big metrics tables are up to date: intentional")); } }