Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 490e0a67

Přidáno uživatelem Jakub Danek před více než 5 roky(ů)

re #41 remove unused mock classes

Zobrazit rozdíly:

server/src/main/java/org/danekja/ymanager/business/mock/FileServiceMock.java
1
package org.danekja.ymanager.business.mock;
2

  
3
import be.quodlibet.boxable.BaseTable;
4
import be.quodlibet.boxable.Cell;
5
import be.quodlibet.boxable.Row;
6
import be.quodlibet.boxable.datatable.DataTable;
7
import org.danekja.ymanager.business.FileExportResult;
8
import org.danekja.ymanager.business.FileImportResult;
9
import org.danekja.ymanager.business.FileService;
10
import org.danekja.ymanager.ws.rest.RESTFullException;
11
import org.apache.pdfbox.pdmodel.PDDocument;
12
import org.apache.pdfbox.pdmodel.PDPage;
13
import org.apache.pdfbox.pdmodel.PDPageContentStream;
14
import org.apache.pdfbox.pdmodel.common.PDRectangle;
15
import org.apache.pdfbox.pdmodel.font.PDType1Font;
16
import org.apache.poi.xssf.usermodel.XSSFCell;
17
import org.apache.poi.xssf.usermodel.XSSFRow;
18
import org.apache.poi.xssf.usermodel.XSSFSheet;
19
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
20

  
21
import java.io.ByteArrayInputStream;
22
import java.io.ByteArrayOutputStream;
23
import java.io.IOException;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.Iterator;
27
import java.util.List;
28

  
29

  
30
public class FileServiceMock implements FileService {
31

  
32
    private List<String> content = new ArrayList<>();
33

  
34
    @Override
35
    public FileImportResult parseXLSFile(String fileName, byte[] bytes) throws RESTFullException {
36
        try {
37

  
38
            XSSFWorkbook wb = new XSSFWorkbook(new ByteArrayInputStream(bytes));
39

  
40
            XSSFSheet sheet=wb.getSheetAt(0);
41
            XSSFRow row;
42
            XSSFCell cell;
43

  
44
            Iterator rows = sheet.rowIterator();
45

  
46
            while (rows.hasNext()) {
47
                row=(XSSFRow) rows.next();
48
                Iterator cells = row.cellIterator();
49

  
50
                while (cells.hasNext()) {
51
                    cell = (XSSFCell) cells.next();
52

  
53
                    content.add(cell.getRawValue());
54
                }
55
            }
56
            return new FileImportResult(fileName, (long) bytes.length);
57
        } catch (IOException e) {
58
            throw new RESTFullException("error", "");
59
        }
60
    }
61

  
62
    @Override
63
    public FileExportResult createPDFFile() throws RESTFullException {
64

  
65
        PDDocument document = new PDDocument();
66
        PDPage page1 = new PDPage(PDRectangle.A4);
67
        PDRectangle rect = page1.getMediaBox();
68
        document.addPage(page1);
69

  
70
        try {
71
            PDPageContentStream cos = new PDPageContentStream(document, page1);
72

  
73
            int line = 0;
74

  
75
            for (String word : content) {
76
                cos.beginText();
77
                cos.setFont(PDType1Font.HELVETICA, 12);
78
                cos.newLineAtOffset(100, rect.getHeight() - 50*(++line));
79
                cos.showText(word);
80
                cos.endText();
81
            }
82

  
83
            cos.close();
84

  
85
            ByteArrayOutputStream output = new ByteArrayOutputStream();
86

  
87
            document.save(output);
88
            document.close();
89

  
90
            return new FileExportResult("export.pdf", pdfTable());
91

  
92
        } catch (IOException ioex) {
93
            throw new RESTFullException("", "");
94
        }
95
    }
96

  
97
    private byte[] pdfTable() throws IOException {
98
        PDPage myPage = new PDPage(PDRectangle.A4);
99
        PDDocument mainDocument = new PDDocument();
100
        PDPageContentStream contentStream = new PDPageContentStream(mainDocument, myPage);
101

  
102

  
103

  
104
        //Dummy Table
105
        float margin = 50;
106
        // starting y position is whole page height subtracted by top and bottom margin
107
        float yStartNewPage = myPage.getMediaBox().getHeight() - (2 * margin);
108
        // we want table across whole page width (subtracted by left and right margin ofcourse)
109
        float tableWidth = myPage.getMediaBox().getWidth() - (2 * margin);
110

  
111
        boolean drawContent = true;
112
        float yStart = yStartNewPage;
113
        float bottomMargin = 70;
114
        // y position is your coordinate of top left corner of the table
115
        float yPosition = 550;
116

  
117
        List<List> data = new ArrayList();
118
        data.add(new ArrayList<>(
119
                Arrays.asList("Column One", "Column Two", "Column Three", "Column Four", "Column Five")));
120
        for (int i = 1; i <= 100; i++) {
121
            data.add(new ArrayList<>(
122
                    Arrays.asList("Row " + i + " Col One", "Row " + i + " Col Two", "Row " + i + " Col Three", "Row " + i + " Col Four", "Row " + i + " Col Five")));
123
        }
124
        BaseTable dataTable = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, mainDocument, myPage, true, true);
125
        DataTable t = new DataTable(dataTable, myPage);
126
        t.addListToTable(data, DataTable.HASHEADER);
127
        dataTable.draw();
128

  
129
        contentStream.close();
130
        mainDocument.addPage(myPage);
131

  
132
        ByteArrayOutputStream output = new ByteArrayOutputStream();
133
        mainDocument.save(output);
134

  
135
        mainDocument.close();
136
        contentStream.close();
137

  
138
        return output.toByteArray();
139
    }
