Skip to main content

poolster_plugin_java/
oauth.rs

1use super::*;
2use poolster_core::engine::{Meta, Plugin, PluginContext};
3pub struct OAuth {
4    meta: Meta,
5}
6pub fn oauth() -> OAuth {
7    OAuth { meta: Meta::new() }
8}
9impl Plugin<Java> for OAuth {
10    fn kind(&self) -> &'static str {
11        "java-oauth"
12    }
13    fn meta(&self) -> &Meta {
14        &self.meta
15    }
16    fn generate(&self, cx: &mut PluginContext<'_, Java>) -> Result<()> {
17        let namespace = java_package_name(
18            cx.settings
19                .package_name
20                .as_deref()
21                .unwrap_or(&format!("io.poolster.{}", package_segment(&cx.api.name))),
22        );
23        for (name, source) in [
24            (
25                "OAuthClientCredentials",
26                include_str!("../templates/oauth.java.tmpl"),
27            ),
28            (
29                "OAuthHttpClient",
30                include_str!("../templates/oauth_http.java.tmpl"),
31            ),
32        ] {
33            cx.files.emit(GeneratedFile::new(
34                format!(
35                    "src/main/java/{}/{}.java",
36                    namespace.replace('.', "/"),
37                    name
38                ),
39                source.replace("__PACKAGE__", &namespace),
40            )?)?;
41        }
42        Ok(())
43    }
44}
45#[cfg(test)]
46mod tests {
47    use super::*;
48    #[test]
49    #[ignore = "requires native JDK17 and Maven"]
50    fn native_oauth_cache_singleflight_replay_origin_and_interruption() {
51        let api = Api {
52            name: "OAuth".into(),
53            ..Default::default()
54        };
55        let tree = poolster_core::engine::Packages::new()
56            .package(
57                crate::package("sdk")
58                    .name("io.poolster.oauth")
59                    .with(crate::sdk())
60                    .with(oauth()),
61            )
62            .generate(&api, None)
63            .unwrap();
64        let dir = tempfile::tempdir().unwrap();
65        tree.write_to(dir.path()).unwrap();
66        let cwd = dir.path().join("sdk");
67        std::fs::create_dir_all(cwd.join("src/test/java/io/poolster/oauth")).unwrap();
68        std::fs::write(
69            cwd.join("src/test/java/io/poolster/oauth/OAuthProbe.java"),
70            include_str!("../tests/fixtures/oauth_probe.java"),
71        )
72        .unwrap();
73        let output = std::process::Command::new("mvn")
74            .args([
75                "-q",
76                "test-compile",
77                "org.codehaus.mojo:exec-maven-plugin:3.5.0:java",
78                "-Dexec.mainClass=io.poolster.oauth.OAuthProbe",
79                "-Dexec.classpathScope=test",
80            ])
81            .current_dir(cwd)
82            .output()
83            .unwrap();
84        assert!(
85            output.status.success(),
86            "{}{}",
87            String::from_utf8_lossy(&output.stdout),
88            String::from_utf8_lossy(&output.stderr)
89        );
90    }
91}