|
15 | 15 | import org.openapitools.codegen.testutils.ConfigAssert; |
16 | 16 | import org.testng.Assert; |
17 | 17 | import org.testng.annotations.BeforeMethod; |
| 18 | +import org.testng.annotations.DataProvider; |
| 19 | +import org.testng.annotations.DataProvider; |
18 | 20 | import org.testng.annotations.Test; |
19 | 21 |
|
20 | 22 | import java.io.File; |
@@ -1586,4 +1588,198 @@ public void useQuarkusSecurityAnnotationsNotProcessedForNonQuarkusLibrary() { |
1586 | 1588 | // written back as a boolean — the key holds the raw Object we put in, not false |
1587 | 1589 | Assert.assertNotEquals(false, codegen.additionalProperties().get(USE_QUARKUS_SECURITY_ANNOTATIONS)); |
1588 | 1590 | } |
| 1591 | + |
| 1592 | + /** |
| 1593 | + * Parameterized test covering all @Authenticated annotation scenarios for Quarkus + OAuth2. |
| 1594 | + * Each row: (spec path, interfaceOnly, expected count of @io.quarkus.security.Authenticated). |
| 1595 | + */ |
| 1596 | + @DataProvider(name = "quarkusOAuth2AuthenticatedCases") |
| 1597 | + public Object[][] quarkusOAuth2AuthenticatedCases() { |
| 1598 | + return new Object[][] { |
| 1599 | + // single OAuth2 flow, no scopes → @Authenticated |
| 1600 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-no-scopes.yaml", true, 1}, |
| 1601 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-no-scopes.yaml", false, 1}, |
| 1602 | + // single OAuth2 flow, non-empty scopes → no @Authenticated |
| 1603 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-with-scopes.yaml", true, 0}, |
| 1604 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-with-scopes.yaml", false, 0}, |
| 1605 | + // multiple OAuth2 flows, all no scopes → @Authenticated exactly once (no per-flow duplication) |
| 1606 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-multi-flow-no-scopes.yaml", true, 1}, |
| 1607 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-multi-flow-no-scopes.yaml", false, 1}, |
| 1608 | + // OR: one scheme no-scope + one scheme scoped → one op gets @Authenticated, scoped-only op gets none |
| 1609 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-or-empty-and-scoped.yaml", true, 1}, |
| 1610 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-oauth2-or-empty-and-scoped.yaml", false, 1}, |
| 1611 | + }; |
| 1612 | + } |
| 1613 | + |
| 1614 | + @Test(dataProvider = "quarkusOAuth2AuthenticatedCases") |
| 1615 | + public void quarkusEmitsAuthenticatedAnnotationForOAuth2(String specPath, boolean interfaceOnly, int expectedCount) throws Exception { |
| 1616 | + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1617 | + output.deleteOnExit(); |
| 1618 | + |
| 1619 | + final OpenAPI openAPI = new OpenAPIParser() |
| 1620 | + .readLocation(specPath, null, new ParseOptions()).getOpenAPI(); |
| 1621 | + |
| 1622 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 1623 | + codegen.setLibrary(QUARKUS_LIBRARY); |
| 1624 | + codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly); |
| 1625 | + codegen.additionalProperties().put(USE_JAKARTA_EE, true); |
| 1626 | + |
| 1627 | + final ClientOptInput input = new ClientOptInput() |
| 1628 | + .openAPI(openAPI) |
| 1629 | + .config(codegen); |
| 1630 | + |
| 1631 | + final DefaultGenerator generator = new DefaultGenerator(); |
| 1632 | + final List<File> files = generator.opts(input).generate(); |
| 1633 | + |
| 1634 | + validateJavaSourceFiles(files); |
| 1635 | + |
| 1636 | + TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java"); |
| 1637 | + final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java")); |
| 1638 | + Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), expectedCount); |
| 1639 | + } |
| 1640 | + |
| 1641 | + /** |
| 1642 | + * Parameterized test covering all @Authenticated annotation scenarios for Quarkus + httpBasic. |
| 1643 | + * Each row: (interfaceOnly). |
| 1644 | + */ |
| 1645 | + @DataProvider(name = "quarkusHttpBasicCases") |
| 1646 | + public Object[][] quarkusHttpBasicCases() { |
| 1647 | + return new Object[][] { |
| 1648 | + {true}, // interface-only |
| 1649 | + {false}, // concrete stub |
| 1650 | + }; |
| 1651 | + } |
| 1652 | + |
| 1653 | + @Test(dataProvider = "quarkusHttpBasicCases") |
| 1654 | + public void quarkusEmitsAuthenticatedAnnotationForHttpBasic(boolean interfaceOnly) throws Exception { |
| 1655 | + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1656 | + output.deleteOnExit(); |
| 1657 | + |
| 1658 | + final OpenAPI openAPI = new OpenAPIParser() |
| 1659 | + .readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-http-basic.yaml", null, new ParseOptions()).getOpenAPI(); |
| 1660 | + |
| 1661 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 1662 | + codegen.setLibrary(QUARKUS_LIBRARY); |
| 1663 | + codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly); |
| 1664 | + codegen.additionalProperties().put(USE_JAKARTA_EE, true); |
| 1665 | + |
| 1666 | + final DefaultGenerator generator = new DefaultGenerator(); |
| 1667 | + final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate(); |
| 1668 | + |
| 1669 | + validateJavaSourceFiles(files); |
| 1670 | + |
| 1671 | + TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java"); |
| 1672 | + final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java")); |
| 1673 | + Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1); |
| 1674 | + } |
| 1675 | + |
| 1676 | + /** |
| 1677 | + * Parameterized test covering all @Authenticated annotation scenarios for Quarkus + http bearer. |
| 1678 | + * Each row: (interfaceOnly). |
| 1679 | + */ |
| 1680 | + @DataProvider(name = "quarkusHttpBearerCases") |
| 1681 | + public Object[][] quarkusHttpBearerCases() { |
| 1682 | + return new Object[][] { |
| 1683 | + {true}, |
| 1684 | + {false}, |
| 1685 | + }; |
| 1686 | + } |
| 1687 | + |
| 1688 | + @Test(dataProvider = "quarkusHttpBearerCases") |
| 1689 | + public void quarkusEmitsAuthenticatedAnnotationForHttpBearer(boolean interfaceOnly) throws Exception { |
| 1690 | + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1691 | + output.deleteOnExit(); |
| 1692 | + |
| 1693 | + final OpenAPI openAPI = new OpenAPIParser() |
| 1694 | + .readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-http-bearer.yaml", null, new ParseOptions()).getOpenAPI(); |
| 1695 | + |
| 1696 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 1697 | + codegen.setLibrary(QUARKUS_LIBRARY); |
| 1698 | + codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly); |
| 1699 | + codegen.additionalProperties().put(USE_JAKARTA_EE, true); |
| 1700 | + |
| 1701 | + final DefaultGenerator generator = new DefaultGenerator(); |
| 1702 | + final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate(); |
| 1703 | + |
| 1704 | + validateJavaSourceFiles(files); |
| 1705 | + |
| 1706 | + TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java"); |
| 1707 | + final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java")); |
| 1708 | + Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1); |
| 1709 | + } |
| 1710 | + |
| 1711 | + /** |
| 1712 | + * Parameterized test covering all @Authenticated annotation scenarios for Quarkus + api key. |
| 1713 | + * Each row: (interfaceOnly). |
| 1714 | + */ |
| 1715 | + @DataProvider(name = "quarkusApiKeyCases") |
| 1716 | + public Object[][] quarkusApiKeyCases() { |
| 1717 | + return new Object[][] { |
| 1718 | + {true}, |
| 1719 | + {false}, |
| 1720 | + }; |
| 1721 | + } |
| 1722 | + |
| 1723 | + @Test(dataProvider = "quarkusApiKeyCases") |
| 1724 | + public void quarkusEmitsAuthenticatedAnnotationForApiKey(boolean interfaceOnly) throws Exception { |
| 1725 | + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1726 | + output.deleteOnExit(); |
| 1727 | + |
| 1728 | + final OpenAPI openAPI = new OpenAPIParser() |
| 1729 | + .readLocation("src/test/resources/3_0/jaxrs-spec/quarkus-api-key.yaml", null, new ParseOptions()).getOpenAPI(); |
| 1730 | + |
| 1731 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 1732 | + codegen.setLibrary(QUARKUS_LIBRARY); |
| 1733 | + codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly); |
| 1734 | + codegen.additionalProperties().put(USE_JAKARTA_EE, true); |
| 1735 | + |
| 1736 | + final DefaultGenerator generator = new DefaultGenerator(); |
| 1737 | + final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate(); |
| 1738 | + |
| 1739 | + validateJavaSourceFiles(files); |
| 1740 | + |
| 1741 | + TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java"); |
| 1742 | + final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java")); |
| 1743 | + Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), 1); |
| 1744 | + } |
| 1745 | + |
| 1746 | + /** |
| 1747 | + * Parameterized test for OpenID Connect: empty scopes → @Authenticated; explicit scopes → absent |
| 1748 | + * (explicit scopes will be handled by @RolesAllowed in a future PR). |
| 1749 | + * Each row: (spec path, interfaceOnly, expected occurrence count). |
| 1750 | + */ |
| 1751 | + @DataProvider(name = "quarkusOpenIdConnectCases") |
| 1752 | + public Object[][] quarkusOpenIdConnectCases() { |
| 1753 | + return new Object[][] { |
| 1754 | + // no scopes → @Authenticated |
| 1755 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-openidconnect-no-scopes.yaml", true, 1}, |
| 1756 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-openidconnect-no-scopes.yaml", false, 1}, |
| 1757 | + // explicit scopes → no @Authenticated |
| 1758 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-openidconnect-with-scopes.yaml", true, 0}, |
| 1759 | + {"src/test/resources/3_0/jaxrs-spec/quarkus-openidconnect-with-scopes.yaml", false, 0}, |
| 1760 | + }; |
| 1761 | + } |
| 1762 | + |
| 1763 | + @Test(dataProvider = "quarkusOpenIdConnectCases") |
| 1764 | + public void quarkusEmitsAuthenticatedAnnotationForOpenIdConnect(String specPath, boolean interfaceOnly, int expectedCount) throws Exception { |
| 1765 | + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1766 | + output.deleteOnExit(); |
| 1767 | + |
| 1768 | + final OpenAPI openAPI = new OpenAPIParser() |
| 1769 | + .readLocation(specPath, null, new ParseOptions()).getOpenAPI(); |
| 1770 | + |
| 1771 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 1772 | + codegen.setLibrary(QUARKUS_LIBRARY); |
| 1773 | + codegen.additionalProperties().put(INTERFACE_ONLY, interfaceOnly); |
| 1774 | + codegen.additionalProperties().put(USE_JAKARTA_EE, true); |
| 1775 | + |
| 1776 | + final DefaultGenerator generator = new DefaultGenerator(); |
| 1777 | + final List<File> files = generator.opts(new ClientOptInput().openAPI(openAPI).config(codegen)).generate(); |
| 1778 | + |
| 1779 | + validateJavaSourceFiles(files); |
| 1780 | + |
| 1781 | + TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/ItemsApi.java"); |
| 1782 | + final String content = Files.readString(output.toPath().resolve("src/gen/java/org/openapitools/api/ItemsApi.java")); |
| 1783 | + Assert.assertEquals(TestUtils.countOccurrences(content, "@io\\.quarkus\\.security\\.Authenticated"), expectedCount); |
| 1784 | + } |
1589 | 1785 | } |
0 commit comments