Článok je venovaný použitiu vlastných fixtures do Fitnesse testovacieho frameworku. V tomto článku otestujeme verejne dostupné, takpovediac, edukačné webové služby.
V predchádzajúcom článku som sľúbil, že budeme pokračovať vytvorením fixtures na otestovanie skutočných webovýuch služieb. Konkrétne sa pokúsime otestovať webové služby:
Druhá časť článkov o Fitnesse testovacom frameworku bude obsahovať menej „omáčkových rečí“, ktoré sa dajú vždy nájsť na webe, zato sa však pokúsim pomocou uverejnených zdrojákov ilustrovať použitie tohto nástroja v praxi.
Začíname
- Download FitNesse build-u “fitnesse-standalone.jar”
- Spustiť ako administrátor: java -jar fitnesse-standalone.jar -p 2222
- Na vygenerovanie proxy java tried z WSDL súborov môžete použite váš obľúbený spôsob. Ja som použil Maven, takže mavenovský pom.xml bude obsahovať aj nasledujúce elementy.
Pom.xml
<build> <sourceDirectory>src</sourceDirectory> <testSourceDirectory>test</testSourceDirectory> <plugins> <plugin> <groupId>com.googlecode.maven-download-plugin</groupId> <artifactId>download-maven-plugin</artifactId> <version>1.2.0</version> <executions> <execution> <id>Download WeatherWS</id> <phase>generate-sources</phase> <goals> <goal>wget</goal> </goals> <configuration> <url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url> <outputDirectory>wsdl_new</outputDirectory> <outputFileName>WeatherWS.wsdl</outputFileName> </configuration> </execution> </executions> </plugin> . . . <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>2.3.11</version> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>2.3.11</version> </dependency> </dependencies> <executions> <execution> <id>generate-wws</id> <phase>generate-sources</phase> <configuration> <additionalJvmArgs>-Dfile.encoding=UTF8</additionalJvmArgs> <sourceRoot>src</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>wsdl_new/WeatherWS.wsdl</wsdl> <extraargs> <extraarg>-wsdlLocation</extraarg> <extraarg></extraarg> <extraarg>-client</extraarg> <extraarg>-p</extraarg> <extraarg>generated.WeatherWS</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions>
Štruktúra Fitnesse projektu
Do adresára wsdl_new pribudnú nami definované wsdl súbory atribútov pom.xml
… a vygenerované proxy triedy na volanie webových služieb do package „generated“
Facade services
Trieda WeatherService pošle webovému servisu požiadavku prostredníctvom ZIP kódu mesta v USA a vráti aktuálnu teplotu vo farenheitoch.
WeatherService.java
package com.rhe.fitnesse.wsFacade; import generated.WeatherWS.WeatherReturn; import generated.WeatherWS.WeatherSoap; import java.util.HashMap; import java.util.logging.Logger; public class WeatherService { public final static Logger LOGGER = Logger.getLogger(ConversionRateService.class.getName()); HashMap<String, Object> hm = new HashMap<String, Object>(); public HashMap<String, Object> doWeather(WeatherSoap wService, String zip) { long start = System.currentTimeMillis(); WeatherReturn ret = wService.getCityWeatherByZIP(zip); hm.put("city", ret.getCity()); hm.put("state", ret.getState()); hm.put("farenheit", ret.getTemperature()); System.out.println(zip+" City:"+ret.getCity()+" State:"+ret.getState()+" Temperature:"+ret.getTemperature()+" Farenheit"); long end = System.currentTimeMillis(); long dur = end-start; LOGGER.info("Operation took " + dur + " ms"); return hm; } }
Trieda ConversionRateService pošle webovému servisu požiadavku prostredníctvom meny „From“ a many „To“, pričom vráti aktuálny kurz oboch mien. Napríklad EUR->CZK.
ConversionRateService.java
package com.rhe.fitnesse.wsFacade; import generated.currencyConvertor.Currency; import generated.currencyConvertor.CurrencyConvertorSoap; import java.util.logging.Logger; public class ConversionRateService { public final static Logger LOGGER = Logger.getLogger(ConversionRateService.class.getName()); public double doCR(CurrencyConvertorSoap ccService, String from, String to) { long start = System.currentTimeMillis(); Currency cFrom = Currency.fromValue(from); Currency cTo = Currency.fromValue(to); double dd = ccService.conversionRate(cFrom, cTo); System.out.println("Conversion rate from "+from+" to "+to+" is "+dd); long end = System.currentTimeMillis(); long dur = end-start; LOGGER.info("Operation took " + dur + " ms"); return dd; } }
Fixtures
obsahujú všetky skripty použiteľné vo Fitness front end.
CheckCurrencyConvertor.java
package com.rhe.fitnesse.fixtures; import com.rhe.fitnesse.main.Main; import com.rhe.fitnesse.wsFacade.ConversionRateService; public class CheckCurrencyConvertor { double rate; public void getConversionrateFromTo(String from, String to) { ConversionRateService crs = new ConversionRateService(); rate = crs.doCR(Main.getCCService(), from, to); } public String rate() { Double dRet = new Double(rate); return dRet.toString(); } public boolean rateIsSameAs(double dVal) { if (rate == dVal) return true; else return false; } }
CheckWeather.java
package com.rhe.fitnesse.fixtures; import java.util.HashMap; import com.rhe.fitnesse.main.Main; import com.rhe.fitnesse.wsFacade.WeatherService; public class CheckWeather { HashMap<String, Object> hm = new HashMap<String, Object>(); double farenheit; String city; String state; public void getWeatherFromZip(String zip) { WeatherService wws = new WeatherService(); hm = wws.doWeather(Main.getWService(), zip); } public String farenheit() { return (String) hm.get("farenheit"); } public String city() { return (String) hm.get("city"); } public String state() { return (String) hm.get("state"); } }
Set-up
Trieda SetUp inicializuje webové služby a vracia ich SOAP handlery.
SetUp.java
package com.rhe.fitnesse.main; import generated.WeatherWS.Weather; import generated.WeatherWS.WeatherSoap; import generated.currencyConvertor.CurrencyConvertor; import generated.currencyConvertor.CurrencyConvertorSoap; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; public abstract class SetUp { public static final int TIMEOUT = 0; public static CurrencyConvertorSoap setupSoapCurrencyConvertor(String url, String nameSpace, String serviceName) { URL wsdlURL = null; try { wsdlURL = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } QName SERVICE_NAME = new QName(nameSpace, serviceName); CurrencyConvertor ss = new CurrencyConvertor(wsdlURL, SERVICE_NAME); CurrencyConvertorSoap port = ss.getCurrencyConvertorSoap(); return port; } public static WeatherSoap setupSoapWeather (String url, String nameSpace, String serviceName) { URL wsdlURL = null; try { wsdlURL = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } QName SERVICE_NAME = new QName(nameSpace, serviceName); Weather ss = new Weather(wsdlURL, SERVICE_NAME); WeatherSoap port = ss.getWeatherSoap(); return port; } }
Spustenie z prostredia Eclipse
Main.java
package com.rhe.fitnesse.main; import generated.WeatherWS.WeatherSoap; import generated.currencyConvertor.CurrencyConvertorSoap; import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; import com.rsd.fitnesse.fixtures.CheckCurrencyConvertor; import com.rsd.fitnesse.fixtures.CheckWeather; import com.rsd.fitnesse.repositoryFacade.Priklad_eu; import com.rsd.fitnesse.wsFacade.ConversionRateService; public class Main { private static CurrencyConvertorSoap ccService; private static WeatherSoap wService; static { LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.INFO); ConsoleHandler consoleHandler = new ConsoleHandler(); ConversionRateService.LOGGER.addHandler(consoleHandler); } public static void main(String[] args) { setWServices(); CheckWeather cw = new CheckWeather(); cw.getWeatherFromZip("85001"); System.out.println(cw.farenheit()); setCCServices(); CheckCurrencyConvertor cc = new CheckCurrencyConvertor(); cc.getConversionrateFromTo("EUR", "CZK"); System.out.println(cc.rate()); return; } public static void setWServices() { wService = SetUp.setupSoapWeather("http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl", "http://ws.cdyne.com/WeatherWS/", "Weather"); } public static void setCCServices() { ccService = SetUp.setupSoapCurrencyConvertor("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL", "http://www.webserviceX.NET/", "CurrencyConvertor"); } public static CurrencyConvertorSoap getCCService() { return ccService; } public static WeatherSoap getWService() { return wService; } }
Na konzole sa objaví:
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.BasicClientConnectionManager). log4j:WARN Please initialize the log4j system properly. Dec 1, 2014 11:25:57 AM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL INFO: Creating Service {http://ws.cdyne.com/WeatherWS/}Weather from WSDL: http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl 85001 City:Phoenix State:AZ Temperature:74 Farenheit 74 Dec 1, 2014 11:25:58 AM com.rsd.fitnesse.wsFacade.WeatherService doWeather INFO: Operation took 264 ms Dec 1, 2014 11:25:58 AM com.rsd.fitnesse.wsFacade.WeatherService doWeather INFO: Operation took 264 ms Dec 1, 2014 11:25:59 AM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL INFO: Creating Service {http://www.webserviceX.NET/}CurrencyConvertor from WSDL: http://www.webservicex.net/CurrencyConvertor.asmx?WSDL Conversion rate from EUR to CZK is 27.608 Dec 1, 2014 11:25:59 AM com.rsd.fitnesse.wsFacade.ConversionRateService doCR INFO: Operation took 406 ms Dec 1, 2014 11:25:59 AM com.rsd.fitnesse.wsFacade.ConversionRateService doCR INFO: Operation took 406 ms 27.608
Fitnesse front end
!define TEST_SYSTEM {slim} !path C:\Users\rhe\workspace\FitnesseFixturesDemo\jar\FitnesseFixturesDemo.jar !path C:\Users\rhe\workspace\FitnesseFixturesDemo\jar\FitnesseFixturesDemo_lib/* |import | |com.rsd.fitnesse.fixtures| !|setUpCurrencyConvertorService| |start? | |true | !3 Get CC rate - Check Currency Convertor |script |check currency convertor| |get Conversionrate from|EUR |to |CZK| |check not |rate |null | |show |rate | |ensure |rate is same as |27.6059| |reject |rate is same as |2 | !|setUpWeatherService| |start? | |true | !3 Get Wheather - temperature in NY |script |check weather | |getWeatherFromZip|10001 | |check not |farenheit|null| |show |city | |show |state | |show |farenheit | !3 Get Wheather - temperature in Phoenix |script |check weather | |getWeatherFromZip|85001 | |check not |farenheit|null| |show |city | |show |state | |show |farenheit |