2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.template;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonObject;
25 import org.assertj.core.util.Lists;
26 import org.bson.Document;
27 import org.junit.Assert;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;;
32 import org.onap.pnfsimulator.template.search.viewmodel.FlatTemplateContent;
33 import org.onap.pnfsimulator.template.search.TemplateSearchHelper;
34 import org.springframework.data.mongodb.core.MongoTemplate;
35 import org.springframework.data.mongodb.core.query.Query;
37 import java.time.Instant;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Optional;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.ArgumentMatchers.anyObject;
45 import static org.mockito.Mockito.doReturn;
46 import static org.mockito.Mockito.times;
47 import static org.mockito.Mockito.verify;
48 import static org.mockito.Mockito.when;
49 import static org.mockito.MockitoAnnotations.initMocks;
51 class TemplateServiceTest {
52 private static final Gson GSON = new Gson();
53 private static final Template SAMPLE_TEMPLATE = new Template("sample name", new Document(), Instant.now().getNano());
54 private static final List<Template> SAMPLE_TEMPLATE_LIST = Collections.singletonList(SAMPLE_TEMPLATE);
57 private TemplateRepository templateRepositoryMock;
60 private MongoTemplate mongoTemplate;
63 private TemplateService service;
68 TemplateSearchHelper searchHelper = new TemplateSearchHelper(mongoTemplate);
69 service = new TemplateService(templateRepositoryMock, searchHelper);
73 void testShouldReturnAllTemplates() {
74 when(templateRepositoryMock.findAll()).thenReturn(SAMPLE_TEMPLATE_LIST);
76 List<Template> actual = service.getAll();
77 assertThat(actual).containsExactly(SAMPLE_TEMPLATE_LIST.get(0));
82 void testShouldGetTemplateBySpecifiedName() {
83 when(templateRepositoryMock.findById("sample name")).thenReturn(Optional.of(SAMPLE_TEMPLATE));
85 Optional<Template> actualTemplate = service.get("sample name");
86 assertThat(actualTemplate).isPresent();
87 assertThat(actualTemplate.get()).isEqualTo(SAMPLE_TEMPLATE);
91 void testShouldSaveTemplate() {
92 service.persist(SAMPLE_TEMPLATE);
94 verify(templateRepositoryMock, times(1)).save(SAMPLE_TEMPLATE);
98 void testShouldDeleteTemplateByName() {
99 service.delete("sample name");
101 verify(templateRepositoryMock, times(1)).deleteById("sample name");
106 void testShouldReturnTemplatesAccordingToGivenSearchCriteria() {
107 doReturn(Lists.emptyList()).when(mongoTemplate).find(any(Query.class), anyObject(), any(String.class));
109 List<String> idsByContentCriteria = service.getIdsByContentCriteria(GSON.fromJson("{\"domain\": \"notification.json\"}", JsonObject.class));
111 assertThat(idsByContentCriteria).isEmpty();
115 void shouldReturnNamesForGivenComposedSearchCriteria(){
116 JsonObject composedCriteriaObject = GSON.fromJson("{\"eventName\": \"pnfRegistration_Nokia_5gDu\", \"sequence\": 1}", JsonObject.class);
117 List<FlatTemplateContent> arr = Lists.newArrayList(new FlatTemplateContent("sampleId", null));
119 doReturn(arr).when(mongoTemplate).find(any(Query.class), anyObject(), any(String.class));
121 List<String> idsByContentCriteria = service.getIdsByContentCriteria(composedCriteriaObject);
122 assertThat(idsByContentCriteria).containsOnly("sampleId");
126 void shouldReturnFalseWhenOverwritingWithoutForce() {
127 String id = "someTemplate";
128 Template template = new Template(id, new Document(), Instant.now().getNano());
129 when(templateRepositoryMock.existsById(id)).thenReturn(true);
130 boolean actual = service.tryPersistOrOverwrite(template, false);
131 Assert.assertFalse(actual);
135 void shouldReturnTrueWhenOverwritingWithForce() {
136 String id = "someTemplate";
137 Template template = new Template(id, new Document(), Instant.now().getNano());
138 when(templateRepositoryMock.existsById(id)).thenReturn(true);
139 boolean actual = service.tryPersistOrOverwrite(template, true);
140 Assert.assertTrue(actual);
144 void shouldReturnTrueWhenSavingNonExistingTemplate() {
145 String id = "someTemplate";
146 Template template = new Template(id, new Document(), Instant.now().getNano());
147 when(templateRepositoryMock.existsById(id)).thenReturn(false);
148 boolean actual = service.tryPersistOrOverwrite(template, false);
149 Assert.assertTrue(actual);