Skip to content

Commit

Permalink
Add SpringFox and query params to HelloController
Browse files Browse the repository at this point in the history
  • Loading branch information
RafhaanShah committed Oct 12, 2020
1 parent 31e3ba4 commit 2bcd083
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ plugins {
}

group = 'com.raf.springbootcamp'
version = '0.0.1-SNAPSHOT'
version = '0.0.2-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}

springBoot {
buildInfo()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

implementation "io.springfox:springfox-boot-starter:3.0.0"
implementation "io.springfox:springfox-swagger-ui:3.0.0"

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/raf/springbootcamp/demo/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import springfox.documentation.oas.annotations.EnableOpenApi;

@SpringBootApplication
@EnableOpenApi
public class Application {

public static void main(String[] args) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/raf/springbootcamp/demo/HelloController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.raf.springbootcamp.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "Hello")
public class HelloController {

@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
@GetMapping("/")
@ApiOperation("Says 'Hello'")
public String getHello(@RequestParam(value = "name", defaultValue = "World", required = false) String name) {
return String.format("Hello, %s!", name);
}

}
45 changes: 45 additions & 0 deletions src/main/java/com/raf/springbootcamp/demo/SpringFoxConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.raf.springbootcamp.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;

@Configuration
public class SpringFoxConfig {

@Autowired
private BuildProperties buildProperties;

@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.basePackage(buildProperties.getGroup()))
.paths(PathSelectors.any())
.build()
.apiInfo(getApiInfo());
}

private ApiInfo getApiInfo() {
return new ApiInfo(
"Spring Boot Demo",
"OpenAPI Documentation",
buildProperties.getVersion(),
"",
new Contact("","",""),
"",
"",
Collections.emptyList()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ public class HelloControllerTest {
private MockMvc mvc;

@Test
public void getHello() throws Exception {
public void getHello_returnsCorrectHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
.andExpect(content().string(equalTo("Hello, World!")));
}

@Test
public void getHelloWithName_returnsCorrectHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").queryParam("name", "Jon").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello, Jon!")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public void setUp() throws Exception {
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(),
String.class);
assertThat(response.getBody()).isEqualTo("Greetings from Spring Boot!");
assertThat(response.getBody()).isEqualTo("Hello, World!");
}
}

0 comments on commit 2bcd083

Please sign in to comment.