|
| 1 | +package io.snyk.languageserver.protocolextension; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.net.URI; |
| 6 | +import java.net.URISyntaxException; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import io.snyk.eclipse.plugin.utils.UriUtils; |
| 14 | + |
| 15 | +class SnykUriUtilsTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void getDecodedParam_Returns_Parameter_Value_If_Present_In_Query_String() |
| 19 | + throws URISyntaxException { |
| 20 | + String query = "product=Snyk+Code&issueId=7642f506c568056a7090d3ceb7b3c2e0&action=showInDetailPanel"; |
| 21 | + URI uriWithQuery = new URI("snyk://path/to/resource?" + query); |
| 22 | + |
| 23 | + var result = UriUtils.getDecodedParam(uriWithQuery, "issueId"); |
| 24 | + assertEquals("7642f506c568056a7090d3ceb7b3c2e0", result); |
| 25 | + |
| 26 | + result = UriUtils.getDecodedParam(uriWithQuery, "action"); |
| 27 | + assertEquals("showInDetailPanel", result); |
| 28 | + |
| 29 | + result = UriUtils.getDecodedParam(uriWithQuery, "product"); |
| 30 | + assertEquals("Snyk Code", result); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void getDecodedParamNullQuery() throws URISyntaxException { |
| 35 | + URI uri = new URI( |
| 36 | + "snyk:///test_repos/project-with-vulns/routes/vulnCodeSnippet.ts"); |
| 37 | + String paramName = "product"; |
| 38 | + |
| 39 | + assertNull(UriUtils.getDecodedParam(uri, paramName)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void getDecodedParamEmptyQuery() throws URISyntaxException { |
| 44 | + URI uri = new URI( |
| 45 | + "snyk:///test_repos/project-with-vulns/routes/vulnCodeSnippet.ts?product="); |
| 46 | + String paramName = "product"; |
| 47 | + |
| 48 | + assertNull(UriUtils.getDecodedParam(uri, paramName)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void getDecodedParamMissingParameter() throws URISyntaxException { |
| 53 | + URI uri = new URI( |
| 54 | + "snyk:///test_repos/project-with-vulns/routes/vulnCodeSnippet.ts?other=parameter"); |
| 55 | + String paramName = "product"; |
| 56 | + |
| 57 | + assertNull(UriUtils.getDecodedParam(uri, paramName)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void parseQueryString_Returns_Parameters_In_Query_String() |
| 62 | + throws Exception { |
| 63 | + String query = "product=Snyk+Code&issueId=7642f506c568056a7090d3ceb7b3c2e0&action=showInDetailPanel"; |
| 64 | + URI uriWithQuery = new URI("snyk://path/to/resource?" + query); |
| 65 | + |
| 66 | + var result = UriUtils.getQueryParameters(uriWithQuery.getQuery()); |
| 67 | + assertEquals(3, result.size()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void parseQueryString_Returns_Empty_Map_If_Query_String_Is_Empty() |
| 72 | + throws URISyntaxException { |
| 73 | + var result = UriUtils.getQueryParameters(null); |
| 74 | + assertTrue(result.isEmpty()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void queryParametersEmptyQueryString() { |
| 79 | + Map<String, String> paramMap = UriUtils.getQueryParameters(""); |
| 80 | + |
| 81 | + assertTrue(paramMap.isEmpty()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void queryParametersNullQueryString() { |
| 86 | + Map<String, String> paramMap = UriUtils.getQueryParameters(null); |
| 87 | + |
| 88 | + assertTrue(paramMap.isEmpty()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void queryParametersSingleParameter() { |
| 93 | + String queryString = "product=Snyk+Code"; |
| 94 | + Map<String, String> expectedParamMap = Collections |
| 95 | + .singletonMap("product", "Snyk Code"); |
| 96 | + |
| 97 | + Map<String, String> paramMap = UriUtils.getQueryParameters(queryString); |
| 98 | + |
| 99 | + assertEquals(expectedParamMap, paramMap); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void queryParametersMultipleParameters() { |
| 104 | + String queryString = "product=Snyk+Code&other=parameter"; |
| 105 | + Map<String, String> expectedParamMap = new HashMap<>(); |
| 106 | + expectedParamMap.put("product", "Snyk Code"); |
| 107 | + expectedParamMap.put("other", "parameter"); |
| 108 | + |
| 109 | + |
| 110 | + Map<String, String> paramMap = UriUtils.getQueryParameters(queryString); |
| 111 | + |
| 112 | + assertEquals(expectedParamMap, paramMap); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments