├── Dockerfile
├── Jenkinsfile
├── pom.xml
└── src
├── .DS_Store
├── main
├── .DS_Store
├── java
│ └── com
│ │ └── in28minutes
│ │ └── microservices
│ │ └── currencyexchangeservice
│ │ ├── CurrencyExchangeServiceApplicationH2.java
│ │ ├── HelloWorld.java
│ │ ├── resource
│ │ ├── CurrencyExchangeController.java
│ │ ├── ExchangeValue.java
│ │ └── ExchangeValueRepository.java
│ │ └── util
│ │ └── environment
│ │ ├── EnvironmentConfigurationLogger.java
│ │ └── InstanceInformationService.java
└── resources
│ ├── application.properties
│ └── data.sql
└── test
├── .DS_Store
├── java
├── .DS_Store
└── com
│ ├── .DS_Store
│ └── in28minutes
│ ├── .DS_Store
│ └── microservices
│ ├── .DS_Store
│ └── currencyexchangeservice
│ ├── .DS_Store
│ ├── CurrencyExchangeServiceApplicationTests.java
│ ├── cucumber
│ ├── CucumberSpringContextConfiguration.java
│ ├── CurrencyExchangeSteps.java
│ ├── HelloWorldSteps.java
│ └── RunCucumberIntegrationTestCase.java
│ └── resource
│ └── CurrencyExchangeControllerTest.java
└── resources
├── cucum.feature
└── currencyexchange.feature
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM openjdk:8-jdk-alpine
2 | VOLUME /tmp
3 | EXPOSE 8000
4 | ADD target/*.jar app.jar
5 | ENV JAVA_OPTS=""
6 | ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
--------------------------------------------------------------------------------
/Jenkinsfile:
--------------------------------------------------------------------------------
1 | //SCRIPTED
2 |
3 | //DECLARATIVE
4 | pipeline {
5 | agent any
6 | // agent { docker { image 'maven:3.6.3'} }
7 | // agent { docker { image 'node:13.8'} }
8 | environment {
9 | dockerHome = tool 'myDocker'
10 | mavenHome = tool 'myMaven'
11 | PATH = "$dockerHome/bin:$mavenHome/bin:$PATH"
12 | }
13 |
14 | stages {
15 | stage('Checkout') {
16 | steps {
17 | sh 'mvn --version'
18 | sh 'docker version'
19 | echo "Build"
20 | echo "PATH - $PATH"
21 | echo "BUILD_NUMBER - $env.BUILD_NUMBER"
22 | echo "BUILD_ID - $env.BUILD_ID"
23 | echo "JOB_NAME - $env.JOB_NAME"
24 | echo "BUILD_TAG - $env.BUILD_TAG"
25 | echo "BUILD_URL - $env.BUILD_URL"
26 | }
27 | }
28 | stage('Compile') {
29 | steps {
30 | sh "mvn clean compile"
31 | }
32 | }
33 |
34 | stage('Test') {
35 | steps {
36 | sh "mvn test"
37 | }
38 | }
39 |
40 | stage('Integration Test') {
41 | steps {
42 | sh "mvn failsafe:integration-test failsafe:verify"
43 | }
44 | }
45 |
46 | stage('Package') {
47 | steps {
48 | sh "mvn package -DskipTests"
49 | }
50 | }
51 |
52 | stage('Build Docker Image') {
53 | steps {
54 | //"docker build -t in28min/currency-exchange-devops:$env.BUILD_TAG"
55 | script {
56 | dockerImage = docker.build("in28min/currency-exchange-devops:${env.BUILD_TAG}")
57 | }
58 |
59 | }
60 | }
61 |
62 | stage('Push Docker Image') {
63 | steps {
64 | script {
65 | docker.withRegistry('', 'dockerhub') {
66 | dockerImage.push();
67 | dockerImage.push('latest');
68 | }
69 | }
70 | }
71 | }
72 | }
73 |
74 | post {
75 | always {
76 | echo 'Im awesome. I run always'
77 | }
78 | success {
79 | echo 'I run when you are successful'
80 | }
81 | failure {
82 | echo 'I run when you fail'
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.in28minutes.microservices
8 | currency-exchange-basic
9 | 0.0.1-RELEASE
10 | jar
11 | currency-exchange
12 |
13 | Demo project for Spring Boot
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-parent
18 | 2.1.1.RELEASE
19 |
20 |
21 |
22 |
23 | UTF-8
24 | UTF-8
25 | 1.8
26 | 3.1.1
27 | Greenwich.SR3
28 |
29 | 2.22.1
30 | 2.22.1
31 |
32 | 0.8.3
33 | ${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar
34 | ${project.build.directory}/jacoco.exec
35 | ${project.build.directory}/jacoco-it.exec
36 | -javaagent:${jacoco.path}=destfile=${jacoco.utReport}
37 | -javaagent:${jacoco.path}=destfile=${jacoco.itReport}
38 |
39 | 5.10.2.17019
40 |
41 |
42 |
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-web
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-starter-data-jpa
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-devtools
57 | runtime
58 |
59 |
60 |
61 | com.h2database
62 | h2
63 | runtime
64 |
65 |
66 |
67 | javax.xml.bind
68 | jaxb-api
69 |
70 |
71 | com.sun.xml.bind
72 | jaxb-impl
73 | 2.3.1
74 |
75 |
76 | org.glassfish.jaxb
77 | jaxb-runtime
78 |
79 |
80 | javax.activation
81 | activation
82 | 1.1.1
83 |
84 |
85 |
86 | org.springframework.boot
87 | spring-boot-starter-test
88 | test
89 |
90 |
91 |
92 | io.rest-assured
93 | rest-assured-all
94 | 4.0.0
95 | test
96 |
97 |
98 | io.cucumber
99 | cucumber-spring
100 | 4.2.0
101 | test
102 |
103 |
104 | io.cucumber
105 | cucumber-java
106 | 2.3.1
107 | test
108 |
109 |
110 | io.cucumber
111 | cucumber-junit
112 | 2.3.1
113 | test
114 |
115 |
116 | org.jacoco
117 | org.jacoco.agent
118 | ${jacoco.version}
119 | runtime
120 | test
121 |
122 |
123 | org.sonarsource.java
124 | sonar-jacoco-listeners
125 | ${jacoco-listeners.version}
126 | test
127 |
128 |
129 | org.springframework.security
130 | spring-security-test
131 | test
132 |
133 |
134 | io.rest-assured
135 | rest-assured
136 | 4.1.2
137 | test
138 |
139 |
140 |
141 |
142 |
143 |
144 | org.springframework.cloud
145 | spring-cloud-dependencies
146 | ${spring-cloud.version}
147 | pom
148 | import
149 |
150 |
151 |
152 |
153 |
154 | currency-exchange
155 |
156 |
157 | org.springframework.boot
158 | spring-boot-maven-plugin
159 |
160 |
161 | org.apache.maven.plugins
162 | maven-surefire-plugin
163 | ${surefire.version}
164 |
165 |
166 |
167 |
168 |
169 |
170 | ${jacoco.utAgentConfig}
171 |
172 |
173 | **/RunCucumberIntegrationTestCase.java
174 |
175 |
176 |
177 |
178 |
179 | listener
180 | org.sonar.java.jacoco.JUnitListener
181 |
182 |
183 |
184 |
185 |
186 | org.apache.maven.plugins
187 | maven-failsafe-plugin
188 | ${failsafe.version}
189 |
190 |
191 |
192 |
193 |
194 | **/*CucumberIntegrationTestCase.java
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 | spring-snapshots
224 | Spring Snapshots
225 | https://repo.spring.io/snapshot
226 |
227 | true
228 |
229 |
230 |
231 | spring-milestones
232 | Spring Milestones
233 | https://repo.spring.io/milestone
234 |
235 | false
236 |
237 |
238 |
239 |
240 |
241 |
242 | spring-snapshots
243 | Spring Snapshots
244 | https://repo.spring.io/snapshot
245 |
246 | true
247 |
248 |
249 |
250 | spring-milestones
251 | Spring Milestones
252 | https://repo.spring.io/milestone
253 |
254 | false
255 |
256 |
257 |
258 |
259 |
--------------------------------------------------------------------------------
/src/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/.DS_Store
--------------------------------------------------------------------------------
/src/main/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/main/.DS_Store
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/CurrencyExchangeServiceApplicationH2.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class CurrencyExchangeServiceApplicationH2 {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(CurrencyExchangeServiceApplicationH2.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/HelloWorld.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice;
2 |
3 | public class HelloWorld {
4 |
5 | public String sayHello(String name) {
6 |
7 | System.out.println("sayHello called with name : " + name);
8 | return "Hello " + name;
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/resource/CurrencyExchangeController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.resource;
2 |
3 | import java.util.Map;
4 |
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.web.bind.annotation.GetMapping;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestHeader;
11 | import org.springframework.web.bind.annotation.RestController;
12 |
13 | import com.in28minutes.microservices.currencyexchangeservice.util.environment.InstanceInformationService;
14 |
15 | @RestController
16 | public class CurrencyExchangeController {
17 |
18 | private static final Logger LOGGER = LoggerFactory.getLogger(CurrencyExchangeController.class);
19 |
20 | @Autowired
21 | private ExchangeValueRepository repository;
22 |
23 | @Autowired
24 | private InstanceInformationService instanceInformationService;
25 |
26 | @GetMapping("/")
27 | public String imHealthy() {
28 | return "{healthy:true}";
29 | }
30 |
31 | //http://localhost:8000/currency-exchange/from/USD/to/INR
32 | @GetMapping("/currency-exchange/from/{from}/to/{to}")
33 | public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to,
34 | @RequestHeader Map headers) {
35 |
36 | printAllHeaders(headers);
37 |
38 | ExchangeValue exchangeValue = repository.findByFromAndTo(from, to);
39 |
40 | LOGGER.info("{} {} {}", from, to, exchangeValue);
41 |
42 | if (exchangeValue == null) {
43 | throw new RuntimeException("Unable to find data to convert " + from + " to " + to);
44 | }
45 |
46 | exchangeValue.setExchangeEnvironmentInfo(instanceInformationService.retrieveInstanceInfo());
47 |
48 | return exchangeValue;
49 | }
50 |
51 | private void printAllHeaders(Map headers) {
52 | headers.forEach((key, value) -> {
53 | LOGGER.info(String.format("Header '%s' = %s", key, value));
54 | });
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/resource/ExchangeValue.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.resource;
2 |
3 | import java.math.BigDecimal;
4 |
5 | import javax.persistence.Column;
6 | import javax.persistence.Entity;
7 | import javax.persistence.Id;
8 |
9 | @Entity
10 | public class ExchangeValue {
11 |
12 | @Id
13 | private Long id;
14 |
15 | @Column(name = "currency_from")
16 | private String from;
17 |
18 | @Column(name = "currency_to")
19 | private String to;
20 |
21 | private BigDecimal conversionMultiple;
22 |
23 | private String exchangeEnvironmentInfo;
24 |
25 | public ExchangeValue() {
26 |
27 | }
28 |
29 | public ExchangeValue(Long id, String from, String to, BigDecimal conversionMultiple) {
30 | super();
31 | this.id = id;
32 | this.from = from;
33 | this.to = to;
34 | this.conversionMultiple = conversionMultiple;
35 | }
36 |
37 | public Long getId() {
38 | return id;
39 | }
40 |
41 | public String getFrom() {
42 | return from;
43 | }
44 |
45 | public String getTo() {
46 | return to;
47 | }
48 |
49 | public BigDecimal getConversionMultiple() {
50 | return conversionMultiple;
51 | }
52 |
53 | public String getExchangeEnvironmentInfo() {
54 | return exchangeEnvironmentInfo;
55 | }
56 |
57 | public void setExchangeEnvironmentInfo(String exchangeEnvironmentInfo) {
58 | this.exchangeEnvironmentInfo = exchangeEnvironmentInfo+"updated";
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "ExchangeValue [id=" + id + ", from=" + from + ", to=" + to + ", conversionMultiple="
64 | + conversionMultiple + ", exchangeEnvironmentInfo=" + exchangeEnvironmentInfo + "]";
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/resource/ExchangeValueRepository.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.resource;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 |
5 | public interface ExchangeValueRepository extends JpaRepository {
6 | ExchangeValue findByFromAndTo(String from, String to);
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/util/environment/EnvironmentConfigurationLogger.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.util.environment;
2 |
3 | import java.util.Arrays;
4 | import java.util.stream.StreamSupport;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.context.event.ContextRefreshedEvent;
9 | import org.springframework.context.event.EventListener;
10 | import org.springframework.core.env.AbstractEnvironment;
11 | import org.springframework.core.env.EnumerablePropertySource;
12 | import org.springframework.core.env.Environment;
13 | import org.springframework.core.env.MutablePropertySources;
14 | import org.springframework.stereotype.Component;
15 |
16 | @Component
17 | public class EnvironmentConfigurationLogger {
18 |
19 | private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentConfigurationLogger.class);
20 |
21 | @SuppressWarnings("rawtypes")
22 | @EventListener
23 | public void handleContextRefresh(ContextRefreshedEvent event) {
24 | final Environment environment = event.getApplicationContext().getEnvironment();
25 | LOGGER.info("====== Environment and configuration ======");
26 | LOGGER.info("Active profiles: {}", Arrays.toString(environment.getActiveProfiles()));
27 | final MutablePropertySources sources = ((AbstractEnvironment) environment).getPropertySources();
28 | StreamSupport.stream(sources.spliterator(), false).filter(ps -> ps instanceof EnumerablePropertySource)
29 | .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()).flatMap(Arrays::stream).distinct()
30 | .forEach(prop -> {
31 | Object resolved = environment.getProperty(prop, Object.class);
32 | if (resolved instanceof String) {
33 | LOGGER.info("{} - {}", prop, environment.getProperty(prop));
34 | } else {
35 | LOGGER.info("{} - {}", prop, "NON-STRING-VALUE");
36 | }
37 |
38 | });
39 | LOGGER.debug("===========================================");
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/in28minutes/microservices/currencyexchangeservice/util/environment/InstanceInformationService.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.util.environment;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Service;
5 |
6 | @Service
7 | public class InstanceInformationService {
8 |
9 | private static final String HOST_NAME = "HOSTNAME";
10 |
11 | private static final String DEFAULT_ENV_INSTANCE_GUID = "UNKNOWN";
12 |
13 | // @Value(${ENVIRONMENT_VARIABLE_NAME:DEFAULT_VALUE})
14 | @Value("${" + HOST_NAME + ":" + DEFAULT_ENV_INSTANCE_GUID + "}")
15 | private String hostName;
16 |
17 | public String retrieveInstanceInfo() {
18 | return hostName + " v1 " + hostName.substring(hostName.length()-5);
19 | }
20 |
21 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=currency-exchange
2 | server.port=8000
3 |
4 | spring.jpa.show-sql=true
5 | spring.h2.console.enabled=true
6 | spring.h2.console.settings.web-allow-others=true
7 |
8 | #logging.level.org.springframework=debug
9 | management.endpoints.web.base-path=/manage
10 | management.endpoints.web.exposure.include=*
11 |
12 | spring.security.user.name=in28minutes
13 | spring.security.user.password=dummy
14 |
15 | #Feign and Ribbon Timeouts
16 | feign.client.config.default.connectTimeout=50000
17 | feign.client.config.default.readTimeout=50000
18 | ribbon.ConnectTimeout= 60000
19 | ribbon.ReadTimeout= 60000
--------------------------------------------------------------------------------
/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 | insert into exchange_value(id,currency_from,currency_to,conversion_multiple)
2 | values(10001,'USD','INR',65);
3 | insert into exchange_value(id,currency_from,currency_to,conversion_multiple)
4 | values(10002,'EUR','INR',75);
5 | insert into exchange_value(id,currency_from,currency_to,conversion_multiple)
6 | values(10003,'AUD','INR',25);
--------------------------------------------------------------------------------
/src/test/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/test/.DS_Store
--------------------------------------------------------------------------------
/src/test/java/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/test/java/.DS_Store
--------------------------------------------------------------------------------
/src/test/java/com/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/test/java/com/.DS_Store
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/test/java/com/in28minutes/.DS_Store
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/test/java/com/in28minutes/microservices/.DS_Store
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/NinjaDevOps0831/jenkin-devops-microservice/260a24c72f1d07de4308be0311bc69930ee769c8/src/test/java/com/in28minutes/microservices/currencyexchangeservice/.DS_Store
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/CurrencyExchangeServiceApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class CurrencyExchangeServiceApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/cucumber/CucumberSpringContextConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.cucumber;
2 |
3 | import org.springframework.boot.test.context.SpringBootContextLoader;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
6 | import org.springframework.test.context.ContextConfiguration;
7 |
8 | import com.in28minutes.microservices.currencyexchangeservice.CurrencyExchangeServiceApplicationH2;
9 |
10 | import cucumber.api.java.Before;
11 |
12 | @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
13 | @ContextConfiguration(classes = CurrencyExchangeServiceApplicationH2.class, loader = SpringBootContextLoader.class)
14 | public class CucumberSpringContextConfiguration {
15 |
16 | @Before
17 | public void setUp() {
18 | }
19 | }
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/cucumber/CurrencyExchangeSteps.java:
--------------------------------------------------------------------------------
1 |
2 | package com.in28minutes.microservices.currencyexchangeservice.cucumber;
3 |
4 | import cucumber.api.java.en.Given;
5 | import cucumber.api.java.en.Then;
6 | import cucumber.api.java.en.When;
7 | import io.restassured.RestAssured;
8 | import io.restassured.builder.RequestSpecBuilder;
9 | import io.restassured.http.ContentType;
10 | import io.restassured.http.Method;
11 | import io.restassured.response.ExtractableResponse;
12 | import io.restassured.response.Response;
13 | import io.restassured.response.ValidatableResponse;
14 |
15 | import org.junit.Assert;
16 |
17 | import static io.restassured.RestAssured.when;
18 |
19 | public class CurrencyExchangeSteps {
20 |
21 | float output = 0f;
22 |
23 | @Given("^conversion rate for (.*) to (.*)$")
24 | public void conversion_rate_for_fromcurrency_to_tocurrency(String from, String to) throws Exception {
25 | RestAssured.requestSpecification = new RequestSpecBuilder()
26 | .setContentType(ContentType.JSON)
27 | .setAccept(ContentType.JSON)
28 | .build();
29 | String url = "http://localhost:8000/currency-exchange/from/"+from+"/to/"+to;
30 | System.out.println(url);
31 | Response request = when().request(Method.GET,url);
32 | ValidatableResponse then = request.then();
33 | ValidatableResponse statusCode = then.statusCode(200);
34 | ExtractableResponse extract = statusCode.extract();
35 | //then.extract().path("");
36 | output = extract.path("conversionMultiple");
37 | }
38 |
39 | @When("^the system is asked to provide the conversion rate$")
40 | public void the_system_is_asked_to_provide_the_conversion_rate() throws Exception {
41 | }
42 |
43 | @Then("^It should output (.*)$")
44 | public void thenCheckOutput(float response) {
45 | Assert.assertEquals(output, response,0.5);
46 |
47 | }
48 |
49 | public static void main(String[] args) {
50 | }
51 | }
52 |
53 |
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/cucumber/HelloWorldSteps.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.cucumber;
2 |
3 | import cucumber.api.java.en.Given;
4 | import cucumber.api.java.en.Then;
5 | import cucumber.api.java.en.When;
6 | import org.junit.Assert;
7 |
8 | import com.in28minutes.microservices.currencyexchangeservice.HelloWorld;
9 |
10 | public class HelloWorldSteps {
11 |
12 | private HelloWorld helloWorld = new HelloWorld();
13 |
14 |
15 | private String name = "";
16 |
17 | private String output = "";
18 |
19 | @Given("^A String name (.*)$")
20 | public void givenInput(String name) {
21 | this.name = name;
22 | }
23 | @When("^sayHello method of HelloWorld.java is called$")
24 | public void whenBusinessLogicCalled() {
25 | output = helloWorld.sayHello(name);
26 | }
27 | @Then("^It should return (.*)$")
28 | public void thenCheckOutput(String response) {
29 | Assert.assertEquals(output, response);
30 | }
31 |
32 |
33 | public static void main(String[] args) {
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/cucumber/RunCucumberIntegrationTestCase.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.cucumber;
2 |
3 | import org.junit.runner.RunWith;
4 |
5 | import cucumber.api.CucumberOptions;
6 | import cucumber.api.junit.Cucumber;
7 |
8 | @RunWith(Cucumber.class)
9 | @CucumberOptions(monochrome = true, features = "src/test/resources", plugin = { "pretty" })
10 | public class RunCucumberIntegrationTestCase {
11 | }
--------------------------------------------------------------------------------
/src/test/java/com/in28minutes/microservices/currencyexchangeservice/resource/CurrencyExchangeControllerTest.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.microservices.currencyexchangeservice.resource;
2 |
3 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5 |
6 | import java.math.BigDecimal;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 | import org.mockito.Mockito;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13 | import org.springframework.boot.test.mock.mockito.MockBean;
14 | import org.springframework.test.context.junit4.SpringRunner;
15 | import org.springframework.test.web.servlet.MockMvc;
16 |
17 | import com.in28minutes.microservices.currencyexchangeservice.util.environment.InstanceInformationService;
18 |
19 |
20 | @RunWith(SpringRunner.class)
21 | @WebMvcTest(CurrencyExchangeController.class)
22 | public class CurrencyExchangeControllerTest {
23 | @Autowired
24 | private MockMvc mvc;
25 |
26 | @MockBean
27 | private ExchangeValueRepository repository;
28 |
29 | @MockBean
30 | private InstanceInformationService instanceInformationService;
31 |
32 | @Test
33 | public void imHealthy() throws Exception {
34 | mvc.perform(get("/")).andExpect(status().isOk());
35 | }
36 |
37 | @Test
38 | public void retrieveExchangeValue() throws Exception {
39 | Mockito.when(repository.findByFromAndTo("EUR", "INR")).thenReturn(new ExchangeValue(Long.getLong("1"), "EUR", "INR", BigDecimal.valueOf(80.00)));
40 | mvc.perform(get("/currency-exchange/from/EUR/to/INR")).andExpect(status().isOk());
41 | }
42 | }
--------------------------------------------------------------------------------
/src/test/resources/cucum.feature:
--------------------------------------------------------------------------------
1 | Feature: Cucumber hello world example
2 |
3 | Scenario Outline: Hello World
4 | Given A String name
5 | When sayHello method of HelloWorld.java is called
6 | Then It should return