|
| 1 | +package io.ipinfo.spring; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import io.ipinfo.api.IPinfoCore; |
| 7 | +import io.ipinfo.api.errors.RateLimitedException; |
| 8 | +import io.ipinfo.api.model.ASN; |
| 9 | +import io.ipinfo.api.model.Geo; |
| 10 | +import io.ipinfo.api.model.IPResponseCore; |
| 11 | +import io.ipinfo.spring.strategies.attribute.AttributeStrategy; |
| 12 | +import io.ipinfo.spring.strategies.interceptor.InterceptorStrategy; |
| 13 | +import io.ipinfo.spring.strategies.ip.IPStrategy; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.DisplayName; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.mockito.InjectMocks; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 22 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 23 | + |
| 24 | +@ExtendWith(MockitoExtension.class) |
| 25 | +class IPinfoCoreSpringTest { |
| 26 | + |
| 27 | + @Mock |
| 28 | + private IPinfoCore mockIPinfoClient; |
| 29 | + |
| 30 | + @Mock |
| 31 | + private AttributeStrategy mockAttributeStrategy; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private IPStrategy mockIpStrategy; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private InterceptorStrategy mockInterceptorStrategy; |
| 38 | + |
| 39 | + @InjectMocks |
| 40 | + private IPinfoCoreSpring ipinfoSpring; |
| 41 | + |
| 42 | + private MockHttpServletRequest request; |
| 43 | + private MockHttpServletResponse response; |
| 44 | + private Object handler; |
| 45 | + |
| 46 | + private IPResponseCore dummyIPResponse; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() { |
| 50 | + request = new MockHttpServletRequest(); |
| 51 | + response = new MockHttpServletResponse(); |
| 52 | + handler = new Object(); |
| 53 | + |
| 54 | + new IPResponseCore( |
| 55 | + "8.8.8.8", |
| 56 | + new Geo( |
| 57 | + "Mountain View", |
| 58 | + "California", |
| 59 | + "CA", |
| 60 | + "United States", |
| 61 | + "US", |
| 62 | + "North America", |
| 63 | + "NA", |
| 64 | + 37.4056, |
| 65 | + -122.0775, |
| 66 | + "America/Los_Angeles", |
| 67 | + "94043" |
| 68 | + ), |
| 69 | + new ASN("AS15169", "Google LLC", "google.com", "", "hosting"), |
| 70 | + false, |
| 71 | + true, |
| 72 | + true, |
| 73 | + false, |
| 74 | + false |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("should skip processing if interceptorStrategy returns false") |
| 80 | + void preHandle_shouldSkipIfInterceptorStrategyFalse() throws Exception { |
| 81 | + when(mockInterceptorStrategy.shouldRun(request)).thenReturn(false); |
| 82 | + |
| 83 | + boolean result = ipinfoSpring.preHandle(request, response, handler); |
| 84 | + |
| 85 | + assertTrue(result, "preHandle should return true to continue chain"); |
| 86 | + // Verify that no other strategies were called if shouldRun returned false |
| 87 | + verify(mockInterceptorStrategy).shouldRun(request); |
| 88 | + verifyNoInteractions( |
| 89 | + mockAttributeStrategy, |
| 90 | + mockIpStrategy, |
| 91 | + mockIPinfoClient |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @DisplayName( |
| 97 | + "should skip processing if attributeStrategy already has attribute" |
| 98 | + ) |
| 99 | + void preHandle_shouldSkipIfHasCoreAttribute() throws Exception { |
| 100 | + when(mockInterceptorStrategy.shouldRun(request)).thenReturn(true); |
| 101 | + when(mockAttributeStrategy.hasCoreAttribute(request)).thenReturn(true); |
| 102 | + |
| 103 | + boolean result = ipinfoSpring.preHandle(request, response, handler); |
| 104 | + |
| 105 | + assertTrue(result, "preHandle should return true to continue chain"); |
| 106 | + verify(mockInterceptorStrategy).shouldRun(request); |
| 107 | + verify(mockAttributeStrategy).hasCoreAttribute(request); |
| 108 | + // Verify no IP lookup or storage occurred |
| 109 | + verifyNoInteractions(mockIpStrategy, mockIPinfoClient); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @DisplayName("should skip processing if IPStrategy returns null IP") |
| 114 | + void preHandle_shouldSkipIfIpIsNull() throws Exception { |
| 115 | + when(mockInterceptorStrategy.shouldRun(request)).thenReturn(true); |
| 116 | + when(mockAttributeStrategy.hasCoreAttribute(request)).thenReturn(false); |
| 117 | + when(mockIpStrategy.getIPAddress(request)).thenReturn(null); |
| 118 | + |
| 119 | + boolean result = ipinfoSpring.preHandle(request, response, handler); |
| 120 | + |
| 121 | + assertTrue(result, "preHandle should return true to continue chain"); |
| 122 | + verify(mockInterceptorStrategy).shouldRun(request); |
| 123 | + verify(mockAttributeStrategy).hasCoreAttribute(request); |
| 124 | + verify(mockIpStrategy).getIPAddress(request); |
| 125 | + // Verify no IP lookup or storage occurred |
| 126 | + verifyNoInteractions(mockIPinfoClient); |
| 127 | + verify(mockAttributeStrategy, never()).storeCoreAttribute(any(), any()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @DisplayName( |
| 132 | + "should perform IP lookup and store attribute if all conditions met" |
| 133 | + ) |
| 134 | + void preHandle_shouldProcessAndStore() throws Exception { |
| 135 | + String testIp = "8.8.8.8"; |
| 136 | + when(mockInterceptorStrategy.shouldRun(request)).thenReturn(true); |
| 137 | + when(mockAttributeStrategy.hasCoreAttribute(request)).thenReturn(false); |
| 138 | + when(mockIpStrategy.getIPAddress(request)).thenReturn(testIp); |
| 139 | + when(mockIPinfoClient.lookupIP(testIp)).thenReturn(dummyIPResponse); |
| 140 | + |
| 141 | + boolean result = ipinfoSpring.preHandle(request, response, handler); |
| 142 | + |
| 143 | + assertTrue(result, "preHandle should return true to continue chain"); |
| 144 | + verify(mockInterceptorStrategy).shouldRun(request); |
| 145 | + verify(mockAttributeStrategy).hasCoreAttribute(request); |
| 146 | + verify(mockIpStrategy).getIPAddress(request); |
| 147 | + verify(mockIPinfoClient).lookupIP(testIp); |
| 148 | + verify(mockAttributeStrategy).storeCoreAttribute( |
| 149 | + request, |
| 150 | + dummyIPResponse |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @DisplayName("should rethrow RateLimitedException during lookup") |
| 156 | + void preHandle_shouldRethrowRateLimitedException() throws Exception { |
| 157 | + String testIp = "invalid.ip"; |
| 158 | + when(mockInterceptorStrategy.shouldRun(request)).thenReturn(true); |
| 159 | + when(mockAttributeStrategy.hasCoreAttribute(request)).thenReturn(false); |
| 160 | + when(mockIpStrategy.getIPAddress(request)).thenReturn(testIp); |
| 161 | + // Simulate a RateLimitedException during lookup |
| 162 | + when(mockIPinfoClient.lookupIP(testIp)).thenThrow( |
| 163 | + new RateLimitedException() |
| 164 | + ); |
| 165 | + |
| 166 | + assertThrows(RateLimitedException.class, () -> |
| 167 | + ipinfoSpring.preHandle(request, response, handler) |
| 168 | + ); |
| 169 | + |
| 170 | + verify(mockInterceptorStrategy).shouldRun(request); |
| 171 | + verify(mockAttributeStrategy).hasCoreAttribute(request); |
| 172 | + verify(mockIpStrategy).getIPAddress(request); |
| 173 | + verify(mockIPinfoClient).lookupIP(testIp); |
| 174 | + verify(mockAttributeStrategy, never()).storeCoreAttribute(any(), any()); |
| 175 | + } |
| 176 | +} |
0 commit comments