Last active
August 4, 2025 14:33
-
-
Save mizanRahman/eb47536b330e24056df2a63756dcfbf2 to your computer and use it in GitHub Desktop.
Live Coding - Dmytro Bilyk: Secret Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.conduct.interview.auto; | |
import io.micrometer.common.util.StringUtils; | |
import org.jetbrains.annotations.NotNull; | |
import java.util.Random; | |
import java.util.UUID; | |
public class SecretService { | |
private final SecretStorageDao secretStorageDao; | |
public SecretService(SecretStorageDao secretStorageDao) { | |
this.secretStorageDao = secretStorageDao; | |
} | |
public String store(String key, String secret) { | |
validateKey(key, secretStorageDao); | |
validateSecret(secret); | |
secretStorageDao.store(key, secret); | |
return key; | |
} | |
public Integer store(Integer key, String secret) { | |
// validateKey(key, secretStorageDao); | |
validateSecret(secret); | |
secretStorageDao.store(key, secret); | |
return key; | |
} | |
public String retrieve(String key) { | |
validateKeyIsBlank(key); | |
if (!secretStorageDao.keyExists(key)) { | |
throw new IllegalArgumentException("Secret key does not exist"); | |
} | |
return secretStorageDao.retrieve(key); | |
} | |
public String store(String secret) { | |
String key = UUID.randomUUID().toString().substring(0, 10); | |
secretStorageDao.store(key, secret); | |
return key; | |
} | |
private static void validateSecret(String secret) { | |
if (StringUtils.isBlank(secret)) { | |
throw new IllegalArgumentException("Can't store the secret. Secret is not valid"); | |
} | |
} | |
private static void validateKey(String key, SecretStorageDao secretStorageDao) { | |
validateKeyIsBlank(key); | |
if (key.length() > 20) { | |
throw new IllegalArgumentException("Can't store the secret. Secret length exceeds 20"); | |
} | |
if (secretStorageDao.keyExists(key)) { | |
throw new IllegalArgumentException("Secret key already exists"); | |
} | |
} | |
private static void validateKeyIsBlank(String key) { | |
if (StringUtils.isBlank(key)) { | |
throw new IllegalArgumentException("Can't store the secret. Key is null"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.conduct.interview.auto; | |
public interface SecretStorageDao { | |
void store(String key, String secret); | |
String retrieve(String key); | |
boolean keyExists(String key); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.conduct.interview.auto; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class SecretStorageIntegerMapDaoImpl implements SecretStorageDao { | |
private final Map<Integer, String> storage = new ConcurrentHashMap<>(); | |
@Override | |
public void store(Integer key, String secret) { | |
this.storage.put(key, secret); | |
} | |
@Override | |
public String retrieve(Integer key) { | |
return this.storage.get(key); | |
} | |
@Override | |
public boolean keyExists(Integer key) { | |
return this.storage.containsKey(key); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.conduct.interview.auto; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class SecretStorageStringMapDaoImpl implements SecretStorageDao { | |
private final Map<String, String> storage = new ConcurrentHashMap(); | |
@Override | |
public void store(String key, String secret) { | |
this.storage.put(key, secret); | |
} | |
@Override | |
public String retrieve(String key) { | |
return this.storage.get(key); | |
} | |
@Override | |
public boolean keyExists(String key) { | |
return this.storage.containsKey(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment