Revize 91476843
Přidáno uživatelem Václav Hrabík před asi 2 roky(ů)
pom.xml | ||
---|---|---|
22 | 22 |
<org.reflections.version>0.10.2</org.reflections.version> |
23 | 23 |
<org.jsoup.version>1.15.2</org.jsoup.version> |
24 | 24 |
<com.fasterxml.jackson.core.version>2.13.3</com.fasterxml.jackson.core.version> |
25 |
<org.junit.jupiter.version>5.4.0</org.junit.jupiter.version>
|
|
25 |
<org.junit.jupiter.version>5.9.1</org.junit.jupiter.version>
|
|
26 | 26 |
<org.apache.maven.plugins.version>3.0.0-M7</org.apache.maven.plugins.version> |
27 | 27 |
<spring-cloud-contract.version>3.0.3</spring-cloud-contract.version> |
28 | 28 |
</properties> |
29 | 29 |
|
30 |
<dependencies> |
|
30 |
<dependencyManagement> |
|
31 |
<dependencies> |
|
32 |
<dependency> |
|
33 |
<groupId>org.junit</groupId> |
|
34 |
<artifactId>junit-bom</artifactId> |
|
35 |
<version>5.9.3</version> |
|
36 |
<type>pom</type> |
|
37 |
<scope>import</scope> |
|
38 |
</dependency> |
|
39 |
</dependencies> |
|
40 |
</dependencyManagement> |
|
31 | 41 |
|
42 |
<dependencies> |
|
32 | 43 |
|
33 | 44 |
<dependency> |
34 | 45 |
<groupId>org.springframework.boot</groupId> |
... | ... | |
112 | 123 |
|
113 | 124 |
<!-- Junit libraries for unit tests--> |
114 | 125 |
<dependency> |
115 |
<groupId>org.junit.jupiter</groupId> |
|
116 |
<artifactId>junit-jupiter-api</artifactId> |
|
117 |
<version>${org.junit.jupiter.version}</version> |
|
126 |
<groupId>org.junit.platform</groupId> |
|
127 |
<artifactId>junit-platform-launcher</artifactId> |
|
118 | 128 |
<scope>test</scope> |
119 | 129 |
</dependency> |
120 | 130 |
|
121 | 131 |
<dependency> |
122 | 132 |
<groupId>org.junit.jupiter</groupId> |
123 | 133 |
<artifactId>junit-jupiter-engine</artifactId> |
124 |
<version>${org.junit.jupiter.version}</version> |
|
134 |
<scope>test</scope> |
|
135 |
</dependency> |
|
136 |
|
|
137 |
<dependency> |
|
138 |
<groupId>org.junit.vintage</groupId> |
|
139 |
<artifactId>junit-vintage-engine</artifactId> |
|
125 | 140 |
<scope>test</scope> |
126 | 141 |
</dependency> |
127 | 142 |
|
... | ... | |
151 | 166 |
<artifactId>spring-security-test</artifactId> |
152 | 167 |
<scope>test</scope> |
153 | 168 |
</dependency> |
154 |
<dependency> |
|
155 |
<groupId>junit</groupId> |
|
156 |
<artifactId>junit</artifactId> |
|
157 |
<scope>test</scope> |
|
158 |
</dependency> |
|
159 |
<dependency> |
|
160 |
<groupId>org.mockito</groupId> |
|
161 |
<artifactId>mockito-inline</artifactId> |
|
162 |
<version>4.3.1</version> |
|
163 |
<scope>test</scope> |
|
164 |
</dependency> |
|
165 | 169 |
|
166 | 170 |
<!-- Contract testing --> |
167 | 171 |
<dependency> |
... | ... | |
171 | 175 |
<scope>test</scope> |
172 | 176 |
</dependency> |
173 | 177 |
|
178 |
<!-- MS SQL Server DataBase --> |
|
179 |
|
|
174 | 180 |
<dependency> |
175 | 181 |
<groupId>com.microsoft.sqlserver</groupId> |
176 | 182 |
<artifactId>mssql-jdbc</artifactId> |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/BaseTest.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
2 | 2 |
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.AntiPatternDetectionAppApplication; |
|
4 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
5 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository; |
6 | 5 |
import io.restassured.module.mockmvc.RestAssuredMockMvc; |
7 |
import org.junit.Before; |
|
8 |
import org.junit.runner.RunWith; |
|
6 |
import org.junit.jupiter.api.BeforeEach; |
|
9 | 7 |
import org.springframework.beans.factory.annotation.Autowired; |
10 | 8 |
import org.springframework.boot.test.context.SpringBootTest; |
11 | 9 |
import org.springframework.boot.test.mock.mockito.MockBean; |
12 |
import org.springframework.test.context.junit4.SpringRunner; |
|
13 | 10 |
import org.springframework.web.context.WebApplicationContext; |
14 | 11 |
|
15 | 12 |
import static org.mockito.ArgumentMatchers.any; |
16 | 13 |
import static org.mockito.Mockito.when; |
17 | 14 |
|
18 |
@RunWith(SpringRunner.class) |
|
19 |
@SpringBootTest(classes = AntiPatternDetectionAppApplication.class) |
|
15 |
@SpringBootTest() |
|
20 | 16 |
public abstract class BaseTest { |
21 | 17 |
|
22 | 18 |
@Autowired |
... | ... | |
25 | 21 |
@MockBean |
26 | 22 |
private UserRepository userRepository; |
27 | 23 |
|
28 |
|
|
29 |
|
|
30 |
@Before |
|
24 |
@BeforeEach |
|
31 | 25 |
public void init() { |
32 | 26 |
User user = new User("foo", "foo@foo.foo", "foo"); |
33 | 27 |
|
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserControllerTest.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
6 |
import org.junit.Test; |
|
7 |
import org.junit.jupiter.api.extension.ExtendWith; |
|
8 |
import org.junit.runner.RunWith; |
|
9 |
import org.mockito.Mockito; |
|
10 |
import org.springframework.beans.factory.annotation.Autowired; |
|
11 |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
|
12 |
import org.springframework.boot.test.context.SpringBootTest; |
|
13 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
import org.springframework.http.HttpStatus; |
|
15 |
import org.springframework.http.MediaType; |
|
16 |
import org.springframework.mock.web.MockHttpServletResponse; |
|
17 |
import org.springframework.security.test.context.support.WithMockUser; |
|
18 |
import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
19 |
import org.springframework.test.context.junit4.SpringRunner; |
|
20 |
import org.springframework.test.web.servlet.MockMvc; |
|
21 |
import org.springframework.test.web.servlet.MvcResult; |
|
22 |
import org.springframework.test.web.servlet.RequestBuilder; |
|
23 |
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
|
24 |
|
|
25 |
import java.util.HashMap; |
|
26 |
|
|
27 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
28 |
import static org.mockito.ArgumentMatchers.any; |
|
29 |
|
|
30 |
//@ExtendWith(SpringExtension.class) |
|
31 |
@RunWith(SpringRunner.class) |
|
32 |
//@SpringBootTest |
|
33 |
@WebMvcTest(value = UserController.class) |
|
34 |
//@WithMockUser |
|
35 |
public class UserControllerTest { |
|
36 |
|
|
37 |
@Autowired |
|
38 |
private MockMvc mockMvc; |
|
39 |
|
|
40 |
@MockBean |
|
41 |
private UserService userService; |
|
42 |
|
|
43 |
|
|
44 |
@Test |
|
45 |
public void registerUserNew() throws Exception { |
|
46 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_CREATED); |
|
47 |
HashMap<String,Object> map = new HashMap<>(); |
|
48 |
map.put("name","pepa"); |
|
49 |
map.put("password","ahojSvete"); |
|
50 |
map.put("email","yxz@ahoj.cz"); |
|
51 |
String json = JSONBuilder.buildJSON(map); |
|
52 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
53 |
.post("/v2/user/register") |
|
54 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
55 |
.contentType(MediaType.APPLICATION_JSON); |
|
56 |
|
|
57 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
58 |
|
|
59 |
MockHttpServletResponse response = result.getResponse(); |
|
60 |
|
|
61 |
assertEquals(HttpStatus.CREATED.value(), response.getStatus()); |
|
62 |
} |
|
63 |
@Test |
|
64 |
public void registerUserInvalidArguments() throws Exception { |
|
65 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.INVALID_USER_ARGUMENTS); |
|
66 |
HashMap<String,Object> map = new HashMap<>(); |
|
67 |
map.put("name","pepa"); |
|
68 |
map.put("password","ahojSvete"); |
|
69 |
map.put("email","yxz"); |
|
70 |
String json = JSONBuilder.buildJSON(map); |
|
71 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
72 |
.post("/v2/user/register") |
|
73 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
74 |
.contentType(MediaType.APPLICATION_JSON); |
|
75 |
|
|
76 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
77 |
|
|
78 |
MockHttpServletResponse response = result.getResponse(); |
|
79 |
|
|
80 |
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); |
|
81 |
} |
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
@Test |
|
86 |
public void registerUserExists() throws Exception { |
|
87 |
Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_EXISTS); |
|
88 |
HashMap<String,Object> map = new HashMap<>(); |
|
89 |
map.put("name","pepa"); |
|
90 |
map.put("password","ahojSvete"); |
|
91 |
map.put("email","yxz@ahoj.cz"); |
|
92 |
String json = JSONBuilder.buildJSON(map); |
|
93 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
94 |
.post("/v2/user/register") |
|
95 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
96 |
.contentType(MediaType.APPLICATION_JSON); |
|
97 |
|
|
98 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
99 |
|
|
100 |
MockHttpServletResponse response = result.getResponse(); |
|
101 |
|
|
102 |
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); |
|
103 |
} |
|
104 |
|
|
105 |
@Test |
|
106 |
public void loginValidUser() throws Exception { |
|
107 |
Mockito.when(userService.verifyUser(any())).thenReturn(UserModelStatusCodes.USER_LOGGED_IN); |
|
108 |
HashMap<String,Object> map = new HashMap<>(); |
|
109 |
map.put("name","pepa"); |
|
110 |
map.put("password","ahojSvete"); |
|
111 |
map.put("email","yxz@ahoj.cz"); |
|
112 |
String json = JSONBuilder.buildJSON(map); |
|
113 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
114 |
.post("/v2/user/login") |
|
115 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
116 |
.contentType(MediaType.APPLICATION_JSON); |
|
117 |
|
|
118 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
119 |
|
|
120 |
MockHttpServletResponse response = result.getResponse(); |
|
121 |
|
|
122 |
assertEquals(HttpStatus.OK.value(), response.getStatus()); |
|
123 |
} |
|
124 |
|
|
125 |
@Test |
|
126 |
public void loginInValidUser() throws Exception { |
|
127 |
Mockito.when(userService.verifyUser(any())).thenReturn(UserModelStatusCodes.USER_LOGIN_FAILED); |
|
128 |
HashMap<String,Object> map = new HashMap<>(); |
|
129 |
map.put("name","pepa"); |
|
130 |
map.put("password","ahojSvete"); |
|
131 |
map.put("email","yxz@ahoj.cz"); |
|
132 |
String json = JSONBuilder.buildJSON(map); |
|
133 |
RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
134 |
.post("/v2/user/login") |
|
135 |
.accept(MediaType.APPLICATION_JSON).content(json) |
|
136 |
.contentType(MediaType.APPLICATION_JSON); |
|
137 |
|
|
138 |
MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
139 |
|
|
140 |
MockHttpServletResponse response = result.getResponse(); |
|
141 |
|
|
142 |
assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatus()); |
|
143 |
} |
|
144 |
|
|
145 |
} |
|
1 |
//package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
|
2 |
// |
|
3 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
4 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
|
5 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
6 |
//import org.junit.Test; |
|
7 |
//import org.junit.jupiter.api.extension.ExtendWith; |
|
8 |
//import org.junit.runner.RunWith; |
|
9 |
//import org.mockito.Mockito; |
|
10 |
//import org.springframework.beans.factory.annotation.Autowired; |
|
11 |
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
|
12 |
//import org.springframework.boot.test.context.SpringBootTest; |
|
13 |
//import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
//import org.springframework.http.HttpStatus; |
|
15 |
//import org.springframework.http.MediaType; |
|
16 |
//import org.springframework.mock.web.MockHttpServletResponse; |
|
17 |
//import org.springframework.security.test.context.support.WithMockUser; |
|
18 |
//import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
19 |
//import org.springframework.test.context.junit4.SpringRunner; |
|
20 |
//import org.springframework.test.web.servlet.MockMvc; |
|
21 |
//import org.springframework.test.web.servlet.MvcResult; |
|
22 |
//import org.springframework.test.web.servlet.RequestBuilder; |
|
23 |
//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
|
24 |
// |
|
25 |
//import java.util.HashMap; |
|
26 |
// |
|
27 |
//import static org.junit.jupiter.api.Assertions.assertEquals; |
|
28 |
//import static org.mockito.ArgumentMatchers.any; |
|
29 |
// |
|
30 |
////@ExtendWith(SpringExtension.class) |
|
31 |
//@RunWith(SpringRunner.class) |
|
32 |
////@SpringBootTest |
|
33 |
//@WebMvcTest(value = UserController.class) |
|
34 |
////@WithMockUser |
|
35 |
//public class UserControllerTest { |
|
36 |
// |
|
37 |
// @Autowired |
|
38 |
// private MockMvc mockMvc; |
|
39 |
// |
|
40 |
// @MockBean |
|
41 |
// private UserService userService; |
|
42 |
// |
|
43 |
// |
|
44 |
// @Test |
|
45 |
// public void registerUserNew() throws Exception { |
|
46 |
// Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_CREATED); |
|
47 |
// HashMap<String,Object> map = new HashMap<>(); |
|
48 |
// map.put("name","pepa"); |
|
49 |
// map.put("password","ahojSvete"); |
|
50 |
// map.put("email","yxz@ahoj.cz"); |
|
51 |
// String json = JSONBuilder.buildJSON(map); |
|
52 |
// RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
53 |
// .post("/v2/user/register") |
|
54 |
// .accept(MediaType.APPLICATION_JSON).content(json) |
|
55 |
// .contentType(MediaType.APPLICATION_JSON); |
|
56 |
// |
|
57 |
// MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
58 |
// |
|
59 |
// MockHttpServletResponse response = result.getResponse(); |
|
60 |
// |
|
61 |
// assertEquals(HttpStatus.CREATED.value(), response.getStatus()); |
|
62 |
// } |
|
63 |
// @Test |
|
64 |
// public void registerUserInvalidArguments() throws Exception { |
|
65 |
// Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.INVALID_USER_ARGUMENTS); |
|
66 |
// HashMap<String,Object> map = new HashMap<>(); |
|
67 |
// map.put("name","pepa"); |
|
68 |
// map.put("password","ahojSvete"); |
|
69 |
// map.put("email","yxz"); |
|
70 |
// String json = JSONBuilder.buildJSON(map); |
|
71 |
// RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
72 |
// .post("/v2/user/register") |
|
73 |
// .accept(MediaType.APPLICATION_JSON).content(json) |
|
74 |
// .contentType(MediaType.APPLICATION_JSON); |
|
75 |
// |
|
76 |
// MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
77 |
// |
|
78 |
// MockHttpServletResponse response = result.getResponse(); |
|
79 |
// |
|
80 |
// assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); |
|
81 |
// } |
|
82 |
// |
|
83 |
// |
|
84 |
// |
|
85 |
// @Test |
|
86 |
// public void registerUserExists() throws Exception { |
|
87 |
// Mockito.when(userService.registerUser(any())).thenReturn(UserModelStatusCodes.USER_EXISTS); |
|
88 |
// HashMap<String,Object> map = new HashMap<>(); |
|
89 |
// map.put("name","pepa"); |
|
90 |
// map.put("password","ahojSvete"); |
|
91 |
// map.put("email","yxz@ahoj.cz"); |
|
92 |
// String json = JSONBuilder.buildJSON(map); |
|
93 |
// RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
94 |
// .post("/v2/user/register") |
|
95 |
// .accept(MediaType.APPLICATION_JSON).content(json) |
|
96 |
// .contentType(MediaType.APPLICATION_JSON); |
|
97 |
// |
|
98 |
// MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
99 |
// |
|
100 |
// MockHttpServletResponse response = result.getResponse(); |
|
101 |
// |
|
102 |
// assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); |
|
103 |
// } |
|
104 |
// |
|
105 |
// @Test |
|
106 |
// public void loginValidUser() throws Exception { |
|
107 |
// Mockito.when(userService.verifyUser(any())).thenReturn(UserModelStatusCodes.USER_LOGGED_IN); |
|
108 |
// HashMap<String,Object> map = new HashMap<>(); |
|
109 |
// map.put("name","pepa"); |
|
110 |
// map.put("password","ahojSvete"); |
|
111 |
// map.put("email","yxz@ahoj.cz"); |
|
112 |
// String json = JSONBuilder.buildJSON(map); |
|
113 |
// RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
114 |
// .post("/v2/user/login") |
|
115 |
// .accept(MediaType.APPLICATION_JSON).content(json) |
|
116 |
// .contentType(MediaType.APPLICATION_JSON); |
|
117 |
// |
|
118 |
// MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
119 |
// |
|
120 |
// MockHttpServletResponse response = result.getResponse(); |
|
121 |
// |
|
122 |
// assertEquals(HttpStatus.OK.value(), response.getStatus()); |
|
123 |
// } |
|
124 |
// |
|
125 |
// @Test |
|
126 |
// public void loginInValidUser() throws Exception { |
|
127 |
// Mockito.when(userService.verifyUser(any())).thenReturn(UserModelStatusCodes.USER_LOGIN_FAILED); |
|
128 |
// HashMap<String,Object> map = new HashMap<>(); |
|
129 |
// map.put("name","pepa"); |
|
130 |
// map.put("password","ahojSvete"); |
|
131 |
// map.put("email","yxz@ahoj.cz"); |
|
132 |
// String json = JSONBuilder.buildJSON(map); |
|
133 |
// RequestBuilder requestBuilder = MockMvcRequestBuilders |
|
134 |
// .post("/v2/user/login") |
|
135 |
// .accept(MediaType.APPLICATION_JSON).content(json) |
|
136 |
// .contentType(MediaType.APPLICATION_JSON); |
|
137 |
// |
|
138 |
// MvcResult result = mockMvc.perform(requestBuilder).andReturn(); |
|
139 |
// |
|
140 |
// MockHttpServletResponse response = result.getResponse(); |
|
141 |
// |
|
142 |
// assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatus()); |
|
143 |
// } |
|
144 |
// |
|
145 |
//} |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImplTest.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder; |
|
7 |
import org.junit.Test; |
|
8 |
import org.junit.runner.RunWith; |
|
9 |
import org.mockito.MockedStatic; |
|
10 |
import org.mockito.Mockito; |
|
11 |
import org.springframework.beans.factory.annotation.Autowired; |
|
12 |
import org.springframework.boot.test.context.SpringBootTest; |
|
13 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
import org.springframework.http.HttpStatus; |
|
15 |
import org.springframework.http.ResponseEntity; |
|
16 |
import org.springframework.test.context.junit4.SpringRunner; |
|
17 |
import java.util.HashMap; |
|
18 |
import java.util.Map; |
|
19 |
|
|
20 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
21 |
import static org.mockito.ArgumentMatchers.*; |
|
22 |
import static org.mockito.Mockito.mock; |
|
23 |
import static org.mockito.Mockito.when; |
|
24 |
|
|
25 |
@RunWith(SpringRunner.class) |
|
26 |
@SpringBootTest |
|
27 |
public class OAuthServiceImplTest { |
|
28 |
|
|
29 |
@Autowired |
|
30 |
private OAuthService oAuthService; |
|
31 |
|
|
32 |
/** |
|
33 |
* Mocked User |
|
34 |
*/ |
|
35 |
private final User mockUser = new User("foo", "foo@foo.cz", "foo"); |
|
36 |
private final String sampleToken = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyMGU4NmJiMC1lMDU4LTQwYTMtYjQyZC02ZTBjOWIyMmQ5MWQiL" + |
|
37 |
"CJzdWIiOiJmb28iLCJpYXQiOjE2ODE0MTY2ODAsImV4cCI6MTY4MTQxNjk4MH0.YJTwPEI4njQqYRuLGilf_oVl0gGD5BWFmdolk1O" + |
|
38 |
"vYWIgTcoIAFwh6bwqrW7XM6Hlj-ItYj9EmihYIqN5gfJVtg"; |
|
39 |
// valid user logged in - http response with code 200 and token in body is expected |
|
40 |
@Test |
|
41 |
public void loginValidUser(){ |
|
42 |
ResponseEntity<String> response = ResponseEntity.ok().body(sampleToken); |
|
43 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
44 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(response); |
|
45 |
ResponseEntity<String> response1 = oAuthService.loginUser(mockUser); |
|
46 |
|
|
47 |
assertEquals(response, response1); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
// return 200 response if valid user is trying to authenticate |
|
52 |
@Test |
|
53 |
public void authenticateValidUser(){ |
|
54 |
ResponseEntity<String> expectedResponse = ResponseEntity.ok().body(mockUser.getName()); |
|
55 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
56 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
57 |
ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
|
58 |
assertEquals(expectedResponse, response); |
|
59 |
} |
|
60 |
|
|
61 |
} |
|
62 |
|
|
63 |
// return 401 if unauthorized user is trying to authenticate |
|
64 |
@Test |
|
65 |
public void authenticateInvalidUser(){ |
|
66 |
ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockUser.getName()); |
|
67 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
68 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
69 |
ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
|
70 |
assertEquals(expectedResponse, response); |
|
71 |
} |
|
72 |
|
|
73 |
} |
|
74 |
@Test |
|
75 |
public void logoutUser(){ |
|
76 |
Map<String,Object> json = new HashMap<>(); |
|
77 |
json.put("message","ok"); |
|
78 |
//have to set token to the user - in this scenario we assume user is logged in |
|
79 |
mockUser.setToken(sampleToken); |
|
80 |
String jsonString = JSONBuilder.buildJSON(json); |
|
81 |
ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.OK).body(jsonString); |
|
82 |
try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
83 |
requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(expectedResponse); |
|
84 |
ResponseEntity<String> response = oAuthService.logoutUser(mockUser); |
|
85 |
assertEquals(expectedResponse, response); |
|
86 |
} |
|
87 |
|
|
88 |
|
|
89 |
} |
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
} |
|
1 |
//package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
// |
|
3 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
|
4 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
5 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
6 |
//import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder; |
|
7 |
//import org.junit.Test; |
|
8 |
//import org.junit.runner.RunWith; |
|
9 |
//import org.mockito.MockedStatic; |
|
10 |
//import org.mockito.Mockito; |
|
11 |
//import org.springframework.beans.factory.annotation.Autowired; |
|
12 |
//import org.springframework.boot.test.context.SpringBootTest; |
|
13 |
//import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
//import org.springframework.http.HttpStatus; |
|
15 |
//import org.springframework.http.ResponseEntity; |
|
16 |
//import org.springframework.test.context.junit4.SpringRunner; |
|
17 |
//import java.util.HashMap; |
|
18 |
//import java.util.Map; |
|
19 |
// |
|
20 |
//import static org.junit.jupiter.api.Assertions.assertEquals; |
|
21 |
//import static org.mockito.ArgumentMatchers.*; |
|
22 |
//import static org.mockito.Mockito.mock; |
|
23 |
//import static org.mockito.Mockito.when; |
|
24 |
// |
|
25 |
//@RunWith(SpringRunner.class) |
|
26 |
//@SpringBootTest |
|
27 |
//public class OAuthServiceImplTest { |
|
28 |
// |
|
29 |
// @Autowired |
|
30 |
// private OAuthService oAuthService; |
|
31 |
// |
|
32 |
// /** |
|
33 |
// * Mocked User |
|
34 |
// */ |
|
35 |
// private final User mockUser = new User("foo", "foo@foo.cz", "foo"); |
|
36 |
// private final String sampleToken = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyMGU4NmJiMC1lMDU4LTQwYTMtYjQyZC02ZTBjOWIyMmQ5MWQiL" + |
|
37 |
// "CJzdWIiOiJmb28iLCJpYXQiOjE2ODE0MTY2ODAsImV4cCI6MTY4MTQxNjk4MH0.YJTwPEI4njQqYRuLGilf_oVl0gGD5BWFmdolk1O" + |
|
38 |
// "vYWIgTcoIAFwh6bwqrW7XM6Hlj-ItYj9EmihYIqN5gfJVtg"; |
|
39 |
// // valid user logged in - http response with code 200 and token in body is expected |
|
40 |
// @Test |
|
41 |
// public void loginValidUser(){ |
|
42 |
// ResponseEntity<String> response = ResponseEntity.ok().body(sampleToken); |
|
43 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
44 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(response); |
|
45 |
// ResponseEntity<String> response1 = oAuthService.loginUser(mockUser); |
|
46 |
// |
|
47 |
// assertEquals(response, response1); |
|
48 |
// } |
|
49 |
// } |
|
50 |
// |
|
51 |
// // return 200 response if valid user is trying to authenticate |
|
52 |
// @Test |
|
53 |
// public void authenticateValidUser(){ |
|
54 |
// ResponseEntity<String> expectedResponse = ResponseEntity.ok().body(mockUser.getName()); |
|
55 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
56 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
57 |
// ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
|
58 |
// assertEquals(expectedResponse, response); |
|
59 |
// } |
|
60 |
// |
|
61 |
// } |
|
62 |
// |
|
63 |
// // return 401 if unauthorized user is trying to authenticate |
|
64 |
// @Test |
|
65 |
// public void authenticateInvalidUser(){ |
|
66 |
// ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockUser.getName()); |
|
67 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
68 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyString())).thenReturn(expectedResponse); |
|
69 |
// ResponseEntity<String> response = oAuthService.authenticate(sampleToken); |
|
70 |
// assertEquals(expectedResponse, response); |
|
71 |
// } |
|
72 |
// |
|
73 |
// } |
|
74 |
// @Test |
|
75 |
// public void logoutUser(){ |
|
76 |
// Map<String,Object> json = new HashMap<>(); |
|
77 |
// json.put("message","ok"); |
|
78 |
// //have to set token to the user - in this scenario we assume user is logged in |
|
79 |
// mockUser.setToken(sampleToken); |
|
80 |
// String jsonString = JSONBuilder.buildJSON(json); |
|
81 |
// ResponseEntity<String> expectedResponse = ResponseEntity.status(HttpStatus.OK).body(jsonString); |
|
82 |
// try(MockedStatic<RequestBuilder> requestBuilderMockedStatic = Mockito.mockStatic(RequestBuilder.class)){ |
|
83 |
// requestBuilderMockedStatic.when(() -> RequestBuilder.sendRequestResponse(anyString(), anyMap())).thenReturn(expectedResponse); |
|
84 |
// ResponseEntity<String> response = oAuthService.logoutUser(mockUser); |
|
85 |
// assertEquals(expectedResponse, response); |
|
86 |
// } |
|
87 |
// |
|
88 |
// |
|
89 |
// } |
|
90 |
// |
|
91 |
// |
|
92 |
// |
|
93 |
// |
|
94 |
// |
|
95 |
// |
|
96 |
// |
|
97 |
// |
|
98 |
// |
|
99 |
// |
|
100 |
//} |
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpTest.java | ||
---|---|---|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
5 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository; |
6 |
import org.junit.Test; |
|
7 |
import org.junit.runner.RunWith; |
|
6 |
import org.junit.jupiter.api.Test; |
|
8 | 7 |
import org.springframework.beans.factory.annotation.Autowired; |
9 | 8 |
import org.springframework.boot.test.context.SpringBootTest; |
10 | 9 |
import org.springframework.boot.test.mock.mockito.MockBean; |
11 |
import org.springframework.test.context.junit4.SpringRunner; |
|
12 | 10 |
|
13 | 11 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
14 | 12 |
import static org.mockito.ArgumentMatchers.any; |
15 |
import static org.mockito.BDDMockito.given; |
|
16 | 13 |
import static org.mockito.Mockito.when; |
17 | 14 |
|
18 | 15 |
/** |
19 | 16 |
* Tests for userServiceImplementation |
20 | 17 |
*/ |
21 |
@RunWith(SpringRunner.class) |
|
22 | 18 |
@SpringBootTest |
23 | 19 |
public class UserServiceImpTest { |
24 | 20 |
|
Také k dispozici: Unified diff
#10493 spravení konraktových testů