Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f80fcfd7

Přidáno uživatelem Václav Hrabík před téměř 2 roky(ů)

#10258 vytvoření prof of concept kontraktových testů s použitím groovy scriptů

Zobrazit rozdíly:

pom.xml
24 24
		<com.fasterxml.jackson.core.version>2.13.3</com.fasterxml.jackson.core.version>
25 25
		<org.junit.jupiter.version>5.4.0</org.junit.jupiter.version>
26 26
		<org.apache.maven.plugins.version>3.0.0-M7</org.apache.maven.plugins.version>
27
		<spring-cloud-contract.version>3.0.3</spring-cloud-contract.version>
27 28
	</properties>
28 29

  
29 30
	<dependencies>
......
45 46
			<artifactId>spring-boot-starter-web</artifactId>
46 47
		</dependency>
47 48

  
48
<!--		<dependency>-->
49
<!--			<groupId>org.springframework.boot</groupId>-->
50
<!--			<artifactId>spring-boot-starter-security</artifactId>-->
51
<!--		</dependency>-->
49
		<dependency>
50
			<groupId>org.springframework.security</groupId>
51
			<artifactId>spring-security-web</artifactId>
52
		</dependency>
52 53

  
53 54
		<dependency>
54 55
			<groupId>mysql</groupId>
......
98 99
			<version>${com.fasterxml.jackson.core.version}</version>
99 100
		</dependency>
100 101

  
101
	<!-- Security library -->
102
		<!-- Security library -->
102 103
		<dependency>
103 104
			<groupId>org.springframework.boot</groupId>
104 105
			<artifactId>spring-boot-starter-security</artifactId>
......
108 109
			<groupId>org.apache.httpcomponents</groupId>
109 110
			<artifactId>httpclient</artifactId>
110 111
		</dependency>
112

  
111 113
		<!-- Junit libraries for unit tests-->
112 114
		<dependency>
113 115
			<groupId>org.junit.jupiter</groupId>
......
129 131
			<artifactId>json-simple</artifactId>
130 132
			<version>1.1.1</version>
131 133
		</dependency>
134

  
132 135
		<!-- UI TESTING -->
133 136
		<dependency>
134 137
			<groupId>org.seleniumhq.selenium</groupId>
......
160 163
			<scope>test</scope>
161 164
		</dependency>
162 165

  
166
		<!-- Contract testing -->
167
		<dependency>
168
			<groupId>org.springframework.cloud</groupId>
169
			<artifactId>spring-cloud-starter-contract-verifier</artifactId>
170
			<version>2.1.1.RELEASE</version>
171
			<scope>test</scope>
172
		</dependency>
173

  
163 174
    </dependencies>
164 175

  
165 176
	<build>
......
185 196
				<version>${org.apache.maven.plugins.version}</version>
186 197
			</plugin>
187 198

  
199
			<plugin>
200
				<groupId>org.springframework.cloud</groupId>
201
				<artifactId>spring-cloud-contract-maven-plugin</artifactId>
202
				<version>${spring-cloud-contract.version}</version>
203
				<extensions>true</extensions>
204
				<configuration>
205
					<testFramework>JUNIT5</testFramework>
206
					<baseClassForTests>
207
						cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller.BaseTest
208
					</baseClassForTests>
209
				</configuration>
210
			</plugin>
211

  
188 212
		</plugins>
189 213
	</build>
190 214

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java
2 2

  
3 3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider;
4 4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthServiceImpl;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserServiceImpl;
5 7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.beans.factory.annotation.Qualifier;
6 9
import org.springframework.boot.web.servlet.FilterRegistrationBean;
7 10
import org.springframework.context.annotation.Bean;
8 11
import org.springframework.context.annotation.Configuration;
......
14 17
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15 18
import org.springframework.security.config.http.SessionCreationPolicy;
16 19
import org.springframework.security.core.userdetails.UserDetailsService;
17
import org.springframework.security.web.SecurityFilterChain;
18
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
20
import org.springframework.security.web.FilterChainProxy;
19 21
import org.springframework.web.client.RestTemplate;
20 22
import org.springframework.web.cors.CorsConfiguration;
21 23
import org.springframework.web.cors.CorsConfigurationSource;
22 24
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
25
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
23 26

  
24 27
import java.util.Arrays;
25 28

  
......
27 30
@EnableWebSecurity(debug = true)
28 31
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
29 32

  
30
    @Autowired
31
    private UserDetailsService userService;
33
    @Bean
34
    public FilterChainProxy filterChainProxy() {
35
        return new FilterChainProxy();
36
    }
37

  
38
//    @Autowired
39
    private final UserDetailsService userService = new OAuthServiceImpl();
32 40

  
33 41
    @Autowired
34 42
    private JwtAuthenticationFilter jwtAuthenticationFilter;
......
66 74
        return super.authenticationManagerBean();
67 75
    }
68 76

  
69

  
70 77
    @Override
