c074bcffcf84823d2e05db5d3603c6e61b47c658
[WebAppTest/.git] /
1 package com.prueba.core.context.integration.liquibase.impl;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import javax.sql.DataSource;
11
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import com.prueba.core.context.integration.database.DataBaseAccess;
16 import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl;
17 import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
18
19 public class LiquibaseContextIntegrationTest {
20         private static final String CODE = "CODE";
21         private static final String NAME = "NAME";
22         private static final String SURNAME = "SURNAME";
23         private static final String PASSWORD = "PASSWORD";
24         private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE";
25         
26         private DataSource dataSource;
27         private LiquibaseContext liquibaseContext;
28         
29         @Before
30         public void init() {
31                 dataSource = DoDataSourceContext.getInstance().getDataSource();
32                 liquibaseContext = new LiquibaseContext(dataSource);
33                 liquibaseContext.init();
34         }
35         
36         @Test
37         public void whenLoadLiquibaseContextThenReturnResultsFromDatabase() {
38                 final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
39                 final List<Map<String, String>> dataResult = dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT",
40                                 answer ->
41                 {
42                         final List<Map<String, String>> result = new ArrayList<>();
43                         while (answer.next()) {
44                                 final Map<String, String> row = new HashMap<>();
45                                 row.put(CODE, answer.getString(CODE));
46                                 row.put(NAME, answer.getString(NAME));
47                                 row.put(SURNAME, answer.getString(SURNAME));
48                                 row.put(PASSWORD, answer.getString(PASSWORD));
49                                 row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE));
50                                 result.add(row);
51                         }
52               
53                         return result;
54                 },
55                 preparedStatement -> {
56                         
57                 });
58                 
59                 final String expectedCode = "GUMARTIN";
60                 final String expectedName = "Gustavo";
61                 final String expectedSurname = "Martin Morcuende";
62                 final String expectedPassword = "lame";
63                 final String expectedAppRoleCode = "ROLE_APP_ADMIN";
64                 
65                 final Map<String, String> row = dataResult.get(0);
66                 assertNotNull(row);
67                 assertEquals(expectedCode, row.get(CODE));
68                 assertEquals(expectedName, row.get(NAME));
69                 assertEquals(expectedSurname, row.get(SURNAME));
70                 assertEquals(expectedPassword, row.get(PASSWORD));
71                 assertEquals(expectedAppRoleCode, row.get(APP_ROLE_CODE));
72         }
73         
74         @Test
75         public void whenLoadLiquibaseContextThenReturnResultsFromDatabaseUsingParameters() {
76                 final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
77                 final List<Map<String, String>> dataResult = dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?",
78                                 answer ->
79                 {
80                         final List<Map<String, String>> result = new ArrayList<>();
81                         while (answer.next()) {
82                                 final Map<String, String> row = new HashMap<>();
83                                 row.put(CODE, answer.getString(CODE));
84                                 row.put(NAME, answer.getString(NAME));
85                                 row.put(SURNAME, answer.getString(SURNAME));
86                                 row.put(PASSWORD, answer.getString(PASSWORD));
87                                 row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE));
88                                 result.add(row);
89                         }
90               
91                         return result;
92                 },
93                 preparedStatement -> {
94                         preparedStatement.setString(1, "GUMARTIN");
95                 });
96                 
97                 final String expectedCode = "GUMARTIN";
98                 final String expectedName = "Gustavo";
99                 final String expectedSurname = "Martin Morcuende";
100                 final String expectedPassword = "lame";
101                 final String expectedAppRoleCode = "ROLE_APP_ADMIN";
102                 
103                 final Map<String, String> row = dataResult.get(0);
104                 assertNotNull(row);
105                 assertEquals(expectedCode, row.get(CODE));
106                 assertEquals(expectedName, row.get(NAME));
107                 assertEquals(expectedSurname, row.get(SURNAME));
108                 assertEquals(expectedPassword, row.get(PASSWORD));
109                 assertEquals(expectedAppRoleCode, row.get(APP_ROLE_CODE));
110         }
111
112         @Test
113         public void whenLoadLiquibaseContextAndNotUserFoundThenDoNotReturResultsFromDatabase() {        
114                 final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
115                 final List<Map<String, String>> dataResult = dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?",
116                                 answer ->
117                 {
118                         final List<Map<String, String>> result = new ArrayList<>();
119                         while (answer.next()) {
120                                 final Map<String, String> row = new HashMap<>();
121                                 row.put(CODE, answer.getString(CODE));
122                                 row.put(NAME, answer.getString(NAME));
123                                 row.put(SURNAME, answer.getString(SURNAME));
124                                 row.put(PASSWORD, answer.getString(PASSWORD));
125                                 row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE));
126                                 result.add(row);
127                         }
128               
129                         return result;
130                 },
131                 preparedStatement -> {
132                         preparedStatement.setString(1, "DONOTEXIST");
133                 });
134                 
135
136                 assertTrue(dataResult.isEmpty());
137         }
138
139 }