写在前面
本次主要是补充了上面两次的单元测试代码,以保障组件功能的正常运行
代码
注释已经比较清楚了,不在此赘述
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
| @Document(indexName = "t1",type = "t2", shards = 2, replicas = 1) @Data @AllArgsConstructor @NoArgsConstructor @Builder @ToString public class IiapTemplateMatchDeviceDoc extends BaseDoc { @Id private String id; private String subjectModel; private String subjectName; private String templatePositionId; private String templatePositionName; private String templateGroupId; private String projectId; private String schemaId; private String templateDeviceId; private String templateDeviceSubjectId; private String manualMatchId; private String matchSpaceSubjectId; private String matchSpaceSubjectName; private Integer matchStatus; private String categoryName; private String categoryValues; }
import lombok.Data;
@Data public class IiapTemplateMatchDeviceDto extends BaseDto { private String subjectModel; private String subjectName; private String templatePositionId; private String templatePositionName; private String projectId; private String schemaId; private String templateDeviceId; private String templateDeviceSubjectId; private String manualMatchId; private String matchSpaceSubjectId; private String matchSpaceSubjectName; private Integer matchStatus; private String categoryName; private String categoryValues; }
public interface IiapImplTemplateMatchDeviceRepository extends ElasticsearchRepository<IiapTemplateMatchDeviceDoc, String> { IiapTemplateMatchDeviceDoc findByManualMatchIdAndTemplateDeviceSubjectId(String manualMatchId, String templateDeviceSubjectId); }
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.stereotype.Service;
@Slf4j @Service("iiapDocMatchDeviceService") public class IiapDocMatchDeviceServiceImpl extends EsAbstractDocService<IiapImplTemplateMatchDeviceRepository,IiapTemplateMatchDeviceDoc> {
@Override protected void prepareSave(IiapTemplateMatchDeviceDoc iiapTemplateMatchDeviceDoc) {
}
@Override public <E extends BaseDto> void buildOwnQuery(QueryBuilder qbs, E s) { IiapTemplateMatchDeviceDto dto=(IiapTemplateMatchDeviceDto)s; if(StringUtils.isNotBlank(dto.getSubjectName())){ ((BoolQueryBuilder)qbs).must(QueryBuilders.matchPhraseQuery("subjectName", dto.getSubjectName())); } } }
import lombok.extern.slf4j.Slf4j; import org.elasticsearch.index.query.QueryBuilder; import org.springframework.stereotype.Service;
@Slf4j @Service public class IiapTemplateMatchDeviceService extends EsAbstractTemplateService<IiapTemplateMatchDeviceDoc> {
@Override protected void prepareSave(IiapTemplateMatchDeviceDoc s) {
}
@Override public <E extends BaseDto> void buildOwnQuery(QueryBuilder qbs, E s) {
}
@Override protected int getBatchSize(){ return 1; } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:/es-test.xml") public class SpringBaseTest { protected static final Logger log = LoggerFactory.getLogger(SpringBaseTest.class);
@Autowired private IiapImplTemplateMatchDeviceRepository iiapImplTemplateMatchDeviceRepository;
@BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("==============单元测试开始============="); }
@AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("==============单元测试结束============="); }
@Test public void baseTest(){ System.out.println("success"); } }
import javax.annotation.Resource; import java.util.List; import java.util.Map;
public class EsDocServiceTest extends SpringBaseTest{
@Resource(name="iiapDocMatchDeviceService") private EsAbstractDocService iiapDocMatchDeviceService;
@Test public void saveOrUpdateTest(){ IiapTemplateMatchDeviceDoc doc=new IiapTemplateMatchDeviceDoc(); doc.setManualMatchId("s.123456"); doc.setCategoryName("网关"); doc.setCategoryValues("1"); doc.setMatchSpaceSubjectId("sp.26"); doc.setMatchSpaceSubjectName("门窗传感器"); doc.setSchemaId("sc.123456789"); doc.setProjectId("pj.123456789"); doc.setCreatedBy("test"); doc.setCreatedTime(System.currentTimeMillis()); doc.setSubjectName("docTest-"+System.currentTimeMillis()); doc.setDeleted(BaseConstant.FILED_NO_DELETED); iiapDocMatchDeviceService.saveOrUpdate(doc); String id=doc.getId(); Assert.assertNotNull(id);
doc.setSubjectName("docTest-change-"+System.currentTimeMillis()); iiapDocMatchDeviceService.saveOrUpdate(doc); String nid=doc.getId(); Assert.assertNotNull(nid); Assert.assertTrue(id.equals(nid)); }
@Test public void searchTest(){ saveOrUpdateTest(); IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSubjectName("docTest"); Iterable result= iiapDocMatchDeviceService.search(dto); Assert.assertNotNull(result); Assert.assertTrue(!IterableUtils.isEmpty(result)); }
@Test public void searchByPageTest(){ saveOrUpdateTest(); IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSubjectName("docTest"); dto.setStartIndex(0); dto.setSize(1); Map<String,Object> result= iiapDocMatchDeviceService.searchByPage(dto); Object obj=result.get("data"); Assert.assertNotNull(obj); Assert.assertTrue(!CollectionUtils.isEmpty((List)obj)); }
@Test public void deleteTest(){ IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSubjectName("docTest"); Iterable<IiapTemplateMatchDeviceDoc> result= iiapDocMatchDeviceService.search(dto); Assert.assertNotNull(result); Assert.assertTrue(!IterableUtils.isEmpty(result)); result.forEach(e->{ iiapDocMatchDeviceService.delete(e.getId()); }); result= iiapDocMatchDeviceService.search(dto); Assert.assertTrue(IterableUtils.isEmpty(result)); }
}
public class EsTemplateServiceTest extends SpringBaseTest{
@Resource(name="iiapTemplateMatchDeviceService") private EsAbstractTemplateService iiapTemplateMatchDeviceService;
@Test public void saveOrUpdateTest(){ for(int i=0;i<10;i++){ IiapTemplateMatchDeviceDoc doc=new IiapTemplateMatchDeviceDoc(); doc.setManualMatchId("s.123456"); doc.setCategoryName("网关"); doc.setCategoryValues("1"); doc.setMatchSpaceSubjectId("sp.26"); doc.setMatchSpaceSubjectName("门窗传感器"); doc.setSchemaId("sc.123456789"); doc.setProjectId("pj.123456789"); doc.setCreatedBy("test"); doc.setCreatedTime(System.currentTimeMillis()); doc.setSubjectName("templateTest-"+System.currentTimeMillis()); doc.setDeleted(BaseConstant.FILED_NO_DELETED); iiapTemplateMatchDeviceService.saveOrUpdate(doc); String id=doc.getId(); Assert.assertNotNull(id);
doc.setSubjectName("templateTest-change-"+System.currentTimeMillis()); iiapTemplateMatchDeviceService.saveOrUpdate(doc); String nid=doc.getId(); Assert.assertNotNull(nid); Assert.assertTrue(id.equals(nid)); } }
@Test public void searchTest(){ saveOrUpdateTest(); IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSubjectName("templateTest"); Iterable<IiapTemplateMatchDeviceDoc> result= iiapTemplateMatchDeviceService.search(dto); Assert.assertNotNull(result); Assert.assertTrue(!IterableUtils.isEmpty(result)); result.forEach(e->{ Assert.assertNotNull(e.getId()); }); }
@Test public void searchByPageTest(){ saveOrUpdateTest(); SortBy sortBy=new SortBy(); sortBy.setField(SortFieldEnum.SortFieldEnum2.getField()); sortBy.setType(SortTypeEnum.SortTypeEnum1.getField()); IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSortBy(sortBy); dto.setSubjectName("templateTest"); dto.setStartIndex(0); dto.setSize(1); Map<String,Object> result= iiapTemplateMatchDeviceService.searchByPage(dto); List<IiapTemplateMatchDeviceDoc> obj=(List<IiapTemplateMatchDeviceDoc>)result.get("data"); Assert.assertNotNull(obj); Assert.assertTrue(!CollectionUtils.isEmpty(obj)); String id1=obj.get(0).getId(); Long time1=obj.get(0).getCreatedTime();
dto.setStartIndex(1); dto.setSize(1); result= iiapTemplateMatchDeviceService.searchByPage(dto); obj=(List<IiapTemplateMatchDeviceDoc>)result.get("data"); Assert.assertNotNull(obj); Assert.assertTrue(!CollectionUtils.isEmpty(obj)); String id2=obj.get(0).getId(); Long time2=obj.get(0).getCreatedTime();
dto.setStartIndex(2); dto.setSize(1); result= iiapTemplateMatchDeviceService.searchByPage(dto); obj=(List<IiapTemplateMatchDeviceDoc>)result.get("data"); Assert.assertNotNull(obj); Assert.assertTrue(!CollectionUtils.isEmpty(obj)); String id5=obj.get(0).getId(); Long time5=obj.get(0).getCreatedTime();
dto.setStartIndex(3); dto.setSize(2); result= iiapTemplateMatchDeviceService.searchByPage(dto); obj=(List<IiapTemplateMatchDeviceDoc>)result.get("data"); Assert.assertNotNull(obj); Assert.assertTrue(!CollectionUtils.isEmpty(obj)); String id3=obj.get(0).getId(); String id4=obj.get(1).getId(); Long time3=obj.get(0).getCreatedTime(); Long time4=obj.get(1).getCreatedTime();
Boolean timeO=time1>time2 && time2>time3 && time3>time4; Boolean idO=!id1.equals(id2)&& !id2.equals(id3)&& !id3.equals(id4); Assert.assertTrue(timeO); Assert.assertTrue(idO); return; }
@Test public void deleteTest() throws InterruptedException { IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSubjectName("templateTest"); Iterable<IiapTemplateMatchDeviceDoc> result= iiapTemplateMatchDeviceService.search(dto); Assert.assertNotNull(result); Assert.assertTrue(!IterableUtils.isEmpty(result)); result.forEach(e->{ iiapTemplateMatchDeviceService.delete(e.getId()); }); Thread.sleep(1000);
result= iiapTemplateMatchDeviceService.search(dto); Assert.assertTrue(IterableUtils.isEmpty(result)); }
@Test public void deleteByQueryRequestTest() throws InterruptedException {
IiapTemplateMatchDeviceDto dto= new IiapTemplateMatchDeviceDto(); dto.setSubjectName("templateTest"); Iterable<IiapTemplateMatchDeviceDoc> result= iiapTemplateMatchDeviceService.search(dto); Assert.assertNotNull(result); Assert.assertTrue(!IterableUtils.isEmpty(result));
iiapTemplateMatchDeviceService.deleteByQueryRequest(dto);
Thread.sleep(1000);
result= iiapTemplateMatchDeviceService.search(dto); Assert.assertTrue(IterableUtils.isEmpty(result)); }
}
|
补充resource:es-test.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<elasticsearch:repositories base-package="com.yyt.aiot.cloud.test.repository"/>
<elasticsearch:transport-client id="client" cluster-nodes="10.0.14.50:9300" cluster-name="1-application" />
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"/> </bean>
<bean name="iiapDocMatchDeviceService" class="com.yyt.aiot.cloud.test.service.doc.IiapDocMatchDeviceServiceImpl"></bean> <bean name="iiapTemplateMatchDeviceService" class="com.yyt.aiot.cloud.test.service.template.IiapTemplateMatchDeviceService"></bean> </beans>
|