Added some tools for engine instance management
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / engine / service / impl / EngineEntityServiceImplTest.java
1 /**
2  * Copyright 2020 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.engine.service.impl;
18
19 import com.google.common.base.CharMatcher;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.onap.holmes.common.engine.dao.EngineEntityDao;
23 import org.onap.holmes.common.engine.entity.EngineEntity;
24 import org.onap.holmes.common.engine.service.EngineEntityService;
25 import org.onap.holmes.common.utils.DbDaoUtil;
26
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 import static com.google.common.base.Predicates.notNull;
33 import static org.hamcrest.core.Is.is;
34 import static org.hamcrest.core.IsNull.notNullValue;
35 import static org.junit.Assert.*;
36
37 public class EngineEntityServiceImplTest {
38
39     private EngineEntityService service = new EngineEntityServiceImpl(new DbDaoUtilStub());
40
41     @Test
42     public void getEntity() throws Exception {
43         EngineEntity entity = service.getEntity("org.onap.holmes_9201");
44         assertThat(entity, notNullValue());
45     }
46
47     @Test
48     public void getAllEntities() throws Exception {
49         List<EngineEntity> entities = service.getAllEntities();
50         assertThat(entities.size(), is(1));
51     }
52
53     @Test
54     public void updateEntity() throws Exception {
55         EngineEntity entity = new EngineEntity("org.onap.holmes", 9201);
56         long time = System.currentTimeMillis();
57         entity.setLastModified(time);
58         service.updateEntity(entity);
59         assertThat(service.getEntity("org.onap.holmes_9201").getLastModified(), is(time));
60     }
61
62     @Test
63     public void insertEntity() throws Exception {
64         EngineEntity entity = new EngineEntity("org.onap.holmes.another", 9201);
65         service.insertEntity(entity);
66         assertThat(service.getAllEntities().size(), is(2));
67     }
68
69     @Test
70     public void deleteEntity() throws Exception {
71         service.deleteEntity("org.onap.holmes.another_9201");
72         assertThat(service.getAllEntities().size(), is(1));
73     }
74 }
75
76 class DbDaoUtilStub extends DbDaoUtil {
77     private EngineEntityDao dao = new EngineEntityDaoStub();
78
79     @Override
80     public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
81
82         return (T) dao;
83
84     }
85 }
86
87 class EngineEntityDaoStub implements EngineEntityDao {
88
89     private Set<EngineEntity> entitySet = new HashSet(){
90         {
91             add(new EngineEntity("org.onap.holmes", 9201));
92         }
93     };
94
95     @Override
96     public EngineEntity getEntity(String id) {
97         return entitySet.stream().filter(e -> e.getId().equals(id)).findFirst().get();
98     }
99
100     @Override
101     public List<EngineEntity> getAllEntities() {
102         return new ArrayList<>(entitySet);
103     }
104
105     @Override
106     public void insertEntity(EngineEntity entity) {
107         entitySet.add(entity);
108     }
109
110     @Override
111     public void updateEntity(EngineEntity entity) {
112         entitySet.add(entity);
113     }
114
115     @Override
116     public void deleteEntity(String id) {
117         for (EngineEntity entity : entitySet) {
118             if (entity.getId().equals(id)) {
119                 entitySet.remove(entity);
120                 break;
121             }
122         }
123     }
124 }