From a1aa1d36b2bb500e8d021d2f911f1d24b4113e0d Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Tue, 4 Jun 2019 13:50:48 +0200 Subject: Improve test for DictionaryService. Add new test for CldsDictionaryService. Refactor createOrUpdateDictionary method. Remove useless final from private method. Checkstyle issue. Change-Id: I26e782aeb4a7429d342f90d4231dd24cca2e152f Issue-ID: CLAMP-355 Signed-off-by: Krystian Kedron --- .../clamp/clds/service/CldsDictionaryService.java | 58 ++++++++++++----- .../clamp/clds/it/CldsDictionaryServiceItCase.java | 72 +++++++++++++--------- 2 files changed, 86 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/main/java/org/onap/clamp/clds/service/CldsDictionaryService.java b/src/main/java/org/onap/clamp/clds/service/CldsDictionaryService.java index c228e171a..454056cd3 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsDictionaryService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsDictionaryService.java @@ -60,47 +60,73 @@ public class CldsDictionaryService extends SecureServiceBase { @PostConstruct - private final void initConstruct() { + private void initConstruct() { permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read"); permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "update"); } /** - * REST Service that creates or Updates a Dictionary. - * + * REST Service that creates a Dictionary. + * * @param dictionaryName dictionary name - * @param cldsDictionary clds dictionary * @return CldsDictionary that was created in DB. */ + public CldsDictionary createDictionary(String dictionaryName) { + CldsDictionary cldsDictionary = new CldsDictionary(); + cldsDictionary.setDictionaryName(dictionaryName); + cldsDictionary.save(cldsDictionary.getDictionaryName(), cldsDao, getUserId()); + return cldsDictionary; + } + + /** + * REST Service that creates or Updates a Dictionary. + * Used in clds-services.xml + * + * @param cldsDictionary clds dictionary + * @return ResponseEntity with CldsDictionary that was created in DB. + */ public ResponseEntity createOrUpdateDictionary(String dictionaryName, - CldsDictionary cldsDictionary) { - final Date startTime = new Date(); + CldsDictionary cldsDictionary) { + + Date startTime = new Date(); LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionary", getPrincipalName()); // TODO revisit based on new permissions isAuthorized(permissionUpdateTosca); + if (cldsDictionary == null) { - cldsDictionary = new CldsDictionary(); - cldsDictionary.setDictionaryName(dictionaryName); + + cldsDictionary = createDictionary(dictionaryName); + } else { + + if (cldsDictionary.getDictionaryName() == null) { + cldsDictionary.setDictionaryName(dictionaryName); + } + + cldsDictionary.save(cldsDictionary.getDictionaryName(), cldsDao, getUserId()); } - cldsDictionary.save(dictionaryName, cldsDao, getUserId()); - auditLogInfo("createOrUpdateDictionary", startTime); + + LoggingUtils.setTimeContext(startTime, new Date()); + LoggingUtils.setResponseContext("0", "createOrUpdateDictionary success", this.getClass().getName()); + auditLogger.info("createOrUpdateDictionary completed"); + return new ResponseEntity<>(cldsDictionary, HttpStatus.OK); } /** * REST Service that creates or Updates a Dictionary Elements for dictionary * in DB. - * + * * @param dictionaryName dictionary name * @param dictionaryItem dictionary item * @return CldsDictionaryItem A dictionary items that was created or updated * in DB */ public ResponseEntity createOrUpdateDictionaryElements(String dictionaryName, - CldsDictionaryItem dictionaryItem) { + CldsDictionaryItem dictionaryItem) { final Date startTime = new Date(); - LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionaryElements", getPrincipalName()); + LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionaryElements", + getPrincipalName()); // TODO revisit based on new permissions isAuthorized(permissionUpdateTosca); dictionaryItem.save(dictionaryName, cldsDao, getUserId()); @@ -110,7 +136,7 @@ public class CldsDictionaryService extends SecureServiceBase { /** * Rest Service that retrieves all CLDS dictionary in DB. - * + * * @return CldsDictionary List List of CldsDictionary available in DB */ public ResponseEntity> getAllDictionaryNames() { @@ -126,7 +152,7 @@ public class CldsDictionaryService extends SecureServiceBase { /** * Rest Service that retrieves all CLDS dictionary items in DB for a give * dictionary name. - * + * * @param dictionaryName dictionary name * @return CldsDictionaryItem list List of CLDS Dictionary items for a given * dictionary name @@ -150,4 +176,4 @@ public class CldsDictionaryService extends SecureServiceBase { util = utilP; } -} +} \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/it/CldsDictionaryServiceItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsDictionaryServiceItCase.java index d31d5a01e..5218c9255 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsDictionaryServiceItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsDictionaryServiceItCase.java @@ -5,6 +5,8 @@ * Copyright (C) 2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,7 +31,6 @@ import static org.junit.Assert.assertNotNull; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import java.io.IOException; import java.util.LinkedList; import java.util.List; @@ -63,23 +64,21 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class CldsDictionaryServiceItCase { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDictionaryServiceItCase.class); + private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDictionaryServiceItCase.class); @Autowired private CldsDictionaryService cldsDictionaryService; - private Authentication authentication; private CldsDictionary cldsDictionary; private CldsDictionaryItem cldsDictionaryItem; - private List authList = new LinkedList(); - private LoggingUtils util; + private List authList = new LinkedList<>(); + + private static final String DICTIONARY_NAME = "TestDictionary"; /** * Setup the variable before the tests execution. * - * @throws IOException - * In case of issues when opening the files */ @Before - public void setupBefore() throws IOException { + public void setupBefore() { authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read")); authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update")); authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read")); @@ -87,22 +86,19 @@ public class CldsDictionaryServiceItCase { authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*")); authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|read")); authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|update")); - authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList); + Authentication authentication = + new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList); SecurityContext securityContext = Mockito.mock(SecurityContext.class); Mockito.when(securityContext.getAuthentication()).thenReturn(authentication); - util = Mockito.mock(LoggingUtils.class); + LoggingUtils util = Mockito.mock(LoggingUtils.class); Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class)); cldsDictionaryService.setLoggingUtil(util); cldsDictionaryService.setSecurityContext(securityContext); - cldsDictionary = new CldsDictionary(); - - cldsDictionary.setDictionaryName("TestDictionary"); - ResponseEntity entity = cldsDictionaryService.createOrUpdateDictionary("TestDictionary", cldsDictionary); - cldsDictionary = (CldsDictionary) entity.getBody(); + cldsDictionary = cldsDictionaryService.createDictionary(DICTIONARY_NAME); cldsDictionaryItem = new CldsDictionaryItem(); cldsDictionaryItem.setDictElementShortName("TestDictionaryItemShortName"); @@ -110,23 +106,43 @@ public class CldsDictionaryServiceItCase { cldsDictionaryItem.setDictElementType("string"); cldsDictionaryItem.setDictionaryId(cldsDictionary.getDictionaryId()); cldsDictionaryItem.setDictElementDesc("TestDictionaryItemDesc"); - cldsDictionaryService.createOrUpdateDictionaryElements("TestDictionary", cldsDictionaryItem); + cldsDictionaryService.createOrUpdateDictionaryElements(DICTIONARY_NAME, cldsDictionaryItem); logger.info("Initial Clds Dictionary uploaded in DB:" + cldsDictionaryItem); } @Test - public void testCreateOrUpdateDictionary() throws Exception { - ResponseEntity responseEntity = cldsDictionaryService.createOrUpdateDictionary("TestDictionary", - cldsDictionary); - CldsDictionary dictionary = responseEntity.getBody(); + public void testCreateDictionaryFromString() { + String dictionaryName = "TestDefaultDictionary"; + CldsDictionary dictionary = cldsDictionaryService.createDictionary(dictionaryName); assertNotNull(dictionary); - logger.info("CLDS Dictionary is:" + dictionary); - assertEquals("TestDictionary", dictionary.getDictionaryName()); + logger.info("CLDS Default Dictionary is:" + dictionary); + assertEquals(dictionaryName, dictionary.getDictionaryName()); + } + + @Test + public void testCreateOrUpdateDictionaryUsedByFrontend() { + ResponseEntity responseEntity = + cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, null); + CldsDictionary dictionary1 = responseEntity.getBody(); + + responseEntity = cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, cldsDictionary); + CldsDictionary dictionary2 = responseEntity.getBody(); + + responseEntity = cldsDictionaryService.createOrUpdateDictionary(DICTIONARY_NAME, new CldsDictionary()); + CldsDictionary dictionary3 = responseEntity.getBody(); + + assertNotNull(dictionary1); + assertNotNull(dictionary2); + assertNotNull(dictionary3); + assertEquals(DICTIONARY_NAME, dictionary1.getDictionaryName()); + assertEquals(DICTIONARY_NAME, dictionary2.getDictionaryName()); + assertNotNull(dictionary3.getDictionaryName()); + assertEquals(DICTIONARY_NAME, dictionary3.getDictionaryName()); } @Test - public void testCreateOrUpdateDictionaryElements() throws Exception { + public void testCreateOrUpdateDictionaryElements() { cldsDictionaryItem = new CldsDictionaryItem(); cldsDictionaryItem.setDictElementShortName("TestDictionaryItemShortName1"); cldsDictionaryItem.setDictElementName("TestDictionaryItemName1"); @@ -135,7 +151,7 @@ public class CldsDictionaryServiceItCase { cldsDictionaryItem.setDictElementDesc("TestDictionaryItemDesc1"); ResponseEntity responseEntity = cldsDictionaryService - .createOrUpdateDictionaryElements("TestDictionary", cldsDictionaryItem); + .createOrUpdateDictionaryElements(DICTIONARY_NAME, cldsDictionaryItem); CldsDictionaryItem dictionaryItem = responseEntity.getBody(); assertNotNull(dictionaryItem); logger.info("CLDS Dictionary Item is:" + dictionaryItem); @@ -143,7 +159,7 @@ public class CldsDictionaryServiceItCase { } @Test - public void testGetAllDictionaryNames() throws Exception { + public void testGetAllDictionaryNames() { ResponseEntity> responseEntity = cldsDictionaryService.getAllDictionaryNames(); List dictionaries = responseEntity.getBody(); assertNotNull(dictionaries); @@ -151,11 +167,11 @@ public class CldsDictionaryServiceItCase { } @Test - public void testGetDictionaryElementsByName() throws Exception { + public void testGetDictionaryElementsByName() { ResponseEntity> responseEntity = cldsDictionaryService - .getDictionaryElementsByName("TestDictionary"); + .getDictionaryElementsByName(DICTIONARY_NAME); List dictionaryItems = responseEntity.getBody(); assertNotNull(dictionaryItems); logger.info("CLDS Dictionary Item LIst is:" + dictionaryItems); } -} +} \ No newline at end of file -- cgit 1.2.3-korg