140

  
141
    private byte[] pdfSimple() throws IOException {
142

  
143
        PDPage myPage = new PDPage(PDRectangle.A4);
144
        PDDocument mainDocument = new PDDocument();
145
        PDPageContentStream contentStream = new PDPageContentStream(mainDocument, myPage);
146

  
147

  
148

  
149
        //Dummy Table
150
        float margin = 50;
151
        // starting y position is whole page height subtracted by top and bottom margin
152
        float yStartNewPage = myPage.getMediaBox().getHeight() - (2 * margin);
153
        // we want table across whole page width (subtracted by left and right margin ofcourse)
154
        float tableWidth = myPage.getMediaBox().getWidth() - (2 * margin);
155

  
156
        boolean drawContent = true;
157
        float yStart = yStartNewPage;
158
        float bottomMargin = 70;
159
        // y position is your coordinate of top left corner of the table
160
        float yPosition = 550;
161

  
162
        BaseTable table = new BaseTable(yPosition, yStartNewPage, bottomMargin, tableWidth, margin, mainDocument, myPage, true, drawContent);
163

  
164

  
165
        Row<PDPage> headerRow = table.createRow(15f);
166
        Cell<PDPage> cell = headerRow.createCell(100, "Header");
167
        table.addHeaderRow(headerRow);
168

  
169

  
170
        Row<PDPage> row = table.createRow(12);
171
        cell = row.createCell(30, "Data 1");
172
        cell = row.createCell(70, "Some value");
173

  
174
        table.draw();
175

  
176

  
177
        contentStream.close();
178
        mainDocument.addPage(myPage);
179

  
180
        ByteArrayOutputStream output = new ByteArrayOutputStream();
181
        mainDocument.save(output);
182

  
183
        mainDocument.close();
184
        contentStream.close();
185

  
186
        return output.toByteArray();
187
    }
