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 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;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
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.*;
37 public class EngineEntityServiceImplTest {
39 private EngineEntityService service = new EngineEntityServiceImpl(new DbDaoUtilStub());
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();
80 public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
87 class EngineEntityDaoStub implements EngineEntityDao {
89 private Set<EngineEntity> entitySet = new HashSet(){
91 add(new EngineEntity("org.onap.holmes", 9201));
96 public EngineEntity getEntity(String id) {
97 return entitySet.stream().filter(e -> e.getId().equals(id)).findFirst().get();
101 public List<EngineEntity> getAllEntities() {
102 return new ArrayList<>(entitySet);
106 public void insertEntity(EngineEntity entity) {
107 entitySet.add(entity);
111 public void updateEntity(EngineEntity entity) {
112 entitySet.add(entity);
116 public void deleteEntity(String id) {
117 for (EngineEntity entity : entitySet) {
118 if (entity.getId().equals(id)) {
119 entitySet.remove(entity);