Merge "Rework action itmes"
[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  * 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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32 import java.io.IOException;
33 import java.util.LinkedList;
34 import java.util.List;
35
36 import javax.servlet.http.HttpServletRequest;
37
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;
58
59 /**
60  * Test CLDS Dictionary Service APIs.
61  */
62 @RunWith(SpringJUnit4ClassRunner.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 public class CldsDictionaryServiceItCase {
65
66     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDictionaryServiceItCase.class);
67     @Autowired
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;
74
75     /**
76      * Setup the variable before the tests execution.
77      *
78      * @throws IOException
79      *         In case of issues when opening the files
80      */
81     @Before
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);
91
92         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
93         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
94
95         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 = new CldsDictionary();
102
103         cldsDictionary.setDictionaryName("TestDictionary");
104         ResponseEntity entity = cldsDictionaryService.createOrUpdateDictionary("TestDictionary", cldsDictionary);
105         cldsDictionary = (CldsDictionary) entity.getBody();
106
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);
114
115         logger.info("Initial Clds Dictionary uploaded in DB:" + cldsDictionaryItem);
116     }
117
118     @Test
119     public void testCreateOrUpdateDictionary() throws Exception {
120         ResponseEntity<CldsDictionary> responseEntity = cldsDictionaryService.createOrUpdateDictionary("TestDictionary",
121             cldsDictionary);
122         CldsDictionary dictionary = responseEntity.getBody();
123         assertNotNull(dictionary);
124         logger.info("CLDS Dictionary is:" + dictionary);
125         assertEquals("TestDictionary", dictionary.getDictionaryName());
126     }
127
128     @Test
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");
136
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());
143     }
144
145     @Test
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);
151     }
152
153     @Test
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);
160     }
161 }