Added DB Query for Legacy Engine Instances
[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.*;
28
29 import static com.google.common.base.Predicates.notNull;
30 import static org.hamcrest.core.Is.is;
31 import static org.hamcrest.core.IsNull.notNullValue;
32 import static org.junit.Assert.*;
33
34 public class EngineEntityServiceImplTest {
35     private EngineEntityService service = new EngineEntityServiceImpl(new DbDaoUtilStub());
36
37     @Test
38     public void getLegacyEngines() {
39         List<String> legacyEngines = service.getLegacyEngines();
40         assertThat(legacyEngines.size(), is(2));
41     }
42
43     @Test
44     public void getEntity() throws Exception {
45         EngineEntity entity = service.getEntity("org.onap.holmes_9201");
46         assertThat(entity, notNullValue());
47     }
48
49     @Test
50     public void getAllEntities() throws Exception {
51         List<EngineEntity> entities = service.getAllEntities();
52         assertThat(entities.size(), is(1));
53     }
54
55     @Test
56     public void updateEntity() throws Exception {
57         EngineEntity entity = new EngineEntity("org.onap.holmes", 9201);
58         long time = System.currentTimeMillis();
59         entity.setLastModified(time);
60         service.updateEntity(entity);
61         assertThat(service.getEntity("org.onap.holmes_9201").getLastModified(), is(time));
62     }
63
64     @Test
65     public void insertEntity() throws Exception {
66         EngineEntity entity = new EngineEntity("org.onap.holmes.another", 9201);
67         service.insertEntity(entity);
68         assertThat(service.getAllEntities().size(), is(2));
69     }
70
71     @Test
72     public void deleteEntity() throws Exception {
73         service.deleteEntity("org.onap.holmes.another_9201");
74         assertThat(service.getAllEntities().size(), is(1));
75     }
76 }
77
78 class DbDaoUtilStub extends DbDaoUtil {
79     private EngineEntityDao dao = new EngineEntityDaoStub();
80
81     @Override
82     public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
83
84         return (T) dao;
85
86     }
87 }
88
89 class EngineEntityDaoStub implements EngineEntityDao {
90
91     private Set<EngineEntity> entitySet = new HashSet(){
92         {
93             add(new EngineEntity("org.onap.holmes", 9201));
94         }
95     };
96
97     @Override
98     public EngineEntity getEntity(String id) {
99         return entitySet.stream().filter(e -> e.getId().equals(id)).findFirst().get();
100     }
101
102     @Override
103     public List<EngineEntity> getAllEntities() {
104         return new ArrayList<>(entitySet);
105     }
106
107     @Override
108     public List<String> getLegacyEngines() {
109         return Arrays.asList("org.onap.holmes", "org.onap.holmes.legacy.1");
110     }
111
112     @Override
113     public void insertEntity(EngineEntity entity) {
114         entitySet.add(entity);
115     }
116
117     @Override
118     public void updateEntity(EngineEntity entity) {
119         entitySet.add(entity);
120     }
121
122     @Override
123     public void deleteEntity(String id) {
124         for (EngineEntity entity : entitySet) {
125             if (entity.getId().equals(id)) {
126                 entitySet.remove(entity);
127                 break;
128             }
129         }
130     }
131 }