2 * Copyright 2020 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.common.engine.service.impl;
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;
28 import static org.hamcrest.core.Is.is;
29 import static org.hamcrest.core.IsNull.notNullValue;
30 import static org.junit.Assert.*;
32 public class EngineEntityServiceImplTest {
33 private EngineEntityService service = new EngineEntityServiceImpl(new DbDaoUtilStub(null));
36 public void getLegacyEngines() {
37 List<String> legacyEngines = service.getLegacyEngines();
38 assertThat(legacyEngines.size(), is(2));
42 public void getEntity() throws Exception {
43 EngineEntity entity = service.getEntity("org.onap.holmes_9201");
44 assertThat(entity, notNullValue());
48 public void getAllEntities() throws Exception {
49 List<EngineEntity> entities = service.getAllEntities();
50 assertThat(entities.size(), is(1));
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));
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));
70 public void deleteEntity() throws Exception {
71 service.deleteEntity("org.onap.holmes.another_9201");
72 assertThat(service.getAllEntities().size(), is(1));
76 class DbDaoUtilStub extends DbDaoUtil {
77 private EngineEntityDao dao = new EngineEntityDaoStub();
79 public DbDaoUtilStub(Jdbi jdbi) {
84 public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
91 class EngineEntityDaoStub implements EngineEntityDao {
93 private Set<EngineEntity> entitySet = new HashSet(){
95 add(new EngineEntity("org.onap.holmes", 9201));
100 public EngineEntity getEntity(String id) {
101 return entitySet.stream().filter(e -> e.getId().equals(id)).findFirst().get();
105 public List<EngineEntity> getAllEntities() {
106 return new ArrayList<>(entitySet);
110 public List<String> getLegacyEngines() {
111 return Arrays.asList("org.onap.holmes", "org.onap.holmes.legacy.1");
115 public void insertEntity(EngineEntity entity) {
116 entitySet.add(entity);
120 public void updateEntity(EngineEntity entity) {
121 entitySet.add(entity);
125 public void deleteEntity(String id) {
126 for (EngineEntity entity : entitySet) {
127 if (entity.getId().equals(id)) {
128 entitySet.remove(entity);