|
| 1 | +package com.spotify.requests; |
| 2 | + |
| 3 | +import com.spotify.exceptions.SpotifyUrlParserException; |
| 4 | +import com.spotify.requests.testclasses.TestFieldRequestClass1; |
| 5 | +import com.spotify.requests.testclasses.TestRequestClass1; |
| 6 | +import com.spotify.requests.testclasses.TestSubRequestClass1; |
| 7 | +import com.spotify.requests.testclasses.TestSubRequestClass2; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +public class SpotifyAnnotationTest { |
| 13 | + |
| 14 | + |
| 15 | + /** |
| 16 | + * Tests classes that don't have @SpotifyRequest annotation |
| 17 | + */ |
| 18 | + @Test |
| 19 | + public void spotifyRequestAnnotationTest1() { |
| 20 | + TestRequestClass1 trc = new TestRequestClass1(); |
| 21 | + trc.setField1("value1"); |
| 22 | + String url = trc.buildRequestUrl(); |
| 23 | + |
| 24 | + assertNull(url); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * Tests class fields with @SpotifySubRequest |
| 30 | + * Ensures that field that are not annotated do not add to url |
| 31 | + */ |
| 32 | + @Test |
| 33 | + public void spotifySubRequestAnnotationTest1() { |
| 34 | + TestSubRequestClass1 trc = new TestSubRequestClass1(); |
| 35 | + |
| 36 | + trc.setField1("subrequest1"); |
| 37 | + trc.setField2("subrequest2"); |
| 38 | + trc.setField3("failsubrequest"); |
| 39 | + |
| 40 | + String url = trc.buildRequestUrl(); |
| 41 | + |
| 42 | + assertEquals("url/subrequest1/subrequest2", url); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Tests class fields with @SpotifySubRequest |
| 48 | + * Should fail to create url since a field annotated with subRequest should not be none |
| 49 | + */ |
| 50 | + @Test |
| 51 | + public void spotifySubRequestAnnotationTest2() { |
| 52 | + TestSubRequestClass1 trc = new TestSubRequestClass1(); |
| 53 | + |
| 54 | + trc.setField1("subrequest1"); |
| 55 | + trc.setField2(null); |
| 56 | + |
| 57 | + String url = trc.buildRequestUrl(); |
| 58 | + |
| 59 | + assertNull(url); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * Tests class fields with @SpotifySubRequest |
| 65 | + * Should fail to create url since subRequest fields must be type String |
| 66 | + */ |
| 67 | + @Test |
| 68 | + public void spotifySubRequestAnnotationTest3() { |
| 69 | + TestSubRequestClass2 trc = new TestSubRequestClass2(); |
| 70 | + |
| 71 | + trc.setIntField(6); |
| 72 | + |
| 73 | + SpotifyUrlParserException thrown = assertThrows(SpotifyUrlParserException.class, trc::buildRequestUrl); |
| 74 | + |
| 75 | + assertNotNull(thrown); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * Tests class fields with @SpotifyRequestField |
| 81 | + * Ensures that default values of field are ignored |
| 82 | + * and non labelled fields |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void spotifyFieldRequestAnnotationTest1() { |
| 86 | + TestFieldRequestClass1 trc = new TestFieldRequestClass1(); |
| 87 | + |
| 88 | + trc.setField1("value1"); |
| 89 | + trc.setField2("value2"); |
| 90 | + trc.setField3(null); |
| 91 | + |
| 92 | + |
| 93 | + String url = trc.buildRequestUrl(); |
| 94 | + |
| 95 | + assertEquals("url?field1=value1&FIELD2=value2", url); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments