Ravi Srinivasan
2019-07-12 fe9006dcadfc00605f012d2bc5492132c436c50c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.redhat.movies;
 
import java.util.List;
 
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MoviesApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MoviesApplicationTests {
 
    @Autowired
    private TestRestTemplate restTemplate;
 
    @LocalServerPort
    private int port;
 
    @Test
    public void contextLoads() {
    }
 
    @Test
    public void testNotNullResponse() {
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<String>(null, headers);
 
        ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/movies", HttpMethod.GET,
                entity, String.class);
 
        Assert.assertNotNull(response.getBody());
    }
 
    @Test
    public void testGetAllMovies() {
 
        ResponseEntity <List<Movie>> response = restTemplate.exchange("http://localhost:" + port + "/movies",
            HttpMethod.GET, null, new ParameterizedTypeReference <List<Movie>> () {});
 
        List <Movie> movies = response.getBody();
        Assert.assertNotNull(movies);
        Assert.assertEquals(7, movies.size());
        Assert.assertEquals("The Godfather", movies.get(0).getName());
    }
 
    @Test
    public void testGetStatus() {
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<String>(null, headers);
 
        ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/status", HttpMethod.GET,
                entity, String.class);
 
        Assert.assertNotNull(response.getBody());
        Assert.assertEquals("Ready", response.getBody());
    }
 
}