-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #105 악기 매물 설명(
description
)에 링크(URL)가 포함되어 있는 경우 예외가 발생하도록 검증 추가
- Loading branch information
Showing
7 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/ajou/hertz/domain/instrument/entity/InstrumentDescription.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.ajou.hertz.domain.instrument.entity; | ||
|
||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import com.ajou.hertz.domain.instrument.exception.UnexpectedLinkInInstrumentDescriptionException; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@Embeddable | ||
public class InstrumentDescription { | ||
|
||
@Column(length = 1000, nullable = false) | ||
private String description; | ||
|
||
public InstrumentDescription(String description) { | ||
validateDescription(description); | ||
this.description = description; | ||
} | ||
|
||
private void validateDescription(String description) { | ||
Matcher descriptionUrlMatcher = Pattern.compile("http://|https://|www\\.").matcher(description); | ||
if (descriptionUrlMatcher.find()) { | ||
throw new UnexpectedLinkInInstrumentDescriptionException(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (!(obj instanceof InstrumentDescription that)) { | ||
return false; | ||
} | ||
return Objects.equals(this.getDescription(), that.getDescription()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(getDescription()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...jou/hertz/domain/instrument/exception/UnexpectedLinkInInstrumentDescriptionException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ajou.hertz.domain.instrument.exception; | ||
|
||
import com.ajou.hertz.common.exception.BadRequestException; | ||
import com.ajou.hertz.common.exception.constant.CustomExceptionType; | ||
|
||
public class UnexpectedLinkInInstrumentDescriptionException extends BadRequestException { | ||
|
||
public UnexpectedLinkInInstrumentDescriptionException() { | ||
super(CustomExceptionType.UNEXPECTED_LINK_IN_INSTRUMENT_DESCRIPTION); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/ajou/hertz/unit/domain/instrument/entity/InstrumentDescriptionTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.ajou.hertz.unit.domain.instrument.entity; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import com.ajou.hertz.domain.instrument.entity.InstrumentDescription; | ||
import com.ajou.hertz.domain.instrument.exception.UnexpectedLinkInInstrumentDescriptionException; | ||
|
||
class InstrumentDescriptionTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"The violin is a small-bodied string instrument with a long neck, known for its beautiful melodies.", | ||
"A drum set consists of various percussion instruments and cymbals, providing powerful rhythms.", | ||
"The flute is a lightweight woodwind made of metal, known for its smooth and clear sound.", | ||
"An electronic keyboard incorporates various sounds and rhythms, allowing for versatile performances.", | ||
"The harmonica is a small wind instrument that produces sound by blowing air through it, offering a unique musical experience." | ||
}) | ||
void 악기_설명에_링크가_존재하지_않는다면_정상적으로_객체가_생성된다(String description) { | ||
// given | ||
|
||
// when | ||
InstrumentDescription instrumentDescription = new InstrumentDescription(description); | ||
|
||
// then | ||
assertThat(instrumentDescription).isNotNull(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"Keyboard instrument striking strings with hammers. Learn more at Musician's Friend: https://www.musiciansfriend.com/keyboards-midi/pianos", | ||
"Stringed instrument played with a bow, high-pitched. More info at Violinist: http://www.violinist.com/", | ||
"Stringed instrument for various genres, strummed or plucked. Details at Guitar Center: www.guitarcenter.com/Acoustic-Guitars.gc", | ||
"Woodwind instrument, sound by blowing air across the mouthpiece. Additional info at Flute World: https://fluteworld.com/", | ||
"Percussion instruments struck by sticks or hands. Explore more at Drumming.com: http://drumming.com/" | ||
}) | ||
void 악기_설명에_링크가_포함되었다면_예외가_발생한다(String description) { | ||
// given | ||
|
||
// when | ||
Throwable ex = catchThrowable(() -> new InstrumentDescription(description)); | ||
|
||
// then | ||
assertThat(ex).isInstanceOf(UnexpectedLinkInInstrumentDescriptionException.class); | ||
} | ||
} |