71 78
    protected void configure(HttpSecurity httpSecurity) throws Exception {
72 79
        httpSecurity
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/BaseTest.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.AntiPatternDetectionAppApplication;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository;
6
import io.restassured.module.mockmvc.RestAssuredMockMvc;
7
import org.junit.Before;
8
import org.junit.runner.RunWith;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.boot.test.context.SpringBootTest;
11
import org.springframework.boot.test.mock.mockito.MockBean;
12
import org.springframework.test.context.junit4.SpringRunner;
13
import org.springframework.web.context.WebApplicationContext;
14

  
15
import static org.mockito.ArgumentMatchers.any;
16
import static org.mockito.Mockito.when;
17

  
18
@RunWith(SpringRunner.class)
19
@SpringBootTest(classes = AntiPatternDetectionAppApplication.class)
20
public abstract class BaseTest {
21

  
22
    @Autowired
23
    WebApplicationContext webApplicationContext;
24

  
25
    @MockBean
26
    private UserRepository userRepository;
27

  
28

  
29

  
30
    @Before
31
    public void init() {
32
        User user = new User("foo", "foo@foo.foo", "foo");
33

  
34
        when(userRepository.findByName("non-existing")).thenReturn(null);
35
        when(userRepository.findByName("foo")).thenReturn(user);
36
        when(userRepository.save(any(User.class))).thenReturn(user);
37
        RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
38
    }
39

  
40
}
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserControllerTest.java
3 3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes;
4 4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService;
5 5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
6
import org.junit.jupiter.api.Test;
6
import org.junit.Test;
7 7
import org.junit.jupiter.api.extension.ExtendWith;
8
import org.junit.runner.RunWith;
8 9
import org.mockito.Mockito;
9 10
import org.springframework.beans.factory.annotation.Autowired;
10 11
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12
import org.springframework.boot.test.context.SpringBootTest;
11 13
import org.springframework.boot.test.mock.mockito.MockBean;
12 14
import org.springframework.http.HttpStatus;
13 15
import org.springframework.http.MediaType;
14 16
import org.springframework.mock.web.MockHttpServletResponse;
15 17
import org.springframework.security.test.context.support.WithMockUser;
16 18
import org.springframework.test.context.junit.jupiter.SpringExtension;
19
import org.springframework.test.context.junit4.SpringRunner;
17 20
import org.springframework.test.web.servlet.MockMvc;
18 21
import org.springframework.test.web.servlet.MvcResult;
19 22
import org.springframework.test.web.servlet.RequestBuilder;
......
24 27
import static org.junit.jupiter.api.Assertions.assertEquals;
25 28
import static org.mockito.ArgumentMatchers.any;
26 29

  
27
@ExtendWith(SpringExtension.class)
30
//@ExtendWith(SpringExtension.class)
31
@RunWith(SpringRunner.class)
32
//@SpringBootTest
28 33
@WebMvcTest(value = UserController.class)
29
@WithMockUser
34
//@WithMockUser
30 35
public class UserControllerTest {
31 36

  
32 37
    @Autowired
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpTest.java
12 12

  
13 13
import static org.junit.jupiter.api.Assertions.assertEquals;
14 14
import static org.mockito.ArgumentMatchers.any;
15
import static org.mockito.BDDMockito.given;
15 16
import static org.mockito.Mockito.when;
16 17

  
17 18
/**
src/test/resources/contracts/registerNewUser_201.groovy
1
package contracts
2

  
3
import org.springframework.cloud.contract.spec.Contract
4

  
5
Contract.make {
6
    description "Creates new user in the application"
7
    request {
8
        method POST()
9
        url("/v2/user/register")
10
        headers {
11
            contentType applicationJson()
12
        }
13
        body(
14
                "name": "non-existing",
15
                "password": "foo",
16
                "email": "foo@foo.foo"
17
        )
18
    }
19
    response {
20
        body(
21
                "message" : "User successfully created"
22
        )
23
        status 201
24
    }
25
}
src/test/resources/contracts/registerNewUser_400.groovy
1
package contracts
2

  
3
import org.springframework.cloud.contract.spec.Contract
4

  
5
Contract.make {
6
    description "User already exists"
7
    request {
8
        method POST()
9
        url("/v2/user/register")
10
        headers {
11
            contentType applicationJson()
12
        }
13
        body(
14
                "name": "foo",
15
                "password": "foo",
16
                "email": "foo@foo.foo"
17
        )
18
    }
19
    response {
20
        body(
21
                "message" : "User exists"
22
        )
23
        status 400
24
    }
25
}
src/test/resources/swagger_prep/registerExistingUser.yaml
1
openapi: 3.0.0
2
info:
3
    title: todo title
4
    description: todo desc
5
    version: 2.0.0
6

  
7
paths:
8
  /v2/user/register:
9
    post:
10
      summary: Registers a new user to the database
11
      description: desc
12
      requestBody:
13
        required: true
14
        content:
15
          application/json:
16
            schema:
17
              $ref: '#/components/schemas/User'
18
            examples:
19
              '201':
20
                value:
21
                  {
22
                    name: non-existing,
23
                    email: foo@foo.foo,
24
                    password: foo
25
                  }
26

  
27
      responses:
28
        '201':  #status code
29
          description: TODO
30
          content:
31
            application/json:
32
              schema:
33
                type: array
34
                items:
35
                  type: string
36
                  properties:
37
                    message:
38
                      type: string
39
              examples:
40
                '201':
41
                  value:
42
                    {
43
                      message: User successfully created
44
                    }
45

  
46
components:
47
  schemas:
48

  
49
    User:
50
      title: User
51
      type: object
52
      properties:
53
        name:
54
          type: string
55
          description: User name
56
        email:
57
          type: string
58
          description: User email
59
        password:
60
          type: string
61
          description: User password
62
      required:
63
        - name
64
        - email
65
        - password

Také k dispozici: Unified diff