##### Coverage Report - com.ctrip.framework.apollo.biz.service.AppService

Classes in this File | Line Coverage | Branch Coverage | Complexity
AppService |
35% | 10/28
50% | 2/4
| 1.5714285714285714;1.571

1 |  |
     package com.ctrip.framework.apollo.biz.service;
2 |  |
3 |  |
     import java.util.List;
4 |  |
     import java.util.Objects;
5 |  |
6 |  |
     import org.springframework.beans.factory.annotation.Autowired;
7 |  |
     import org.springframework.data.domain.Page;
8 |  |
     import org.springframework.data.domain.Pageable;
9 |  |
     import org.springframework.stereotype.Service;
10 |  |
     import org.springframework.transaction.annotation.Transactional;
11 |  |
12 |  |
     import com.ctrip.framework.apollo.common.entity.App;
13 |  |
     import com.ctrip.framework.apollo.biz.entity.Audit;
14 |  |
     import com.ctrip.framework.apollo.biz.repository.AppRepository;
15 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
16 |  |
     import com.ctrip.framework.apollo.core.exception.ServiceException;
17 |  |
18 |  |
     @Service
19 | 1 |
     public class AppService {
20 |  |
21 |  |
       @Autowired
22 |  |
       private AppRepository appRepository;
23 |  |
24 |  |
       @Autowired
25 |  |
       private AuditService auditService;
26 |  |
27 |  |
       public boolean isAppIdUnique(String appId) {
28 | 6 |
         Objects.requireNonNull(appId, "AppId must not be null");
29 | 6 |
         return Objects.isNull(appRepository.findByAppId(appId));
30 |  |
       }
31 |  |
32 |  |
       @Transactional
33 |  |
       public void delete(long id, String operator) {
34 | 0 |
         App app = appRepository.findOne(id);
35 | 0 |
         if (app == null) {
36 | 0 |
           return;
37 |  |
         }
38 |  |
39 | 0 |
         app.setDeleted(true);
40 | 0 |
         app.setDataChangeLastModifiedBy(operator);
41 | 0 |
         appRepository.save(app);
42 |  |
43 | 0 |
         auditService.audit(App.class.getSimpleName(), id, Audit.OP.DELETE, operator);
44 | 0 |
       }
45 |  |
46 |  |
       public List<App> findAll(Pageable pageable) {
47 | 0 |
         Page<App> page = appRepository.findAll(pageable);
48 | 0 |
         return page.getContent();
49 |  |
       }
50 |  |
51 |  |
       public List<App> findByName(String name) {
52 | 0 |
         return appRepository.findByName(name);
53 |  |
       }
54 |  |
55 |  |
       public App findOne(String appId) {
56 | 0 |
         return appRepository.findByAppId(appId);
57 |  |
       }
58 |  |
59 |  |
       @Transactional
60 |  |
       public App save(App entity) {
61 | 6 |
         if (!isAppIdUnique(entity.getAppId())) {
62 | 1 |
           throw new ServiceException("appId not unique");
63 |  |
         }
64 | 5 |
         entity.setId(0);//protection
65 | 5 |
         App app = appRepository.save(entity);
66 |  |
67 | 10 |
         auditService.audit(App.class.getSimpleName(), app.getId(), Audit.OP.INSERT,
68 | 5 |
             app.getDataChangeCreatedBy());
69 |  |
70 | 5 |
         return app;
71 |  |
       }
72 |  |
73 |  |
       @Transactional
74 |  |
       public App update(App app) {
75 | 0 |
         App managedApp = appRepository.findByAppId(app.getAppId());
76 | 0 |
         BeanUtils.copyEntityProperties(app, managedApp);
77 | 0 |
         managedApp = appRepository.save(managedApp);
78 |  |
79 | 0 |
         auditService.audit(App.class.getSimpleName(), managedApp.getId(), Audit.OP.UPDATE,
80 | 0 |
             managedApp.getDataChangeLastModifiedBy());
81 |  |
82 | 0 |
         return managedApp;
83 |  |
       }
84 |  |
     }