GET endpoint
- no DB support jet - Integation Tets
This commit is contained in:
		
							parent
							
								
									64707ff810
								
							
						
					
					
						commit
						198b49dd8f
					
				
							
								
								
									
										22
									
								
								src/main/java/djmil/cashcard/CashCardController.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/main/java/djmil/cashcard/CashCardController.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					package djmil.cashcard;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.GetMapping;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.PathVariable;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.RequestMapping;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.RestController;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RestController
 | 
				
			||||||
 | 
					@RequestMapping("/cashcards")
 | 
				
			||||||
 | 
					public class CashCardController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @GetMapping("/{requestedId}")
 | 
				
			||||||
 | 
					    public ResponseEntity<CashCard> findById(@PathVariable Long requestedId) {
 | 
				
			||||||
 | 
					        if (requestedId.equals(99L)) {
 | 
				
			||||||
 | 
					            CashCard cashCard = new CashCard(99L, 123.45);
 | 
				
			||||||
 | 
					            return ResponseEntity.ok(cashCard);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            return ResponseEntity.notFound().build();
 | 
				
			||||||
 | 
					    	}
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,13 +1,48 @@
 | 
				
			|||||||
package djmil.cashcard;
 | 
					package djmil.cashcard;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.junit.jupiter.api.Test;
 | 
					import org.junit.jupiter.api.Test;
 | 
				
			||||||
import org.springframework.boot.test.context.SpringBootTest;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
@SpringBootTest
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.boot.test.context.SpringBootTest;
 | 
				
			||||||
 | 
					import org.springframework.boot.test.web.client.TestRestTemplate;
 | 
				
			||||||
 | 
					import org.springframework.http.HttpStatus;
 | 
				
			||||||
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.jayway.jsonpath.DocumentContext;
 | 
				
			||||||
 | 
					import com.jayway.jsonpath.JsonPath;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.assertj.core.api.Assertions.assertThat;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
 | 
				
			||||||
class CashcardApplicationTests {
 | 
					class CashcardApplicationTests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Test
 | 
						@Test
 | 
				
			||||||
	void contextLoads() {
 | 
						void contextLoads() {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Autowired
 | 
				
			||||||
 | 
					    TestRestTemplate restTemplate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    void shouldReturnACashCardWhenDataIsSaved() {
 | 
				
			||||||
 | 
					        ResponseEntity<String> response = restTemplate.getForEntity("/cashcards/99", String.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							DocumentContext documentContext = JsonPath.parse(response.getBody());
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							Number id = documentContext.read("$.id");
 | 
				
			||||||
 | 
							assertThat(id).isEqualTo(99);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							Double amount = documentContext.read("$.amount");
 | 
				
			||||||
 | 
							assertThat(amount).isEqualTo(123.45);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Test
 | 
				
			||||||
 | 
						void shouldNotReturnACashCardWithAnUnknownId() {
 | 
				
			||||||
 | 
							ResponseEntity<String> response = restTemplate.getForEntity("/cashcards/1000", String.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  		assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
 | 
				
			||||||
 | 
							assertThat(response.getBody()).isBlank();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user