UserService { public void checkKeyUniqueness(String key, String value, boolean isEncrypted) { if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) { ProjectLogger.log( "UserService:checkKeyUniqueness: Key or value is null. key = " + key + " value= " + value, LoggerEnum.ERROR.name()); return; } String val = value; if (isEncrypted) { try { val = getEncryptionService().encryptData(val); } catch (Exception e) { ProjectLogger.log( "UserService:checkKeyUniqueness: Exception occurred with error message = " + e.getMessage(), e); } } Response result = getCassandraOperation().getRecordsByIndexedProperty(userDb.getKeySpace(), userDb.getTableName(), key, val); List<Map<String, Object>> userMapList = (List<Map<String, Object>>) result.get(JsonKey.RESPONSE); if (!userMapList.isEmpty()) { ResponseCode responseCode = null; if (JsonKey.EMAIL.equals(key)) { responseCode = ResponseCode.emailInUse; } else if (JsonKey.PHONE.equals(key)) { responseCode = ResponseCode.PhoneNumberInUse; } ProjectCommonException.throwClientErrorException(responseCode, null); } }  void checkKeyUniqueness(String key, String value, boolean isEncrypted);  }
@Test(expected = Test.None.class) public void testCheckKeyUniquenessWhenKeyBlank(){ try{ when(cassandraOperation.getRecordsByIndexedProperty(userDb.getKeySpace(), userDb.getTableName(), "", "")).thenReturn(new Response()); userService.checkKeyUniqueness("","",false); }catch (Exception e) { Assert.assertTrue(false); } }
@Test() public void testCheckKeyUniquenessWhenKeyValueIsUnique(){ try{ Response response=new Response(); List<Map<String, Object>> userMapList =new ArrayList<>(); response.put(JsonKey.RESPONSE,userMapList); when(cassandraOperation.getRecordsByIndexedProperty(userDb.getKeySpace(), userDb.getTableName(), "key", "value")).thenReturn(response); userService.checkKeyUniqueness("key","value",false); }catch (Exception e) { Assert.assertTrue(false); } }
@Test() public void testCheckKeyUniquenessWhenEmailValueIsNotUnique(){ try{ Response response=new Response(); List<Map<String, Object>> userMapList =new ArrayList<>(); Map<String,Object>map=new HashMap<>(); map.put(JsonKey.NAME,"NAME"); userMapList.add(map); response.put(JsonKey.RESPONSE,userMapList); when(cassandraOperation.getRecordsByIndexedProperty(userDb.getKeySpace(), userDb.getTableName(), "email", "valueNotUnique")).thenReturn(response); userService.checkKeyUniqueness("email","valueNotUnique",false); }catch (Exception e) { Assert.assertEquals("Email already exists.",e.getMessage()); } }
@Test() public void testCheckKeyUniquenessWhenPhoneValueIsNotUnique(){ try{ Response response=new Response(); List<Map<String, Object>> userMapList =new ArrayList<>(); Map<String,Object>map=new HashMap<>(); map.put(JsonKey.NAME,"NAME"); userMapList.add(map); response.put(JsonKey.RESPONSE,userMapList); when(cassandraOperation.getRecordsByIndexedProperty(userDb.getKeySpace(), userDb.getTableName(), "phone", "valueNotUnique")).thenReturn(response); userService.checkKeyUniqueness("phone","valueNotUnique",false); }catch (Exception e) { Assert.assertEquals("Phone already in use. Please provide different phone number.",e.getMessage()); } }
@Test() public void testCheckKeyUniquenessWhenPhoneValueIsUnique(){ try{ Response response=new Response(); List<Map<String, Object>> userMapList =new ArrayList<>(); response.put(JsonKey.RESPONSE,userMapList); when(encryptionService.encryptData("valueUnique")).thenReturn("valueUnique"); when(cassandraOperation.getRecordsByIndexedProperty(userDb.getKeySpace(), userDb.getTableName(), "phone", "valueUnique")).thenReturn(response); userService.checkKeyUniqueness("phone","valueUnique",true); }catch (Exception e) { Assert.assertTrue(false); } }