188

  
189

  
190
}
server/src/main/java/org/danekja/ymanager/business/mock/ManagerMock.java
1
package org.danekja.ymanager.business.mock;
2

  
3
import org.danekja.ymanager.business.Manager;
4
import org.danekja.ymanager.domain.RequestType;
5
import org.danekja.ymanager.domain.Status;
6
import org.danekja.ymanager.domain.UserRole;
7
import org.danekja.ymanager.domain.VacationType;
8
import org.danekja.ymanager.dto.*;
9
import org.danekja.ymanager.ws.rest.RESTFullException;
10

  
11
import java.time.LocalDate;
12
import java.time.LocalDateTime;
13
import java.time.LocalTime;
14
import java.time.Period;
15
import java.util.*;
16
import java.util.function.Predicate;
17
import java.util.stream.Collectors;
18

  
19
import static java.util.Arrays.asList;
20

  
21
public class ManagerMock implements Manager {
22

  
23
    private List<DefaultSettings> settings = new LinkedList<>();
24

  
25
    private Map<Long, List<VacationDay>> vacations = new HashMap<>();
26

  
27
    private Map<Long, UserSettings> userSettings = new HashMap<>();
28

  
29
    private BasicProfileUser createBasicProfileUser(long id, Status status) {
30
        BasicProfileUser user = new BasicProfileUser();
31

  
32
        user.setId(id);
33
        user.setFirstName("Tomas");
34
        user.setLastName(status == null ? "unknown" : status.name().toLowerCase());
35
        user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg");
36
        user.setCalendar(asList(
37
                createVacationDay(1L, LocalDate.now(), VacationType.VACATION, Status.ACCEPTED),
38
                createVacationDay(2L, LocalDate.now().plusDays(1), VacationType.VACATION, Status.ACCEPTED))
39
        );
40

  
41
        return user;
42
    }
43

  
44
    private VacationDay createVacationDay(long id, LocalDate date, VacationType vacationType, Status status) {
45
        VacationDay vacationDay = new VacationDay();
46

  
47
        vacationDay.setId(id);
48
        vacationDay.setType(vacationType);
49
        vacationDay.setDate(date);
50
        vacationDay.setStatus(status);
51

  
52
        switch (vacationType) {
53
            case SICK_DAY: {
54
                vacationDay.setFrom(null);
55
                vacationDay.setTo(null);
56
            } break;
57
            case VACATION: {
58
                vacationDay.setFrom(LocalTime.of(9, 00));
59
                vacationDay.setTo(LocalTime.of(13, 00));
60
            } break;
61
        }
62

  
63
        return vacationDay;
64
    }
65

  
66
    private VacationRequest createVacationRequest(long id, VacationType vacationType, Status status) {
67
        VacationRequest request = new VacationRequest();
68

  
69
        request.setId(id);
70
        request.setFirstName("Tomas");
71
        request.setLastName("Novak");
72
        request.setDate(LocalDate.now());
73
        request.setType(vacationType);
74
        request.setStatus(status);
75
        request.setTimestamp(LocalDateTime.now());
76

  
77
        switch (vacationType) {
78
            case SICK_DAY: {
79
                request.setFrom(null);
80
                request.setTo(null);
81
            } break;
82
            case VACATION: {
83
                request.setFrom(LocalTime.of(9, 30));
84
                request.setTo(LocalTime.of(18, 30));
85
            } break;
86
        }
87

  
88
        return request;
89
    }
90

  
91
    private AuthorizationRequest createAuthorizationRequest(long id, Status status) {
92
        AuthorizationRequest request = new AuthorizationRequest();
93

  
94
        request.setId(id);
95
        request.setFirstName("Tomas");
96
        request.setLastName("Novak");
97
        request.setStatus(status);
98
        request.setTimestamp(LocalDateTime.now());
99

  
100
        return request;
101
    }
102

  
103
    private FullUserProfile createFullUserProfile(Long id) {
104
        FullUserProfile user = new FullUserProfile();
105

  
106
        user.setId(id);
107
        user.setFirstName("Tomas");
108
        user.setLastName("Novak");
109
        user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg");
110
        user.setStatus(Status.ACCEPTED);
111

  
112
        if (userSettings.containsKey(id)) {
113
            UserSettings settings = userSettings.get(id);
114
            user.setVacationCount(settings.getVacationCount());
115
            user.setSickDayCount(settings.getSickDayCount());
116
            user.setRole(settings.getRole());
117
        } else {
118
            user.setVacationCount(8.5F);
119
            user.setSickDayCount(3);
120
            user.setRole(UserRole.EMPLOYER);
121
        }
122

  
123
        user.setNotification(LocalDateTime.of(2000, 12, 1, 9, 0));
124

  
125
        return user;
126
    }
127

  
128
    private DefaultSettings createDefaultSettings() {
129
        DefaultSettings settings = new DefaultSettings();
130

  
131
        settings.setSickDayCount(3);
132
        settings.setNotification(LocalDateTime.now());
133

  
134
        return settings;
135
    }
136

  
137
    @Override
138
    public List<BasicProfileUser> getUsers(Status status) throws RESTFullException {
139
        int count = 5;
140
        List<BasicProfileUser> users = new ArrayList<>(count);
141
        for (int i = 1; i <= count; i++) {
142
            users.add(createBasicProfileUser(i, status));
143
        }
144
        return users;
145
    }
146

  
147
    @Override
148
    public List<VacationRequest> getVacationRequests(Status status) throws RESTFullException {
149
        int count = 5;
150
        List<VacationRequest> requests = new ArrayList<>(count);
151
        int length = VacationType.values().length;
152
        for (int i = 1; i <= count; i++) {
153
            requests.add(createVacationRequest(i, VacationType.values()[i % length], status));
154
        }
155
        return requests;
156
    }
157

  
158
    @Override
159
    public List<AuthorizationRequest> getAuthorizationRequests(Status status) {
160
        int count = 5;
161
        List<AuthorizationRequest> requests = new ArrayList<>(count);
162
        for (int i = 1; i <= count; i++) {
163
            requests.add(createAuthorizationRequest(i, status));
164
        }
165
        return requests;
166
    }
167

  
168
    @Override
169
    public FullUserProfile getUserProfile(Long userId) {
170
        return createFullUserProfile(userId);
171
    }
172

  
173
    @Override
174
    public DefaultSettings getDefaultSettings() {
175
        return settings.isEmpty() ? createDefaultSettings() : settings.get(settings.size()-1);
176
    }
177

  
178
    @Override
179
    public List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) {
180
        if (vacations.containsKey(userId)) {
181
            List<VacationDay> vacationDays = vacations.get(userId);
182
            Predicate<VacationDay> predicate = (day) -> day.getDate().isAfter(fromDate);
183
            predicate = predicate.or((day) -> day.getDate().isEqual(fromDate));
184
            if (toDate != null) {
185
                predicate = predicate.and((day) -> day.getDate().isBefore(toDate)).or((day) -> day.getDate().isEqual(toDate));
186
            }
187
            return vacationDays.stream().filter(predicate).collect(Collectors.toList());
188

  
189
        } else {
190
            int daysDifference = toDate == null ? 5 : Period.between(fromDate, toDate).getDays();
191
            List<VacationDay> days = new ArrayList<>(daysDifference);
192
            for (int i = 0; i < daysDifference; i++) {
193
                days.add(createVacationDay(i + 1, fromDate.plusDays(i), VacationType.values()[i % 2], status));
194
            }
195
            return days;
196
        }
197
    }
