001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.testing; 019 020import io.stallion.boot.AppContextLoader; 021 022import io.stallion.services.Log; 023import io.stallion.settings.Settings; 024 025import org.junit.AfterClass; 026 027import java.io.File; 028import java.net.URL; 029import java.nio.file.Path; 030import java.nio.file.Paths; 031import java.util.logging.Level; 032 033import static io.stallion.utils.Literals.empty; 034 035 036public abstract class AppIntegrationCaseBase { 037 038 public static TestClient client; 039 040 public static void startApp(String folderName) throws Exception { 041 startApp(folderName, false); 042 } 043 044 public static void startApp(String folderName, Boolean watchFolders) throws Exception { 045 Log.info("setUpClass client and app"); 046 Settings.shutdown(); 047 String path; 048 if (new File(folderName).exists()) { 049 path = folderName; 050 } else { 051 URL resourceUrl = AppIntegrationCaseBase.class. 052 getResource(folderName); 053 Path resourcePath = Paths.get(resourceUrl.toURI()); 054 path = resourcePath.toString(); 055 } 056 Log.fine("--------------------------------------------------------------------------------------------------"); 057 Log.info("Booting app from folder: {0} ", path); 058 Log.fine("--------------------------------------------------------------------------------------------------"); 059 AppContextLoader.loadAndStartForTests(path); 060 String level = System.getenv("stallionLogLevel"); 061 if (!empty(level)) { 062 Log.setLogLevel(Level.parse(level.toUpperCase())); 063 } 064 client = new TestClient(AppContextLoader.instance()); 065 Log.fine("--------------------------------------------------------------------------------------------------"); 066 Log.info("App booted for folder: {0} ", path); 067 Log.fine("--------------------------------------------------------------------------------------------------"); 068 } 069 070 @AfterClass 071 public static void tearDownClass() throws Exception { 072 cleanUpClass(); 073 } 074 075 076 public static void cleanUpClass() { 077 AppContextLoader.shutdown(); 078 Settings.shutdown(); 079 client = null; 080 Stubbing.reset(); 081 } 082 083 public void assertContains(String content, String expected) { 084 if (!content.contains(expected)) { 085 Log.warn("Content ''{0}'' does not contain string ''{1}''!!", content, expected); 086 } 087 assert content.contains(expected); 088 } 089 090 public void assertNotContains(String content, String unexpected) { 091 if (content.contains(unexpected)) { 092 Log.warn("Content ''{0}'' erroneously contains string ''{1}''!!", content, unexpected); 093 } 094 assert !content.contains(unexpected); 095 } 096 097 public void assertResponseDoesNotContain(MockResponse response, String content) { 098 assertResponseDoesNotContain(response, content, 200); 099 } 100 101 public void assertResponseDoesNotContain(MockResponse response, String content, int status) { 102 if (response.getContent().contains(content)) { 103 Log.warn("Unexpected string {0} found in response content!!\n{1}\n\n", content, response.getContent()); 104 } 105 assert !response.getContent().contains(content); 106 } 107 108 public void assertResponseContains(MockResponse response, String content) { 109 assertResponseContains(response, content, 200); 110 } 111 112 public void assertResponseSucceeded(MockResponse response) { 113 if (response.getStatus() != 200) { 114 throw new AssertionError("Response status was: " + response.getStatus() + " Content: " + response.getContent()); 115 } 116 } 117 118 public void assertResponseContains(MockResponse response, String content, int status) { 119 if (!response.getContent().contains(content)) { 120 Log.warn("String {0} not found in response content!!\n{1}\n\n", content, response.getContent()); 121 } 122 if (response.getStatus() != status) { 123 Log.warn("Bad response status! expected={0} actual={1}", status, response.getStatus()); 124 assert response.getStatus() == status; 125 } 126 assert response.getContent().contains(content); 127 } 128 129}