2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.it;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
32 import java.io.IOException;
33 import java.util.LinkedList;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Matchers;
42 import org.mockito.Mockito;
43 import org.onap.clamp.clds.model.CldsDictionary;
44 import org.onap.clamp.clds.model.CldsDictionaryItem;
45 import org.onap.clamp.clds.service.CldsDictionaryService;
46 import org.onap.clamp.clds.util.LoggingUtils;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
52 import org.springframework.security.core.Authentication;
53 import org.springframework.security.core.GrantedAuthority;
54 import org.springframework.security.core.authority.SimpleGrantedAuthority;
55 import org.springframework.security.core.context.SecurityContext;
56 import org.springframework.security.core.userdetails.User;
57 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
60 * Test CLDS Dictionary Service APIs.
62 @RunWith(SpringJUnit4ClassRunner.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 public class CldsDictionaryServiceItCase {
66 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDictionaryServiceItCase.class);
68 private CldsDictionaryService cldsDictionaryService;
69 private Authentication authentication;
70 private CldsDictionary cldsDictionary;
71 private CldsDictionaryItem cldsDictionaryItem;
72 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
73 private LoggingUtils util;
76 * Setup the variable before the tests execution.
79 * In case of issues when opening the files
82 public void setupBefore() throws IOException {
83 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
84 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
85 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
86 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
87 authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
88 authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|read"));
89 authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|update"));
90 authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
92 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
93 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
95 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 = new CldsDictionary();
103 cldsDictionary.setDictionaryName("TestDictionary");
104 ResponseEntity entity = cldsDictionaryService.createOrUpdateDictionary("TestDictionary", cldsDictionary);
105 cldsDictionary = (CldsDictionary) entity.getBody();
107 cldsDictionaryItem = new CldsDictionaryItem();
108 cldsDictionaryItem.setDictElementShortName("TestDictionaryItemShortName");
109 cldsDictionaryItem.setDictElementName("TestDictionaryItemName");
110 cldsDictionaryItem.setDictElementType("string");
111 cldsDictionaryItem.setDictionaryId(cldsDictionary.getDictionaryId());
112 cldsDictionaryItem.setDictElementDesc("TestDictionaryItemDesc");
113 cldsDictionaryService.createOrUpdateDictionaryElements("TestDictionary", cldsDictionaryItem);
115 logger.info("Initial Clds Dictionary uploaded in DB:" + cldsDictionaryItem);
119 public void testCreateOrUpdateDictionary() throws Exception {
120 ResponseEntity<CldsDictionary> responseEntity = cldsDictionaryService.createOrUpdateDictionary("TestDictionary",
122 CldsDictionary dictionary = responseEntity.getBody();
123 assertNotNull(dictionary);
124 logger.info("CLDS Dictionary is:" + dictionary);
125 assertEquals("TestDictionary", dictionary.getDictionaryName());
129 public void testCreateOrUpdateDictionaryElements() throws Exception {
130 cldsDictionaryItem = new CldsDictionaryItem();
131 cldsDictionaryItem.setDictElementShortName("TestDictionaryItemShortName1");
132 cldsDictionaryItem.setDictElementName("TestDictionaryItemName1");
133 cldsDictionaryItem.setDictElementType("string");
134 cldsDictionaryItem.setDictionaryId(cldsDictionary.getDictionaryId());
135 cldsDictionaryItem.setDictElementDesc("TestDictionaryItemDesc1");
137 ResponseEntity<CldsDictionaryItem> responseEntity = cldsDictionaryService
138 .createOrUpdateDictionaryElements("TestDictionary", cldsDictionaryItem);
139 CldsDictionaryItem dictionaryItem = responseEntity.getBody();
140 assertNotNull(dictionaryItem);
141 logger.info("CLDS Dictionary Item is:" + dictionaryItem);
142 assertEquals("TestDictionaryItemName1", dictionaryItem.getDictElementName());
146 public void testGetAllDictionaryNames() throws Exception {
147 ResponseEntity<List<CldsDictionary>> responseEntity = cldsDictionaryService.getAllDictionaryNames();
148 List<CldsDictionary> dictionaries = responseEntity.getBody();
149 assertNotNull(dictionaries);
150 logger.info("CLDS Dictionary List is:" + dictionaries);
154 public void testGetDictionaryElementsByName() throws Exception {
155 ResponseEntity<List<CldsDictionaryItem>> responseEntity = cldsDictionaryService
156 .getDictionaryElementsByName("TestDictionary");
157 List<CldsDictionaryItem> dictionaryItems = responseEntity.getBody();
158 assertNotNull(dictionaryItems);
159 logger.info("CLDS Dictionary Item LIst is:" + dictionaryItems);