198

  
199
    @Override
200
    public void createSettings(DefaultSettings sett) {
201
        settings.add(sett);
202
    }
203

  
204
    @Override
205
    public void createVacation(Long userId, VacationDay vacationDay) {
206
        if (!vacations.containsKey(userId)) {
207
            vacations.put(userId, new ArrayList<>());
208
        }
209

  
210
        List<VacationDay> days = vacations.get(userId);
211
        days.add(vacationDay);
212
        vacationDay.setId((long)days.size());
213
    }
214

  
215
    @Override
216
    public void changeSettings(Long userId, UserSettings settings) {
217

  
218
        if (userSettings.containsKey(userId)) {
219
            if (settings.getId().equals(userId)) {
220
                userSettings.get(userId).setNotification(settings.getNotification());
221
            }
222
        } else {
223
            userSettings.put(userId, settings);
224
        }
225
    }
226

  
227
    @Override
228
    public void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException {
229
        if (vacationDay.getId() == null) {
230
            throw new RESTFullException("Vacation does not contain ID.", "rest.exception.generic");
231
        }
232

  
233
        if (!vacations.containsKey(userId)) {
234
            throw new RESTFullException("User does not exist.", "rest.exception.generic");
235
        }
236

  
237
        List<VacationDay> vacs = vacations.get(userId);
238
        boolean founded = false;
239
        for (int i = 0; i < vacs.size(); i++) {
240
            if (vacs.get(i).getId() == vacationDay.getId()) {
241
                VacationDay day = vacs.get(i);
242
                day.setStatus(vacationDay.getStatus());
243
                day.setFrom(vacationDay.getFrom());
244
                day.setTo(vacationDay.getTo());
245
                day.setDate(vacationDay.getDate());
246
                day.setType(vacationDay.getType());
247
                founded = true;
248
                break;
249
            }
250
        }
251

  
252
        if (!founded) {
253
            throw new RESTFullException("Vacation does not exist.", "rest.exception.generic");
254
        }
255
    }
256

  
257
    @Override
258
    public void changeRequest(RequestType type, BasicRequest request) throws RESTFullException {
259

  
260
        if (request.getId() == null) {
261
            throw new RESTFullException("ID of request can not be empty.", "rest.exception.generic");
262
        }
263

  
264
        switch (type) {
265
            case VACATION: {
266
                boolean found = false;
267
                for (List<VacationDay> value : vacations.values()) {
268
                    for (VacationDay day : value) {
269
                        if (day.getId() == request.getId()) {
270
                            found = true;
271
                            day.setStatus(request.getStatus());
272
                            break;
273
                        }
274
                    }
275
                    if (found) break;
276
                }
277
                if (!found) {
278
                    throw new RESTFullException("Vacation does not exist.", "rest.exception.generic");
279
                }
280
            } break;
281
            case AUTHORIZATION: {
282
                throw new RESTFullException("User does not exist.", "rest.exception.generic");
283
            }
284
        }
285
    }
286

  
287
    @Override
288
    public void deleteVacation(Long userId, Long vacationId) throws RESTFullException{
289
        if (!vacations.containsKey(userId)) {
290
            throw new RESTFullException("User does not exist.", "rest.exception.generic");
291
        }
292

  
293
        List<VacationDay> vacs = vacations.get(userId);
294
        if (!vacs.removeIf(day -> day.getId() == vacationId)) {
295
            throw new RESTFullException("Vacation does not exist for the user.", "rest.exception.generic");
296
        }
297
    }
298
}

Také k dispozici: Unified diff