forked from opentiny/tiny-engine-backend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI18nEntryControllerTest.java
More file actions
160 lines (136 loc) · 6.13 KB
/
I18nEntryControllerTest.java
File metadata and controls
160 lines (136 loc) · 6.13 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
package com.tinyengine.it.controller;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.when;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.dto.DeleteI18nEntry;
import com.tinyengine.it.model.dto.FileResult;
import com.tinyengine.it.model.dto.I18nEntryDto;
import com.tinyengine.it.model.dto.I18nEntryListResult;
import com.tinyengine.it.model.dto.OperateI18nBatchEntries;
import com.tinyengine.it.model.dto.OperateI18nEntries;
import com.tinyengine.it.model.entity.I18nEntry;
import com.tinyengine.it.service.app.I18nEntryService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* test case
*
* @since 2024-10-29
*/
class I18nEntryControllerTest {
@Mock
private I18nEntryService i18nEntryService;
@InjectMocks
private I18nEntryController i18nEntryController;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testGetI18nEntriesByApp() {
I18nEntryListResult mockData = new I18nEntryListResult();
when(i18nEntryService.findI18nEntryByApp(anyInt(), anyString())).thenReturn(mockData);
Integer host = 1;
String hostType = "app";
Result<I18nEntryListResult> result = i18nEntryController.getI18nEntriesByApp(host, hostType);
Assertions.assertEquals(mockData, result.getData());
}
@Test
void testGetI18nEntriesById() {
I18nEntryDto i18nEntry = new I18nEntryDto();
when(i18nEntryService.findI18nEntryById(1)).thenReturn(i18nEntry);
Result<I18nEntryDto> result = i18nEntryController.getI18nEntriesById(1);
Assertions.assertEquals(i18nEntry, result.getData());
}
@Test
void testCreateI18nEntries() {
I18nEntry i18nEntry = new I18nEntry();
List<I18nEntry> mockData = Arrays.<I18nEntry>asList(i18nEntry);
when(i18nEntryService.create(any(OperateI18nEntries.class))).thenReturn(mockData);
Result<List<I18nEntry>> result = i18nEntryController.createI18nEntries(new OperateI18nEntries());
Assertions.assertEquals(i18nEntry, result.getData().get(0));
}
@Test
void testBatchCreateEntries() {
I18nEntry i18nEntry = new I18nEntry();
when(i18nEntryService.bulkCreate(any(OperateI18nBatchEntries.class))).thenReturn(Arrays.asList(i18nEntry));
Result<List<I18nEntry>> result = i18nEntryController.batchCreateEntries(new OperateI18nBatchEntries());
Assertions.assertEquals(i18nEntry, result.getData().get(0));
}
@Test
void testUpdateI18nEntries() {
I18nEntryDto i18nEntry = new I18nEntryDto();
when(i18nEntryService.findI18nEntryById(1)).thenReturn(i18nEntry);
when(i18nEntryService.updateI18nEntryById(any(I18nEntry.class))).thenReturn(1);
Result<I18nEntryDto> result = i18nEntryController.updateI18nEntries(1, new I18nEntry());
Assertions.assertEquals(i18nEntry, result.getData());
}
@Test
void testUpdateEntry() {
I18nEntry i18nEntry = new I18nEntry();
List<I18nEntry> mockData = Arrays.<I18nEntry>asList(i18nEntry);
when(i18nEntryService.bulkUpdate(any(OperateI18nEntries.class))).thenReturn(mockData);
Result<List<I18nEntry>> result = i18nEntryController.updateEntry(new OperateI18nEntries());
Assertions.assertEquals(i18nEntry, result.getData().get(0));
}
@Test
void testDeleteI18nEntries() {
List<I18nEntryDto> i18nEntryDtos = Arrays.<I18nEntryDto>asList(new I18nEntryDto());
when(i18nEntryService.deleteI18nEntriesByHostAndHostTypeAndKey(any(DeleteI18nEntry.class)))
.thenReturn(i18nEntryDtos);
Result<List<I18nEntryDto>> result = i18nEntryController.deleteI18nEntries(new DeleteI18nEntry());
Assertions.assertEquals(i18nEntryDtos, result.getData());
}
@Test
void testUpdateI18nSingleFile() throws Exception {
Result<FileResult> mockData = new Result<>();
mockData.setSuccess(true);
when(i18nEntryService.readSingleFileAndBulkCreate(any(MultipartFile.class), anyInt()))
.thenReturn(mockData);
MultipartFile file = Mockito.mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn("example.json");
when(file.isEmpty()).thenReturn(false);
HashMap<String, MultipartFile> filesMap = new HashMap<String, MultipartFile>() {{
put("filesMap", file);
}};
Result<FileResult> result = i18nEntryController.updateI18nSingleFile(1, filesMap);
Assertions.assertTrue(result.isSuccess());
Assertions.assertEquals(mockData, result);
}
@Test
void testUpdateI18nMultiFile() throws Exception {
when(i18nEntryService.readFilesAndbulkCreate(anyString(), any(MultipartFile.class), anyInt()))
.thenReturn(new Result<FileResult>());
MultipartFile file = Mockito.mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn("example.json");
when(file.isEmpty()).thenReturn(false);
HashMap<String, MultipartFile> filesMap = new HashMap<String, MultipartFile>() {{
put("filesMap", file);
}};
Result<FileResult> result = i18nEntryController.updateI18nMultiFile(1, filesMap);
Assertions.assertEquals(new Result<Map<String, Object>>(), result);
}
}