Switched from Dropwizard to Springboot
[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 org.jdbi.v3.core.Jdbi;
20 import org.junit.Test;
21 import org.onap.holmes.common.engine.dao.EngineEntityDao;
22 import org.onap.holmes.common.engine.entity.EngineEntity;
23 import org.onap.holmes.common.engine.service.EngineEntityService;
24 import org.onap.holmes.common.database.DbDaoUtil;
25
26 import java.util.*;
27
28 import static org.hamcrest.core.Is.is;
29 import static org.hamcrest.core.IsNull.notNullValue;
30 import static org.junit.Assert.*;
31
32 public class EngineEntityServiceImplTest {
33     private EngineEntityService service = new EngineEntityServiceImpl(new DbDaoUtilStub(null));
34
35     @Test
36     public void getLegacyEngines() {
37         List<String> legacyEngines = service.getLegacyEngines();
38         assertThat(legacyEngines.size(), is(2));
39     }
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     public DbDaoUtilStub(Jdbi jdbi) {
80         super(jdbi);
81     }
82
83     @Override
84     public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
85
86         return (T) dao;
87
88     }
89 }
90
91 class EngineEntityDaoStub implements EngineEntityDao {
92
93     private Set<EngineEntity> entitySet = new HashSet(){
94         {
95             add(new EngineEntity("org.onap.holmes", 9201));
96         }
97     };
98
99     @Override
100     public EngineEntity getEntity(String id) {
101         return entitySet.stream().filter(e -> e.getId().equals(id)).findFirst().get();
102     }
103
104     @Override
105     public List<EngineEntity> getAllEntities() {
106         return new ArrayList<>(entitySet);
107     }
108
109     @Override
110     public List<String> getLegacyEngines() {
111         return Arrays.asList("org.onap.holmes", "org.onap.holmes.legacy.1");
112     }
113
114     @Override
115     public void insertEntity(EngineEntity entity) {
116         entitySet.add(entity);
117     }
118
119     @Override
120     public void updateEntity(EngineEntity entity) {
121         entitySet.add(entity);
122     }
123
124     @Override
125     public void deleteEntity(String id) {
126         for (EngineEntity entity : entitySet) {
127             if (entity.getId().equals(id)) {
128                 entitySet.remove(entity);
129                 break;
130             }
131         }
132     }
133 }