074696094a8e5d4abd819b2c4aa1facf599bc899
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.pnfsimulator.template;
22
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;
36
37 import java.time.Instant;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Optional;
41
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;
50
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);
55
56     @Mock
57     private TemplateRepository templateRepositoryMock;
58
59     @Mock
60     private MongoTemplate mongoTemplate;
61
62     @InjectMocks
63     private TemplateService service;
64
65     @BeforeEach
66     void setUp() {
67         initMocks(this);
68         TemplateSearchHelper searchHelper = new TemplateSearchHelper(mongoTemplate);
69         service = new TemplateService(templateRepositoryMock, searchHelper);
70     }
71
72     @Test
73     void testShouldReturnAllTemplates() {
74         when(templateRepositoryMock.findAll()).thenReturn(SAMPLE_TEMPLATE_LIST);
75
76         List<Template> actual = service.getAll();
77         assertThat(actual).containsExactly(SAMPLE_TEMPLATE_LIST.get(0));
78     }
79
80
81     @Test
82     void testShouldGetTemplateBySpecifiedName() {
83         when(templateRepositoryMock.findById("sample name")).thenReturn(Optional.of(SAMPLE_TEMPLATE));
84
85         Optional<Template> actualTemplate = service.get("sample name");
86         assertThat(actualTemplate).isPresent();
87         assertThat(actualTemplate.get()).isEqualTo(SAMPLE_TEMPLATE);
88     }
89
90     @Test
91     void testShouldSaveTemplate() {
92         service.persist(SAMPLE_TEMPLATE);
93
94         verify(templateRepositoryMock, times(1)).save(SAMPLE_TEMPLATE);
95     }
96
97     @Test
98     void testShouldDeleteTemplateByName() {
99         service.delete("sample name");
100
101         verify(templateRepositoryMock, times(1)).deleteById("sample name");
102     }
103
104
105     @Test
106     void testShouldReturnTemplatesAccordingToGivenSearchCriteria() {
107         doReturn(Lists.emptyList()).when(mongoTemplate).find(any(Query.class), anyObject(), any(String.class));
108
109         List<String> idsByContentCriteria = service.getIdsByContentCriteria(GSON.fromJson("{\"domain\": \"notification.json\"}", JsonObject.class));
110
111         assertThat(idsByContentCriteria).isEmpty();
112     }
113
114     @Test
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));
118
119         doReturn(arr).when(mongoTemplate).find(any(Query.class), anyObject(), any(String.class));
120
121         List<String> idsByContentCriteria = service.getIdsByContentCriteria(composedCriteriaObject);
122         assertThat(idsByContentCriteria).containsOnly("sampleId");
123     }
124
125     @Test
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);
132     }
133
134     @Test
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);
141     }
142
143     @Test
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);
150     }
151
152 }