Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserAgents.of(UserAgent, Iterable<Agent>) #902

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-902.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add UserAgents.of(UserAgent, Iterable<Agent>)
links:
- https://github.com/palantir/conjure-java-runtime-api/pull/902
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.util.List;
import java.util.Optional;
import org.immutables.value.Value;
Expand Down Expand Up @@ -56,6 +57,16 @@ static UserAgent of(Agent agent) {
return ImmutableUserAgent.builder().primary(agent).build();
}

/**
* Returns a user agent with the given base agent combined with specified additional informational agents.
*/
static UserAgent of(UserAgent base, Iterable<Agent> additional) {
return ImmutableUserAgent.builder()
.from(base)
.addAllInformational(additional)
.build();
}

/**
* Returns a new {@link UserAgent} instance whose {@link #informational} agents are this instance's agents plus the
* given agent.
Expand Down Expand Up @@ -86,13 +97,14 @@ interface Agent {

@Value.Check
default void check() {
Preconditions.checkArgument(
UserAgents.isValidName(name()), "Illegal agent name format", SafeArg.of("name", name()));
// Should never hit the following.
Preconditions.checkArgument(
UserAgents.isValidVersion(version()),
"Illegal version format. This is a bug",
SafeArg.of("version", version()));
if (!UserAgents.isValidName(name())) {
throw new SafeIllegalArgumentException("Illegal agent name format", SafeArg.of("name", name()));
}
if (!UserAgents.isValidVersion(version())) {
// Should never hit the following.
throw new SafeIllegalArgumentException(
"Illegal version format. This is a bug", SafeArg.of("version", version()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably update this to use immutables params and avoid the Agent.Builder altogether

}

static Agent of(String name, String version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.palantir.conjure.java.api.config.service.UserAgent.Agent;
import com.palantir.logsafe.SafeArg;
import java.util.List;
import org.junit.jupiter.api.Test;

public class UserAgentTest {
Expand Down Expand Up @@ -59,6 +63,29 @@ public void testCorrectHeaderFormatWithoutNodeId() {
assertThat(UserAgents.format(derivedAgent)).isEqualTo("service/1.0.0 conjure/2.0.0");
}

@Test
void testPrimaryWithInformational() {
UserAgent baseUserAgent = UserAgent.of(Agent.of("service", "1.0.0"));
List<Agent> info = ImmutableList.of(Agent.of("conjure", "1.2.3"), Agent.of("jdk", "17.0.4.1"));
UserAgent first = UserAgent.of(baseUserAgent, info);
assertThat(first).satisfies(agent -> {
assertThat(agent.primary()).isEqualTo(baseUserAgent.primary());
assertThat(agent.informational()).hasSize(2).isEqualTo(info);
assertThat(UserAgents.format(agent)).isEqualTo("service/1.0.0 conjure/1.2.3 jdk/17.0.4.1");
assertThat(UserAgents.parse(UserAgents.format(agent))).isEqualTo(agent);
assertThat(UserAgent.of(agent, ImmutableList.of())).isEqualTo(agent);
});

List<Agent> moreInfo = ImmutableList.of(Agent.of("test", "4.5.6"));
assertThat(UserAgent.of(first, moreInfo)).satisfies(agent -> {
assertThat(agent.primary()).isEqualTo(baseUserAgent.primary());
assertThat(agent.informational()).hasSize(3).containsExactlyElementsOf(Iterables.concat(info, moreInfo));
assertThat(UserAgents.format(agent)).isEqualTo("service/1.0.0 conjure/1.2.3 jdk/17.0.4.1 test/4.5.6");
assertThat(UserAgents.parse(UserAgents.format(agent))).isEqualTo(agent);
assertThat(UserAgent.of(agent, ImmutableList.of())).isEqualTo(agent);
});
}

@Test
public void testInvalidServiceName() {
assertThatLoggableExceptionThrownBy(() -> UserAgent.Agent.of("invalid service name", "1.0.0"))
Expand Down