2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
26 package org.onap.clamp.clds.it;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
34 import java.util.LinkedList;
35 import java.util.List;
37 import javax.servlet.http.HttpServletRequest;
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;
61 * Test CLDS Dictionary Service APIs.
63 @RunWith(SpringJUnit4ClassRunner.class)
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 public class CldsDictionaryServiceItCase {
67 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDictionaryServiceItCase.class);
69 private CldsDictionaryService cldsDictionaryService;
70 private CldsDictionary cldsDictionary;
71 private CldsDictionaryItem cldsDictionaryItem;
72 private List<GrantedAuthority> authList = new LinkedList<>();
74 private static final String DICTIONARY_NAME = "TestDictionary";
77 * Setup the variable before the tests execution.
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"));
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);
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);
99 cldsDictionaryService.setSecurityContext(securityContext);
101 cldsDictionary = cldsDictionaryService.createDictionary(DICTIONARY_NAME);
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);
111 logger.info("Initial Clds Dictionary uploaded in DB:" + cldsDictionaryItem);
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());
124 public void testCreateOrUpdateDictionaryUsedByFrontend() {
125 ResponseEntity<CldsDictionary> responseEntity =
126 cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, null);
127 CldsDictionary dictionary1 = responseEntity.getBody();
129 responseEntity = cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, cldsDictionary);
130 CldsDictionary dictionary2 = responseEntity.getBody();
132 responseEntity = cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, new CldsDictionary());
133 CldsDictionary dictionary3 = responseEntity.getBody();
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());
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");
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());
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);
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);