Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#339 user develop language fix #342

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -61,14 +62,21 @@ public List<UserDevelopLanguageRes> updateUserDevelopLanguages(long userId) {
User user = userCommonService.getUserById(userId);
user.deleteAllDevelopLanguageTags();
var languageMap = userGithubUtil.updateUserDevelopLanguage(user.getRepositoryUrl());
var languages = sortDevelopLanguageMap(languageMap);
languages.forEach((key, value) -> {
var result = sortDevelopLanguageMap(languageMap);
result.ifPresentOrElse(language -> language.forEach((key, value) -> {
UserDevelopLanguageTag developLanguageTag = UserDevelopLanguageTag.builder()
.user(user)
.developLanguage(DevelopLanguage.of(key))
.byteSize(value)
.build();
userDevelopLanguageTagRepository.save(developLanguageTag);
}), () -> {
var userDevelopLanguageTag = UserDevelopLanguageTag.builder()
.user(user)
.developLanguage(DevelopLanguage.NULL)
.byteSize(0)
.build();
userDevelopLanguageTagRepository.save(userDevelopLanguageTag);
});
List<UserDevelopLanguageRes> response = new ArrayList<>();
for (var developLanguage : user.getUserDevelopLanguageTags()) {
Expand Down Expand Up @@ -114,12 +122,17 @@ private void validateWantedJobDuplicate(List<WantedJob> wantedJobs) {
}
}

private Map<String, Integer> sortDevelopLanguageMap(Map<String, Integer> languageMap){
private Optional<Map<String, Integer>> sortDevelopLanguageMap(
Map<String, Integer> languageMap) {
if (languageMap.isEmpty()) {
return Optional.empty();
}
List<String> languageKeys = new ArrayList<>(languageMap.keySet());
languageKeys.sort((o1, o2) -> languageMap.get(o2).compareTo(languageMap.get(o1)));
Map<String, Integer> result = new LinkedHashMap<>();
languageKeys.subList(0, 3).forEach(key -> result.put(key, languageMap.get(key)));
return result;
int length = Math.min(languageMap.size(), 3);
languageKeys.subList(0, length).forEach(key -> result.put(key, languageMap.get(key)));
return Optional.of(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public enum DevelopLanguage {
OBJECT_C("Objective-C",
"https://github.com/Open-Eye-Im-Developer/MoGakGo-BE/assets/85854384/c99bf59a-716f-4dfd-9414-005133ce9c97"),
ETC("ETC",
"https://github.com/Open-Eye-Im-Developer/MoGakGo-BE/assets/85854384/b489846c-bcbf-4575-8bfc-7788da4b9ba0");
"https://github.com/Open-Eye-Im-Developer/MoGakGo-BE/assets/85854384/b489846c-bcbf-4575-8bfc-7788da4b9ba0"),
NULL("NULL",
"https://github.com/Open-Eye-Im-Developer/MoGakGo-BE/assets/85854384/8f0c33f3-62ef-4379-ba6f-eb1eb0ef2471");

private final String language;
private final String imageUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public UserGithubUtil(@Value("${auth.github-access-token}") String accessToken)

public Map<String, Integer> updateUserDevelopLanguage(String repositoryUrl) {
var repositoriesData = getRepositoriesData(repositoryUrl);
if (repositoriesData == null) {
if (repositoriesData == null || repositoriesData.isEmpty()) {
return Map.of();
}
var monoList = repositoriesData.stream().map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@ExtendWith(MockitoExtension.class)
@DisplayName("μ„œλΉ„μŠ€ ν…ŒμŠ€νŠΈ: UserService")
class UserServiceTest {

@InjectMocks
private UserService userService;
@Mock
Expand Down Expand Up @@ -131,6 +132,19 @@ void init() {
.contains(300, 200, 100);
}

@Test
void μœ μ €_κ°œλ°œμ–Έμ–΄_μ—…λ°μ΄νŠΈ_λ ˆν¬μ§€ν† λ¦¬κ°€_μ‘΄μž¬ν•˜μ§€_μ•ŠλŠ”_경우() {
// Arrange
var expectedUserId = 1L;
when(userCommonService.getUserById(expectedUserId)).thenReturn(user);
when(userGithubUtil.updateUserDevelopLanguage(any(String.class))).thenReturn(Map.of());
// Act
var actualResult = userService.updateUserDevelopLanguages(expectedUserId);
// Assert
assertThat(actualResult).isNotNull().hasSize(1).extracting("language")
.contains(DevelopLanguage.NULL);
}

@Test
void μœ μ €_ν”„λ‘œν•„_쑰회() {
// Arrange
Expand Down Expand Up @@ -181,7 +195,7 @@ void init() {
}

@Test
void μœ μ €_μž”λ””λ ₯_쑰회(){
void μœ μ €_μž”λ””λ ₯_쑰회() {
// Arrange
var expectedUserId = 1L;
when(userCommonService.getUserById(expectedUserId)).thenReturn(user);
Expand Down
Loading