Web Logging code included in exisitng module namely "spring-rest"

This commit is contained in:
mokhan 2017-07-24 23:03:24 +05:30
parent 6c105969f2
commit 29b0da9960
12 changed files with 583 additions and 275 deletions

View File

@ -1,321 +1,329 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>spring-rest</artifactId> <artifactId>spring-rest</artifactId>
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<name>spring-rest</name> <name>spring-rest</name>
<packaging>war</packaging> <packaging>war</packaging>
<parent> <parent>
<artifactId>parent-boot-4</artifactId> <artifactId>parent-boot-4</artifactId>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-4</relativePath> <relativePath>../parent-boot-4</relativePath>
</parent> </parent>
<dependencies> <dependencies>
<!-- Spring Boot Dependencies --> <!-- Spring Boot Dependencies -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId> <artifactId>spring-boot-test</artifactId>
</dependency> </dependency>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId> <groupId>commons-logging</groupId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId> <artifactId>spring-oxm</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-fileupload</groupId> <groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId> <artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version> <version>${commons-fileupload.version}</version>
</dependency> </dependency>
<!-- web --> <!-- web -->
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId> <artifactId>jstl</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<!-- marshalling --> <!-- marshalling -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId> <groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId> <artifactId>jackson-dataformat-xml</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.thoughtworks.xstream</groupId> <groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId> <artifactId>xstream</artifactId>
<version>${xstream.version}</version> <version>${xstream.version}</version>
</dependency> </dependency>
<!-- util --> <!-- util -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency> </dependency>
<!-- okhttp --> <!-- okhttp -->
<dependency> <dependency>
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>${com.squareup.okhttp3.version}</version> <version>${com.squareup.okhttp3.version}</version>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId> <artifactId>hamcrest-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
</dependency> </dependency>
<!-- --> <!-- -->
<dependency> <dependency>
<groupId>com.google.protobuf</groupId> <groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId> <artifactId>protobuf-java</artifactId>
<version>${protobuf-java.version}</version> <version>${protobuf-java.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.googlecode.protobuf-java-format</groupId> <groupId>com.googlecode.protobuf-java-format</groupId>
<artifactId>protobuf-java-format</artifactId> <artifactId>protobuf-java-format</artifactId>
<version>${protobuf-java-format.version}</version> <version>${protobuf-java-format.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.esotericsoftware</groupId> <groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId> <artifactId>kryo</artifactId>
<version>${kryo.version}</version> <version>${kryo.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId> <artifactId>json-path</artifactId>
</dependency> </dependency>
</dependencies>
<build> <!-- Apache common IO for utility -->
<finalName>spring-rest</finalName> <dependency>
<resources> <groupId>commons-io</groupId>
<resource> <artifactId>commons-io</artifactId>
<directory>src/main/resources</directory> <version>2.4</version>
<filtering>true</filtering> </dependency>
</resource>
</resources>
<plugins> </dependencies>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin> <build>
<groupId>org.codehaus.cargo</groupId> <finalName>spring-rest</finalName>
<artifactId>cargo-maven2-plugin</artifactId> <resources>
<version>${cargo-maven2-plugin.version}</version> <resource>
<configuration> <directory>src/main/resources</directory>
<wait>true</wait> <filtering>true</filtering>
<container> </resource>
<containerId>tomcat8x</containerId> </resources>
<type>embedded</type>
<systemProperties>
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
</systemProperties>
</container>
<configuration>
<properties>
<cargo.servlet.port>8082</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
</plugins> <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</build> <plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version>
<configuration>
<wait>true</wait>
<container>
<containerId>tomcat8x</containerId>
<type>embedded</type>
<systemProperties>
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
</systemProperties>
</container>
<configuration>
<properties>
<cargo.servlet.port>8082</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
<profiles> </plugins>
<profile> </build>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins> <profiles>
</build>
</profile>
<profile> <profile>
<id>live</id> <id>integration</id>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.codehaus.cargo</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>cargo-maven2-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<executions> <executions>
<execution> <execution>
<id>start-server</id> <phase>integration-test</phase>
<phase>pre-integration-test</phase> <goals>
<goals> <goal>test</goal>
<goal>start</goal> </goals>
</goals> <configuration>
</execution> <excludes>
<execution> <exclude>none</exclude>
<id>stop-server</id> </excludes>
<phase>post-integration-test</phase> <includes>
<goals> <include>**/*IntegrationTest.java</include>
<goal>stop</goal> </includes>
</goals> </configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin> </plugins>
<groupId>org.apache.maven.plugins</groupId> </build>
<artifactId>maven-surefire-plugin</artifactId> </profile>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>
</includes>
<systemPropertyVariables>
<webTarget>cargo</webTarget>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins> <profile>
</build> <id>live</id>
</profile> <build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</profiles> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>
</includes>
<systemPropertyVariables>
<webTarget>cargo</webTarget>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
<properties> </plugins>
<commons-fileupload.version>1.3.2</commons-fileupload.version> </build>
<kryo.version>4.0.0</kryo.version> </profile>
<protobuf-java-format.version>1.4</protobuf-java-format.version>
<protobuf-java.version>3.1.0</protobuf-java.version>
<commons-lang3.version>3.5</commons-lang3.version>
<xstream.version>1.4.9</xstream.version>
<!-- util --> </profiles>
<guava.version>20.0</guava.version>
<!-- Maven plugins --> <properties>
<cargo-maven2-plugin.version>1.6.0</cargo-maven2-plugin.version> <commons-fileupload.version>1.3.2</commons-fileupload.version>
<findbugs-maven-plugin.version>3.0.4</findbugs-maven-plugin.version> <kryo.version>4.0.0</kryo.version>
<protobuf-java-format.version>1.4</protobuf-java-format.version>
<protobuf-java.version>3.1.0</protobuf-java.version>
<commons-lang3.version>3.5</commons-lang3.version>
<xstream.version>1.4.9</xstream.version>
<!-- okhttp --> <!-- util -->
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version> <guava.version>20.0</guava.version>
<json.path.version>2.2.0</json.path.version> <!-- Maven plugins -->
</properties> <cargo-maven2-plugin.version>1.6.0</cargo-maven2-plugin.version>
<findbugs-maven-plugin.version>3.0.4</findbugs-maven-plugin.version>
<!-- okhttp -->
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
<json.path.version>2.2.0</json.path.version>
</properties>
</project> </project>

View File

@ -0,0 +1,18 @@
package com.baeldung.web.log.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan("com.baeldung.web.log")
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.web.log.app;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import com.baeldung.web.log.util.RequestLoggingUtil;
@Component
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String postData = null;
HttpServletRequest requestCacheWrapperObject = null;
try {
// Uncomment to produce the stream closed issue
// postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream());
// To overcome request stream closed issue
requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
requestCacheWrapperObject.getParameterMap();
} catch (Exception exception) {
exception.printStackTrace();
} finally {
postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject);
LOGGER.info("REQUEST DATA: " + postData);
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
LOGGER.info("RESPONSE: " + response.getStatus());
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.web.log.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA : ");
return filter;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.web.log.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
@Configuration
public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter {
@Autowired
private TaxiFareRequestInterceptor taxiFareRequestInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/");
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.web.log.controller;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baeldung.web.log.data.RateCard;
import com.baeldung.web.log.data.TaxiRide;
import com.baeldung.web.log.service.TaxiFareCalculatorService;
@Controller
public class TaxiFareController {
@Autowired
private TaxiFareCalculatorService taxiFareCalculatorService;
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class);
@RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/")
@ResponseBody
public RateCard getTaxiFare() {
LOGGER.debug("getTaxiFare() - START");
return new RateCard();
}
@RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/")
@ResponseBody
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
LOGGER.debug("calculateTaxiFare() - START");
String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare);
LOGGER.debug("calculateTaxiFare() - END");
return totalFare;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.web.log.data;
public class RateCard {
private String nightSurcharge;
private String ratePerMile;
public RateCard(){
nightSurcharge="Extra $ 100";
ratePerMile="$ 10 Per Mile";
}
public String getNightSurcharge() {
return nightSurcharge;
}
public void setNightSurcharge(String nightSurcharge) {
this.nightSurcharge = nightSurcharge;
}
public String getRatePerMile() {
return ratePerMile;
}
public void setRatePerMile(String ratePerMile) {
this.ratePerMile = ratePerMile;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.web.log.data;
public class TaxiRide {
private Boolean isNightSurcharge;
private Long distanceInMile;
public TaxiRide(){}
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){
this.isNightSurcharge = isNightSurcharge;
this.distanceInMile = distanceInMile;
}
public Boolean getIsNightSurcharge() {
return isNightSurcharge;
}
public void setIsNightSurcharge(Boolean isNightSurcharge) {
this.isNightSurcharge = isNightSurcharge;
}
public Long getDistanceInMile() {
return distanceInMile;
}
public void setDistanceInMile(Long distanceInMile) {
this.distanceInMile = distanceInMile;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.web.log.service;
import org.springframework.stereotype.Service;
import com.baeldung.web.log.data.TaxiRide;
@Service
public class TaxiFareCalculatorService {
public String calculateFare(TaxiRide taxiRide) {
Long fare = 0l;
if (taxiRide.getIsNightSurcharge()) {
fare = taxiRide.getDistanceInMile() * 10 + 100;
} else {
fare = taxiRide.getDistanceInMile() * 10;
}
return String.valueOf(fare);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.web.log.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;
public class RequestLoggingUtil {
public static String getStringFromInputStream(InputStream is) {
StringWriter writer = new StringWriter();
String encoding = "UTF-8";
try {
IOUtils.copy(is, writer, encoding);
} catch (IOException e) {
e.printStackTrace();
}
return writer.toString();
}
public static String readPayload(final HttpServletRequest request) throws IOException {
String payloadData = null;
ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (null != contentCachingRequestWrapper) {
byte[] buf = contentCachingRequestWrapper.getContentAsByteArray();
if (buf.length > 0) {
payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding());
}
}
return payloadData;
}
}

View File

@ -7,6 +7,10 @@
</encoder> </encoder>
</appender> </appender>
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
<level value="DEBUG" />
</logger>
<logger name="org.springframework" level="WARN" /> <logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" /> <logger name="org.springframework.transaction" level="WARN" />

View File

@ -0,0 +1,33 @@
package com.baeldung.web.log.test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.baeldung.web.log.data.TaxiRide;
public class TestTaxiFareController {
private static final String URL = "http://localhost:" + 8082 + "/spring-rest/taxifare/";
@Test
public void givenRequest_thenfetchTaxiFareRateCard() {
TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> response = testRestTemplate.getForEntity(URL + "get/", String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
@Test
public void givenTaxiRide_thenGetCalculatedFare() {
TestRestTemplate testRestTemplate = new TestRestTemplate();
TaxiRide taxiRide = new TaxiRide(true,10l);
String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class);
assertThat(fare, equalTo("200"));
}
}