Merge "Improve test for DictionaryService."
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsDictionaryServiceItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.it;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 import java.util.LinkedList;
35 import java.util.List;
36
37 import javax.servlet.http.HttpServletRequest;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Matchers;
43 import org.mockito.Mockito;
44 import org.onap.clamp.clds.model.CldsDictionary;
45 import org.onap.clamp.clds.model.CldsDictionaryItem;
46 import org.onap.clamp.clds.service.CldsDictionaryService;
47 import org.onap.clamp.clds.util.LoggingUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
53 import org.springframework.security.core.Authentication;
54 import org.springframework.security.core.GrantedAuthority;
55 import org.springframework.security.core.authority.SimpleGrantedAuthority;
56 import org.springframework.security.core.context.SecurityContext;
57 import org.springframework.security.core.userdetails.User;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59
60 /**
61  * Test CLDS Dictionary Service APIs.
62  */
63 @RunWith(SpringJUnit4ClassRunner.class)
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 public class CldsDictionaryServiceItCase {
66
67     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDictionaryServiceItCase.class);
68     @Autowired
69     private CldsDictionaryService cldsDictionaryService;
70     private CldsDictionary cldsDictionary;
71     private CldsDictionaryItem cldsDictionaryItem;
72     private List<GrantedAuthority> authList = new LinkedList<>();
73
74     private static final String DICTIONARY_NAME = "TestDictionary";
75
76     /**
77      * Setup the variable before the tests execution.
78      *
79      */
80     @Before
81     public void setupBefore() {
82         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
83         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
84         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
85         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
86         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
87         authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|read"));
88         authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|update"));
89
90         Authentication authentication =
91             new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
92         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
93         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
94
95         LoggingUtils util = Mockito.mock(LoggingUtils.class);
96         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
97         cldsDictionaryService.setLoggingUtil(util);
98
99         cldsDictionaryService.setSecurityContext(securityContext);
100
101         cldsDictionary = cldsDictionaryService.createDictionary(DICTIONARY_NAME);
102
103         cldsDictionaryItem = new CldsDictionaryItem();
104         cldsDictionaryItem.setDictElementShortName("TestDictionaryItemShortName");
105         cldsDictionaryItem.setDictElementName("TestDictionaryItemName");
106         cldsDictionaryItem.setDictElementType("string");
107         cldsDictionaryItem.setDictionaryId(cldsDictionary.getDictionaryId());
108         cldsDictionaryItem.setDictElementDesc("TestDictionaryItemDesc");
109         cldsDictionaryService.createOrUpdateDictionaryElements(DICTIONARY_NAME, cldsDictionaryItem);
110
111         logger.info("Initial Clds Dictionary uploaded in DB:" + cldsDictionaryItem);
112     }
113
114     @Test
115     public void testCreateDictionaryFromString() {
116         String dictionaryName = "TestDefaultDictionary";
117         CldsDictionary dictionary = cldsDictionaryService.createDictionary(dictionaryName);
118         assertNotNull(dictionary);
119         logger.info("CLDS Default Dictionary is:" + dictionary);
120         assertEquals(dictionaryName, dictionary.getDictionaryName());
121     }
122
123     @Test
124     public void testCreateOrUpdateDictionaryUsedByFrontend() {
125         ResponseEntity<CldsDictionary> responseEntity =
126                 cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, null);
127         CldsDictionary dictionary1 = responseEntity.getBody();
128
129         responseEntity = cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, cldsDictionary);
130         CldsDictionary dictionary2 = responseEntity.getBody();
131
132         responseEntity = cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, new CldsDictionary());
133         CldsDictionary dictionary3 = responseEntity.getBody();
134
135         assertNotNull(dictionary1);
136         assertNotNull(dictionary2);
137         assertNotNull(dictionary3);
138         assertEquals(DICTIONARY_NAME, dictionary1.getDictionaryName());
139         assertEquals(DICTIONARY_NAME, dictionary2.getDictionaryName());
140         assertNotNull(dictionary3.getDictionaryName());
141         assertEquals(DICTIONARY_NAME, dictionary3.getDictionaryName());
142     }
143
144     @Test
145     public void testCreateOrUpdateDictionaryElements() {
146         cldsDictionaryItem = new CldsDictionaryItem();
147         cldsDictionaryItem.setDictElementShortName("TestDictionaryItemShortName1");
148         cldsDictionaryItem.setDictElementName("TestDictionaryItemName1");
149         cldsDictionaryItem.setDictElementType("string");
150         cldsDictionaryItem.setDictionaryId(cldsDictionary.getDictionaryId());
151         cldsDictionaryItem.setDictElementDesc("TestDictionaryItemDesc1");
152
153         ResponseEntity<CldsDictionaryItem> responseEntity = cldsDictionaryService
154                 .createOrUpdateDictionaryElements(DICTIONARY_NAME, cldsDictionaryItem);
155         CldsDictionaryItem dictionaryItem = responseEntity.getBody();
156         assertNotNull(dictionaryItem);
157         logger.info("CLDS Dictionary Item is:" + dictionaryItem);
158         assertEquals("TestDictionaryItemName1", dictionaryItem.getDictElementName());
159     }
160
161     @Test
162     public void testGetAllDictionaryNames() {
163         ResponseEntity<List<CldsDictionary>> responseEntity = cldsDictionaryService.getAllDictionaryNames();
164         List<CldsDictionary> dictionaries = responseEntity.getBody();
165         assertNotNull(dictionaries);
166         logger.info("CLDS Dictionary List is:" + dictionaries);
167     }
168
169     @Test
170     public void testGetDictionaryElementsByName() {
171         ResponseEntity<List<CldsDictionaryItem>> responseEntity = cldsDictionaryService
172                 .getDictionaryElementsByName(DICTIONARY_NAME);
173         List<CldsDictionaryItem> dictionaryItems = responseEntity.getBody();
174         assertNotNull(dictionaryItems);
175         logger.info("CLDS Dictionary Item LIst is:" + dictionaryItems);
176     }
177 }