String serialize(T payload) {
30 | return gson.toJson(payload);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/OptimizelyDecisionsCallback.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2025, Optimizely and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.optimizelydecision;
17 |
18 | import javax.annotation.Nonnull;
19 | import java.util.Map;
20 |
21 | /**
22 | * Callback interface for async multiple decisions operations.
23 | */
24 | @FunctionalInterface
25 | public interface OptimizelyDecisionsCallback {
26 | /**
27 | * Called when an async multiple decisions operation completes.
28 | *
29 | * @param decisions Map of flag keys to decision results
30 | */
31 | void onCompleted(@Nonnull Map decisions);
32 | }
33 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/NotificationHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | /**
20 | * NotificationHandler is a generic interface Optimizely notification listeners.
21 | * This interface replaces {@link NotificationListener} which didn't provide adequate type safety.
22 | *
23 | * While this class adds generic handler implementations to be created, the domain of supported
24 | * implementations is maintained by the {@link NotificationCenter}
25 | */
26 | public interface NotificationHandler {
27 | void handle(T message) throws Exception;
28 | }
29 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/EventProcessor.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import com.optimizely.ab.event.internal.UserEvent;
20 | import com.optimizely.ab.event.internal.payload.EventBatch;
21 | import com.optimizely.ab.notification.NotificationHandler;
22 |
23 | /**
24 | * EventProcessor interface is used to provide an intermediary processing stage within
25 | * event production. It's assumed that the EventProcessor dispatches events via a provided
26 | * {@link EventHandler}.
27 | */
28 | public interface EventProcessor {
29 | void process(UserEvent userEvent);
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/GsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer.impl;
17 |
18 | import com.google.gson.Gson;
19 | import com.google.gson.GsonBuilder;
20 | import com.optimizely.ab.odp.ODPEvent;
21 | import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
22 |
23 | import java.util.List;
24 |
25 | public class GsonSerializer implements ODPJsonSerializer {
26 | @Override
27 | public String serializeEvents(List events) {
28 | Gson gson = new GsonBuilder().serializeNulls().create();
29 | return gson.toJson(events);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Reusable action of building snapshot and publish
2 |
3 | on:
4 | workflow_call:
5 | inputs:
6 | action:
7 | required: true
8 | type: string
9 | github_tag:
10 | required: true
11 | type: string
12 | secrets:
13 | MAVEN_SIGNING_KEY_BASE64:
14 | required: true
15 | MAVEN_SIGNING_PASSPHRASE:
16 | required: true
17 | MAVEN_CENTRAL_USERNAME:
18 | required: true
19 | MAVEN_CENTRAL_PASSWORD:
20 | required: true
21 | jobs:
22 | run_build:
23 | runs-on: ubuntu-latest
24 | steps:
25 | - uses: actions/checkout@v4
26 | - name: set up JDK 8
27 | uses: actions/setup-java@v2
28 | with:
29 | java-version: '8'
30 | distribution: 'temurin'
31 | - name: Grant execute permission for gradlew
32 | run: chmod +x gradlew
33 | - name: ${{ inputs.action }}
34 | env:
35 | MAVEN_SIGNING_KEY_BASE64: ${{ secrets.MAVEN_SIGNING_KEY_BASE64 }}
36 | MAVEN_SIGNING_PASSPHRASE: ${{ secrets.MAVEN_SIGNING_PASSPHRASE }}
37 | MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
38 | MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
39 | run: GITHUB_TAG=${{ inputs.github_tag }} ./gradlew ${{ inputs.action }}
40 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/notification/TestNotificationHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | /**
23 | * TestNotificationHandler used for unit testing NotificationCenter and NotificationManager
24 | */
25 | class TestNotificationHandler implements NotificationHandler {
26 | private final List messages = new ArrayList<>();
27 |
28 | @Override
29 | public void handle(T message) {
30 | messages.add(message);
31 | }
32 |
33 | List getMessages() {
34 | return messages;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/UnexpectedValueTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.config.audience.match;
19 |
20 | /**
21 | * UnexpectedValueTypeException is thrown when the condition value found in the datafile is
22 | * not one of an expected type for this version of the SDK.
23 | */
24 | public class UnexpectedValueTypeException extends Exception {
25 | private static String message = "has an unsupported condition value. You may need to upgrade to a newer release of the Optimizely SDK.";
26 |
27 | public UnexpectedValueTypeException() {
28 | super(message);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/OptimizelyRuntimeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | /**
20 | * Base class for all Optimizely Exceptions.
21 | */
22 | public class OptimizelyRuntimeException extends RuntimeException {
23 |
24 | public OptimizelyRuntimeException() {
25 | }
26 |
27 | public OptimizelyRuntimeException(Exception exception) {
28 | super(exception);
29 | }
30 |
31 | public OptimizelyRuntimeException(String message) {
32 | super(message);
33 | }
34 |
35 | public OptimizelyRuntimeException(String message, Throwable cause) {
36 | super(message, cause);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/error/RaiseExceptionErrorHandlerTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.error;
18 |
19 | import com.optimizely.ab.OptimizelyRuntimeException;
20 | import org.junit.Before;
21 | import org.junit.Test;
22 |
23 | public class RaiseExceptionErrorHandlerTest {
24 |
25 | private RaiseExceptionErrorHandler errorHandler;
26 |
27 | @Before
28 | public void setUp() throws Exception {
29 | errorHandler = new RaiseExceptionErrorHandler();
30 | }
31 |
32 | @Test(expected = OptimizelyRuntimeException.class)
33 | public void handleError() {
34 | errorHandler.handleError(new OptimizelyRuntimeException());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/UnknownEventTypeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 | import com.optimizely.ab.config.EventType;
21 |
22 | /**
23 | * Exception thrown when attempting to use/refer to an {@link EventType} that isn't present in the current
24 | * {@link ProjectConfig}.
25 | */
26 | public class UnknownEventTypeException extends OptimizelyRuntimeException {
27 |
28 | public UnknownEventTypeException(String message) {
29 | super(message);
30 | }
31 |
32 | public UnknownEventTypeException(String message, Throwable cause) {
33 | super(message, cause);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/EmptyCondition.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2019, 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.config.audience;
17 |
18 | import com.optimizely.ab.OptimizelyUserContext;
19 | import com.optimizely.ab.config.ProjectConfig;
20 |
21 | import javax.annotation.Nullable;
22 | import java.util.Map;
23 |
24 | public class EmptyCondition extends LeafCondition {
25 | @Nullable
26 | @Override
27 | public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
28 | return true;
29 | }
30 |
31 | @Override
32 | public String toJson() { return null; }
33 |
34 | @Override
35 | public String getOperandOrId() {
36 | return null;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/NullCondition.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2019, 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.config.audience;
17 |
18 | import com.optimizely.ab.OptimizelyUserContext;
19 | import com.optimizely.ab.config.ProjectConfig;
20 |
21 | import javax.annotation.Nullable;
22 | import java.util.Map;
23 |
24 | public class NullCondition extends LeafCondition {
25 | @Nullable
26 | @Override
27 | public Boolean evaluate(ProjectConfig config, OptimizelyUserContext user) {
28 | return null;
29 | }
30 |
31 | @Override
32 | public String toJson() { return null; }
33 |
34 | @Override
35 | public String getOperandOrId() {
36 | return null;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.optimizelydecision;
19 |
20 | public enum DecisionMessage {
21 | SDK_NOT_READY("Optimizely SDK not configured properly yet."),
22 | FLAG_KEY_INVALID("No flag was found for key \"%s\"."),
23 | VARIABLE_VALUE_INVALID("Variable value for key \"%s\" is invalid or wrong type."),
24 | CMAB_ERROR("Failed to fetch CMAB data for experiment %s.");
25 |
26 | private String format;
27 |
28 | DecisionMessage(String format) {
29 | this.format = format;
30 | }
31 |
32 | public String reason(Object... args){
33 | return String.format(format, args);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/event/internal/BaseEventTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal;
18 |
19 | import org.junit.Before;
20 | import org.junit.Test;
21 |
22 | import static org.junit.Assert.*;
23 |
24 | public class BaseEventTest {
25 |
26 | private BaseEvent baseEvent;
27 |
28 | @Before
29 | public void setUp() {
30 | this.baseEvent = new BaseEvent();
31 | }
32 |
33 | @Test
34 | public void getUUID() {
35 | assertNotNull(baseEvent.getUUID());
36 | assertFalse(baseEvent.getUUID().isEmpty());
37 | }
38 |
39 | @Test
40 | public void getTimestamp() {
41 | assertTrue(baseEvent.getTimestamp() > 0);
42 | }
43 | }
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/Condition.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2018, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience;
18 |
19 | import com.optimizely.ab.OptimizelyUserContext;
20 | import com.optimizely.ab.config.ProjectConfig;
21 |
22 | import javax.annotation.Nullable;
23 | import java.util.List;
24 | import java.util.Map;
25 |
26 | /**
27 | * Interface implemented by all conditions condition objects to aid in condition evaluation.
28 | */
29 | public interface Condition {
30 |
31 | @Nullable
32 | Boolean evaluate(ProjectConfig config, OptimizelyUserContext user);
33 |
34 | String toJson();
35 |
36 | String getOperandOrId();
37 |
38 | List getConditions();
39 | }
40 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionLTMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionLTMatch performs a "less than" comparison via {@link SemanticVersion#compare(Object, Object)}.
23 | */
24 | class SemanticVersionLTMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
27 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
28 | return SemanticVersion.compare(attributeValue, conditionValue) < 0;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/NoopEventHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import org.slf4j.Logger;
20 | import org.slf4j.LoggerFactory;
21 |
22 | import java.util.Map;
23 |
24 | /**
25 | * {@link EventHandler} that logs events but does not perform any dispatching.
26 | */
27 | public class NoopEventHandler implements EventHandler {
28 |
29 | private static final Logger logger = LoggerFactory.getLogger(NoopEventHandler.class);
30 |
31 | @Override
32 | public void dispatchEvent(LogEvent logEvent) {
33 | logger.debug("Called dispatchEvent with URL: {} and params: {}", logEvent.getEndpointUrl(),
34 | logEvent.getRequestParams());
35 | }
36 | }
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionGTMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionGTMatch performs a "greater than" comparison via {@link SemanticVersion#compare(Object, Object)}.
23 | */
24 | class SemanticVersionGTMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
27 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
28 | return SemanticVersion.compare(attributeValue, conditionValue) > 0;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionEqualsMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionEqualsMatch performs a equality comparison via {@link SemanticVersion#compare(Object, Object)}.
23 | */
24 | class SemanticVersionEqualsMatch implements Match {
25 | @Nullable
26 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
27 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
28 | return SemanticVersion.compare(attributeValue, conditionValue) == 0;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/SourceInfo.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2019, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 |
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.Map;
20 |
21 | public interface SourceInfo {
22 | Map get();
23 | }
24 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/notification/RolloutSourceInfoTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.notification;
19 |
20 | import org.junit.Before;
21 | import org.junit.Test;
22 |
23 | import java.util.Collections;
24 | import java.util.Map;
25 |
26 | import static org.junit.Assert.assertEquals;
27 |
28 | public class RolloutSourceInfoTest {
29 |
30 | private RolloutSourceInfo rolloutSourceInfo;
31 |
32 | @Before
33 | public void setUp() {
34 | rolloutSourceInfo = new RolloutSourceInfo();
35 | }
36 |
37 | @Test
38 | public void testGet() {
39 | Map expectedInfo = Collections.EMPTY_MAP;
40 | assertEquals(expectedInfo, rolloutSourceInfo.get());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionGEMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionGEMatch performs a "greater than or equal to" comparison
23 | * via {@link SemanticVersion#compare(Object, Object)}.
24 | */
25 | class SemanticVersionGEMatch implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
29 | return SemanticVersion.compare(attributeValue, conditionValue) >= 0;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionLEMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SemanticVersionLEMatch performs a "less than or equal to" comparison
23 | * via {@link SemanticVersion#compare(Object, Object)}.
24 | */
25 | class SemanticVersionLEMatch implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (attributeValue == null) return null; // stay silent (no WARNING) when attribute value is missing or empty.
29 | return SemanticVersion.compare(attributeValue, conditionValue) <= 0;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/testutils/OTUtils.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * https://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.testutils;
18 |
19 | import com.optimizely.ab.*;
20 | import java.util.Collections;
21 | import java.util.Map;
22 |
23 | public class OTUtils {
24 | public static OptimizelyUserContext user(String userId, Map attributes) {
25 | Optimizely optimizely = new Optimizely.Builder().build();
26 | return new OptimizelyUserContext(optimizely, userId, attributes);
27 | }
28 |
29 | public static OptimizelyUserContext user(Map attributes) {
30 | return user("any-user", attributes);
31 | }
32 |
33 | public static OptimizelyUserContext user() {
34 | return user("any-user", Collections.emptyMap());
35 | }
36 | }
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/bucketing/DecisionPath.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2025 Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 | package com.optimizely.ab.bucketing;
17 |
18 | public enum DecisionPath {
19 | WITH_CMAB, // Use CMAB logic
20 | WITHOUT_CMAB // Skip CMAB logic (traditional A/B testing)
21 | }
22 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/UnknownExperimentException.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 | import com.optimizely.ab.config.Experiment;
21 |
22 | /**
23 | * Exception thrown when attempting to use/refer to an {@link Experiment} that isn't present in the current
24 | * {@link ProjectConfig}.
25 | *
26 | * This may occur if code changes are made prior to the experiment being setup in the Optimizely application.
27 | */
28 | public class UnknownExperimentException extends OptimizelyRuntimeException {
29 |
30 | public UnknownExperimentException(String message) {
31 | super(message);
32 | }
33 |
34 | public UnknownExperimentException(String message, Throwable cause) {
35 | super(message, cause);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/JacksonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer.impl;
17 |
18 | import com.fasterxml.jackson.core.JsonProcessingException;
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.optimizely.ab.odp.ODPEvent;
21 | import com.optimizely.ab.odp.serializer.ODPJsonSerializer;
22 |
23 | import java.util.List;
24 |
25 | public class JacksonSerializer implements ODPJsonSerializer {
26 | @Override
27 | public String serializeEvents(List events) {
28 | ObjectMapper objectMapper = new ObjectMapper();
29 | try {
30 | return objectMapper.writeValueAsString(events);
31 | } catch (JsonProcessingException e) {
32 | // log error here
33 | }
34 | return null;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/HoldoutGsonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import java.lang.reflect.Type;
20 |
21 | import com.google.gson.JsonDeserializationContext;
22 | import com.google.gson.JsonDeserializer;
23 | import com.google.gson.JsonElement;
24 | import com.google.gson.JsonObject;
25 | import com.google.gson.JsonParseException;
26 | import com.optimizely.ab.config.Holdout;
27 |
28 | final class HoldoutGsonDeserializer implements JsonDeserializer {
29 |
30 | @Override
31 | public Holdout deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
32 | throws JsonParseException {
33 |
34 | JsonObject jsonObject = json.getAsJsonObject();
35 |
36 | return GsonHelpers.parseHoldout(jsonObject, context);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/FeatureFlagGsonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.google.gson.JsonDeserializationContext;
20 | import com.google.gson.JsonDeserializer;
21 | import com.google.gson.JsonElement;
22 | import com.google.gson.JsonObject;
23 | import com.google.gson.JsonParseException;
24 | import com.optimizely.ab.config.FeatureFlag;
25 |
26 | import java.lang.reflect.Type;
27 |
28 | public class FeatureFlagGsonDeserializer implements JsonDeserializer {
29 | @Override
30 | public FeatureFlag deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
31 | throws JsonParseException {
32 |
33 | JsonObject jsonObject = json.getAsJsonObject();
34 | return GsonHelpers.parseFeatureFlag(jsonObject, context);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/cmab/service/CmabService.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2025, Optimizely
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.cmab.service;
17 |
18 | import java.util.List;
19 |
20 | import com.optimizely.ab.OptimizelyUserContext;
21 | import com.optimizely.ab.config.ProjectConfig;
22 | import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
23 |
24 | public interface CmabService {
25 | /**
26 | * Get variation id for the user
27 | * @param projectConfig the project configuration
28 | * @param userContext the user context
29 | * @param ruleId the rule identifier
30 | * @param options list of decide options
31 | * @return CompletableFuture containing the CMAB decision
32 | */
33 | CmabDecision getDecision(
34 | ProjectConfig projectConfig,
35 | OptimizelyUserContext userContext,
36 | String ruleId,
37 | List options
38 | );
39 | }
40 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/ExperimentGsonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.google.gson.JsonDeserializationContext;
20 | import com.google.gson.JsonDeserializer;
21 | import com.google.gson.JsonElement;
22 | import com.google.gson.JsonObject;
23 | import com.google.gson.JsonParseException;
24 |
25 | import com.optimizely.ab.config.Experiment;
26 |
27 | import java.lang.reflect.Type;
28 |
29 | final class ExperimentGsonDeserializer implements JsonDeserializer {
30 |
31 | @Override
32 | public Experiment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
33 | throws JsonParseException {
34 |
35 | JsonObject jsonObject = json.getAsJsonObject();
36 |
37 | return GsonHelpers.parseExperiment(jsonObject, context);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/RolloutSourceInfo.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2019, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 |
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.Collections;
20 | import java.util.Map;
21 |
22 | public class RolloutSourceInfo implements SourceInfo {
23 | @Override
24 | public Map get() {
25 | return Collections.EMPTY_MAP;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/DefaultMatchForLegacyAttributes.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * This is a temporary class. It mimics the current behaviour for
23 | * legacy custom attributes. This will be dropped for ExactMatch and the unit tests need to be fixed.
24 | */
25 | class DefaultMatchForLegacyAttributes implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (!(conditionValue instanceof String)) {
29 | throw new UnexpectedValueTypeException();
30 | }
31 | if (attributeValue == null) {
32 | return false;
33 | }
34 | return conditionValue.toString().equals(attributeValue.toString());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/SafetyUtils.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019,2021, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import org.slf4j.Logger;
20 | import org.slf4j.LoggerFactory;
21 |
22 | /**
23 | * Collection of utils used to prevent the Optimizely SDK from throwing or crashing the hosting application.
24 | */
25 | public class SafetyUtils {
26 |
27 | private static final Logger logger = LoggerFactory.getLogger(SafetyUtils.class);
28 |
29 | /**
30 | * Helper method which checks if Object is an instance of AutoCloseable and calls close() on it.
31 | *
32 | * @param obj The object
33 | */
34 | public static void tryClose(Object obj) {
35 | if (!(obj instanceof AutoCloseable)) {
36 | return;
37 | }
38 |
39 | try {
40 | ((AutoCloseable) obj).close();
41 | } catch (Exception e) {
42 | logger.warn("Unexpected exception on trying to close {}.", obj);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/config/VariationTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import org.junit.Test;
20 |
21 | import static org.hamcrest.CoreMatchers.is;
22 | import static org.hamcrest.MatcherAssert.assertThat;
23 |
24 | /**
25 | * Tests for {@link Variation}.
26 | */
27 | public class VariationTest {
28 |
29 | /**
30 | * Not a grammatical error: verify that {@link Variation#is(String)} is comparing the provided value
31 | * with that given by {@link Variation#getKey()}.
32 | */
33 | @Test
34 | public void isUsesVariationKey() throws Exception {
35 | Variation variation = new Variation("1234", "key");
36 | assertThat(variation.is("blah"), is(false));
37 |
38 | // we shouldn't be comparing the ids
39 | assertThat(variation.is("1234"), is(false));
40 |
41 | // we *should* be comparing keys
42 | assertThat(variation.is("key"), is(true));
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/SubstringMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | /**
22 | * SubstringMatch checks if the attribute value contains the condition value.
23 | * This assumes both the condition and attribute values are provided as Strings.
24 | */
25 | class SubstringMatch implements Match {
26 | @Nullable
27 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
28 | if (!(conditionValue instanceof String)) {
29 | throw new UnexpectedValueTypeException();
30 | }
31 |
32 | if (!(attributeValue instanceof String)) {
33 | return null;
34 | }
35 |
36 | try {
37 | return attributeValue.toString().contains(conditionValue.toString());
38 | } catch (Exception e) {
39 | return null;
40 | }
41 | }
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/config/AtomicProjectConfigManagerTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import org.junit.Before;
20 | import org.junit.Test;
21 |
22 | import static com.optimizely.ab.config.DatafileProjectConfigTestUtils.validConfigJsonV4;
23 | import static org.junit.Assert.*;
24 |
25 | public class AtomicProjectConfigManagerTest {
26 |
27 | private AtomicProjectConfigManager projectConfigManager;
28 |
29 | @Before
30 | public void setUp() {
31 | projectConfigManager = new AtomicProjectConfigManager();
32 | }
33 |
34 | @Test
35 | public void testGetAndSetConfig() throws Exception {
36 | assertNull(projectConfigManager.getConfig());
37 |
38 | ProjectConfig projectConfig = new DatafileProjectConfig.Builder().withDatafile(validConfigJsonV4()).build();
39 | projectConfigManager.setConfig(projectConfig);
40 | assertEquals(projectConfig, projectConfigManager.getConfig());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/NotificationListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | /**
20 | * An interface class for Optimizely notification listeners.
21 | *
22 | * We changed this from a abstract class to a interface to support lambdas moving forward in Java 8 and beyond.
23 | *
24 | * @deprecated in favor of the {@link NotificationHandler} interface.
25 | */
26 | @Deprecated
27 | public interface NotificationListener {
28 |
29 | /**
30 | * This is the base method of notification. Implementation classes such as {@link ActivateNotificationListener}
31 | * will implement this call and provide another method with the correct parameters
32 | * Notify called when a notification is triggered via the {@link com.optimizely.ab.notification.NotificationCenter}
33 | *
34 | * @param args - variable argument list based on the type of notification.
35 | */
36 | public void notify(Object... args);
37 | }
38 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/AtomicProjectConfigManager.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, 2023, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import java.util.concurrent.atomic.AtomicReference;
20 |
21 | public class AtomicProjectConfigManager implements ProjectConfigManager {
22 |
23 | private final AtomicReference projectConfigReference = new AtomicReference<>();
24 |
25 | @Override
26 | public ProjectConfig getConfig() {
27 | return projectConfigReference.get();
28 | }
29 |
30 | /**
31 | * Access to current cached project configuration.
32 | *
33 | * @return {@link ProjectConfig}
34 | */
35 | @Override
36 | public ProjectConfig getCachedConfig() {
37 | return projectConfigReference.get();
38 | }
39 |
40 | @Override
41 | public String getSDKKey() {
42 | return null;
43 | }
44 |
45 | public void setConfig(ProjectConfig projectConfig) {
46 | projectConfigReference.set(projectConfig);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/event/NoopEventHandlerTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import ch.qos.logback.classic.Level;
20 |
21 | import com.optimizely.ab.internal.LogbackVerifier;
22 |
23 | import org.junit.Rule;
24 | import org.junit.Test;
25 |
26 | import java.util.Collections;
27 |
28 | import static com.optimizely.ab.event.LogEvent.RequestMethod;
29 |
30 | /**
31 | * Tests for {@link NoopEventHandler} -- mostly for coverage...
32 | */
33 | public class NoopEventHandlerTest {
34 |
35 | @Rule
36 | public LogbackVerifier logbackVerifier = new LogbackVerifier();
37 |
38 | @Test
39 | public void dispatchEvent() throws Exception {
40 | NoopEventHandler noopEventHandler = new NoopEventHandler();
41 | noopEventHandler.dispatchEvent(
42 | new LogEvent(RequestMethod.GET, "blah", Collections.emptyMap(), null));
43 | logbackVerifier.expectMessage(Level.DEBUG, "Called dispatchEvent with URL: blah and params: {}");
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/ENHANCEMENT.yml:
--------------------------------------------------------------------------------
1 | name: ✨Enhancement
2 | description: Create a new ticket for a Enhancement/Tech-initiative for the benefit of the SDK which would be considered for a minor version update.
3 | title: "[ENHANCEMENT] "
4 | labels: ["enhancement"]
5 | body:
6 | - type: textarea
7 | id: description
8 | attributes:
9 | label: "Description"
10 | description: Briefly describe the enhancement in a few sentences.
11 | placeholder: Short description...
12 | validations:
13 | required: true
14 | - type: textarea
15 | id: benefits
16 | attributes:
17 | label: "Benefits"
18 | description: How would the enhancement benefit to your product or usage?
19 | placeholder: Benefits...
20 | validations:
21 | required: true
22 | - type: textarea
23 | id: detail
24 | attributes:
25 | label: "Detail"
26 | description: How would you like the enhancement to work? Please provide as much detail as possible
27 | placeholder: Detailed description...
28 | validations:
29 | required: false
30 | - type: textarea
31 | id: examples
32 | attributes:
33 | label: "Examples"
34 | description: Are there any examples of this enhancement in other products/services? If so, please provide links or references.
35 | placeholder: Links/References...
36 | validations:
37 | required: false
38 | - type: textarea
39 | id: risks
40 | attributes:
41 | label: "Risks/Downsides"
42 | description: Do you think this enhancement could have any potential downsides or risks?
43 | placeholder: Risks/Downsides...
44 | validations:
45 | required: false
46 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/notification/FeatureTestSourceInfoTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.optimizely.ab.notification;
19 |
20 | import org.junit.Before;
21 | import org.junit.Test;
22 |
23 | import java.util.HashMap;
24 |
25 | import static org.junit.Assert.assertEquals;
26 |
27 | public class FeatureTestSourceInfoTest {
28 |
29 | private static final String EXPERIMENT_KEY = "featureTestKey";
30 | private static final String VARIATION_KEY = "featureTestVariationKey";
31 |
32 | private FeatureTestSourceInfo featureSourceInfo;
33 |
34 | @Before
35 | public void setUp() {
36 | featureSourceInfo = new FeatureTestSourceInfo(EXPERIMENT_KEY, VARIATION_KEY);
37 | }
38 |
39 | @Test
40 | public void testGet() {
41 | HashMap expectedSourceInfo = new HashMap<>();
42 | expectedSourceInfo.put("experimentKey", EXPERIMENT_KEY);
43 | expectedSourceInfo.put("variationKey", VARIATION_KEY);
44 |
45 | assertEquals(expectedSourceInfo, featureSourceInfo.get());
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/NumberComparator.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import static com.optimizely.ab.internal.AttributesUtil.isValidNumber;
20 |
21 | /**
22 | * NumberComparator performs a numeric comparison. The input values are assumed to be numbers else
23 | * compare will throw an {@link UnknownValueTypeException}.
24 | */
25 | public class NumberComparator {
26 | public static int compare(Object o1, Object o2) throws UnknownValueTypeException {
27 | if (!isValidNumber(o1) || !isValidNumber(o2)) {
28 | throw new UnknownValueTypeException();
29 | }
30 |
31 | return compareUnsafe(o1, o2);
32 | }
33 |
34 | /**
35 | * compareUnsafe is provided to avoid checking the input values are numbers. It's assumed that the inputs
36 | * are known to be Numbers.
37 | */
38 | static int compareUnsafe(Object o1, Object o2) {
39 | return Double.compare(((Number) o1).doubleValue(), ((Number) o2).doubleValue());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/JacksonHelpers.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.fasterxml.jackson.core.ObjectCodec;
20 | import com.fasterxml.jackson.databind.JsonNode;
21 |
22 | import java.io.IOException;
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | final class JacksonHelpers {
27 | private JacksonHelpers() {
28 | }
29 |
30 | static List arrayNodeToList(JsonNode arrayNode, Class itemClass, ObjectCodec codec) throws IOException {
31 | if (arrayNode == null || arrayNode.isNull() || !arrayNode.isArray()) {
32 | return null;
33 | }
34 |
35 | List items = new ArrayList<>(arrayNode.size());
36 |
37 | for (int i = 0; i < arrayNode.size(); i++) {
38 | JsonNode itemNode = arrayNode.get(i);
39 | if (itemNode.isNull()) {
40 | continue;
41 | }
42 | items.add(codec.treeToValue(itemNode, itemClass));
43 | }
44 |
45 | return items;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionReasons.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelydecision;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | public class DecisionReasons {
23 |
24 | protected final List errors = new ArrayList<>();
25 | protected final List infos = new ArrayList<>();
26 |
27 | public void addError(String format, Object... args) {
28 | String message = String.format(format, args);
29 | errors.add(message);
30 | }
31 |
32 | public String addInfo(String format, Object... args) {
33 | String message = String.format(format, args);
34 | infos.add(message);
35 | return message;
36 | }
37 |
38 | public void merge(DecisionReasons target) {
39 | errors.addAll(target.errors);
40 | infos.addAll(target.infos);
41 | }
42 |
43 | public List toReport() {
44 | List reasons = new ArrayList<>(errors);
45 | reasons.addAll(infos);
46 | return reasons;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/build.gradle:
--------------------------------------------------------------------------------
1 | dependencies {
2 | implementation group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
3 | implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: jacksonVersion
4 | implementation group: 'com.google.code.findbugs', name: 'annotations', version: findbugsAnnotationVersion
5 | implementation group: 'com.google.code.findbugs', name: 'jsr305', version: findbugsJsrVersion
6 | testImplementation group: 'junit', name: 'junit', version: junitVersion
7 | testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: logbackVersion
8 |
9 | // an assortment of json parsers
10 | compileOnly group: 'com.google.code.gson', name: 'gson', version: gsonVersion, optional
11 | compileOnly group: 'org.json', name: 'json', version: jsonVersion, optional
12 | compileOnly group: 'com.googlecode.json-simple', name: 'json-simple', version: jsonSimpleVersion, optional
13 | compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion, optional
14 | }
15 |
16 | tasks.named('processJmhResources') {
17 | duplicatesStrategy = DuplicatesStrategy.WARN
18 | }
19 |
20 |
21 | test {
22 | useJUnit {
23 | excludeCategories 'com.optimizely.ab.categories.ExhaustiveTest'
24 | }
25 | }
26 |
27 | task exhaustiveTest(type: Test) {
28 | useJUnit {
29 | includeCategories 'com.optimizely.ab.categories.ExhaustiveTest'
30 | }
31 | }
32 |
33 |
34 | task generateVersionFile {
35 | // add the build version information into a file that'll go into the distribution
36 | ext.buildVersion = new File(projectDir, "src/main/resources/optimizely-build-version")
37 | buildVersion.text = version
38 | }
39 |
40 | build.finalizedBy(generateVersionFile)
41 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/internal/PropertyUtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import org.junit.After;
20 | import org.junit.Test;
21 |
22 | import static org.junit.Assert.*;
23 |
24 | public class PropertyUtilTest {
25 |
26 | private static final String SHARED_KEY = "test.prop";
27 | private static final String EXPECTED = "bar";
28 |
29 | @After
30 | public void tearDown() {
31 | System.setProperty(SHARED_KEY, "INVALID");
32 | }
33 |
34 | @Test
35 | public void testSystemPropBeforeOptimizelyProp() {
36 | String expected = "foo";
37 | System.setProperty("optimizely." + SHARED_KEY, expected);
38 | assertEquals(expected, PropertyUtils.get(SHARED_KEY));
39 | }
40 |
41 | @Test
42 | public void getFromOptimizelyProp() {
43 | assertEquals(EXPECTED, PropertyUtils.get("file.only"));
44 | }
45 |
46 | @Test
47 | public void getFromSystemProp() {
48 | System.setProperty("optimizely.sys.only", EXPECTED);
49 | assertEquals(EXPECTED, PropertyUtils.get("sys.only"));
50 | }
51 | }
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/optimizelyjson/TestTypes.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelyjson;
18 |
19 | /**
20 | * Test types for parsing JSON strings to Java objects (OptimizelyJSON)
21 | */
22 | public class TestTypes {
23 |
24 | public static class MD1 {
25 | public String k1;
26 | public boolean k2;
27 | public MD2 k3;
28 | }
29 |
30 | public static class MD2 {
31 | public double kk1;
32 | public MD3 kk2;
33 | }
34 |
35 | public static class MD3 {
36 | public boolean kkk1;
37 | public double kkk2;
38 | public String kkk3;
39 | public Object[] kkk4;
40 | }
41 |
42 | // Invalid parse type
43 |
44 | public static class NotMatchingType {
45 | public String x99;
46 | }
47 |
48 | // Test types for integer parsing tests
49 |
50 | public static class MDN1 {
51 | public int k1;
52 | public double k2;
53 | public MDN2 k3;
54 | }
55 |
56 | public static class MDN2 {
57 | public int kk1;
58 | public double kk2;
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/event/internal/ClientEngineInfoTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal;
18 |
19 | import com.optimizely.ab.event.internal.payload.EventBatch;
20 | import org.junit.After;
21 | import org.junit.Test;
22 |
23 | import static org.junit.Assert.*;
24 |
25 | public class ClientEngineInfoTest {
26 |
27 | @After
28 | public void tearDown() throws Exception {
29 | ClientEngineInfo.setClientEngineName(ClientEngineInfo.DEFAULT_NAME);
30 | }
31 |
32 | @Test
33 | public void testSetAndGetClientEngine() {
34 | // default "java-sdk" name
35 | assertEquals("java-sdk", ClientEngineInfo.getClientEngineName());
36 |
37 | ClientEngineInfo.setClientEngineName(null);
38 | assertEquals("java-sdk", ClientEngineInfo.getClientEngineName());
39 |
40 | ClientEngineInfo.setClientEngineName("");
41 | assertEquals("java-sdk", ClientEngineInfo.getClientEngineName());
42 |
43 | ClientEngineInfo.setClientEngineName("test-name");
44 | assertEquals("test-name", ClientEngineInfo.getClientEngineName());
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/internal/JsonParserProviderTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.internal;
17 |
18 | import org.junit.After;
19 | import org.junit.Before;
20 | import org.junit.Test;
21 | import static org.junit.Assert.*;
22 |
23 | public class JsonParserProviderTest {
24 | @Before
25 | @After
26 | public void clearParserSystemProperty() {
27 | PropertyUtils.clear("default_parser");
28 | }
29 |
30 | @Test
31 | public void getGsonParserProviderWhenNoDefaultIsSet() {
32 | assertEquals(JsonParserProvider.GSON_CONFIG_PARSER, JsonParserProvider.getDefaultParser());
33 | }
34 |
35 | @Test
36 | public void getCorrectParserProviderWhenValidDefaultIsProvided() {
37 | PropertyUtils.set("default_parser", "JSON_CONFIG_PARSER");
38 | assertEquals(JsonParserProvider.JSON_CONFIG_PARSER, JsonParserProvider.getDefaultParser());
39 | }
40 |
41 | @Test
42 | public void getGsonParserWhenProvidedDefaultParserDoesNotExist() {
43 | PropertyUtils.set("default_parser", "GARBAGE_VALUE");
44 | assertEquals(JsonParserProvider.GSON_CONFIG_PARSER, JsonParserProvider.getDefaultParser());
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/ProjectConfigManager.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, 2023, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | public interface ProjectConfigManager {
22 | /**
23 | * Implementations of this method should block until a datafile is available.
24 | *
25 | * @return ProjectConfig
26 | */
27 | ProjectConfig getConfig();
28 |
29 | /**
30 | * Implementations of this method should not block until a datafile is available, instead return current cached project configuration.
31 | * return null if ProjectConfig is not ready at the moment.
32 | *
33 | * NOTE: To use ODP segments, implementation of this function is required to return current project configuration.
34 | * @return ProjectConfig
35 | */
36 | @Nullable
37 | ProjectConfig getCachedConfig();
38 |
39 | /**
40 | * Implementations of this method should return SDK key. If there is no SDKKey then it should return null.
41 | *
42 | * NOTE: To update ODP segments configuration via polling, it is required to return sdkKey.
43 | * @return String
44 | */
45 | @Nullable
46 | String getSDKKey();
47 | }
48 |
49 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/OptimizelyDecisionContext.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import javax.annotation.Nonnull;
20 | import javax.annotation.Nullable;
21 |
22 | public class OptimizelyDecisionContext {
23 | public static final String OPTI_NULL_RULE_KEY = "$opt-null-rule-key";
24 | public static final String OPTI_KEY_DIVIDER = "-$opt$-";
25 |
26 | private String flagKey;
27 | private String ruleKey;
28 |
29 | public OptimizelyDecisionContext(@Nonnull String flagKey, @Nullable String ruleKey) {
30 | if (flagKey == null) throw new NullPointerException("FlagKey must not be null, please provide a valid input.");
31 | this.flagKey = flagKey;
32 | this.ruleKey = ruleKey;
33 | }
34 |
35 | public String getFlagKey() {
36 | return flagKey;
37 | }
38 |
39 | public String getRuleKey() {
40 | return ruleKey != null ? ruleKey : OPTI_NULL_RULE_KEY;
41 | }
42 |
43 | public String getKey() {
44 | StringBuilder keyBuilder = new StringBuilder();
45 | keyBuilder.append(flagKey);
46 | keyBuilder.append(OPTI_KEY_DIVIDER);
47 | keyBuilder.append(getRuleKey());
48 | return keyBuilder.toString();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/EventEndpoints.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal;
18 | import java.util.HashMap;
19 | import java.util.Map;
20 |
21 | /**
22 | * EventEndpoints provides region-specific endpoint URLs for Optimizely events.
23 | * Similar to the TypeScript logxEndpoint configuration.
24 | */
25 | public class EventEndpoints {
26 |
27 | private static final Map LOGX_ENDPOINTS = new HashMap<>();
28 |
29 | static {
30 | LOGX_ENDPOINTS.put("US", "https://logx.optimizely.com/v1/events");
31 | LOGX_ENDPOINTS.put("EU", "https://eu.logx.optimizely.com/v1/events");
32 | }
33 |
34 | /**
35 | * Get the event endpoint URL for the specified region.
36 | * Defaults to US region endpoint if region is null.
37 | *
38 | * @param region the region for which to get the endpoint
39 | * @return the endpoint URL for the specified region, or US endpoint if region is null
40 | */
41 | public static String getEndpointForRegion(String region) {
42 | if (region != null && region.equals("EU")) {
43 | return LOGX_ENDPOINTS.get("EU");
44 | }
45 | return LOGX_ENDPOINTS.get("US");
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/JsonSerializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event.internal.serializer;
18 |
19 | import org.json.JSONObject;
20 |
21 | class JsonSerializer implements Serializer {
22 |
23 | public String serialize(T payload) {
24 | JSONObject payloadJsonObject = new JSONObject(payload);
25 | String jsonResponse = payloadJsonObject.toString();
26 | StringBuilder stringBuilder = new StringBuilder();
27 |
28 | for (int i = 0; i < jsonResponse.length(); i++) {
29 | Character ch = jsonResponse.charAt(i);
30 | Character nextChar = null;
31 | if (i + 1 < jsonResponse.length()) {
32 | nextChar = jsonResponse.charAt(i + 1);
33 | }
34 | if ((Character.isLetter(ch) || Character.isDigit(ch)) && Character.isUpperCase(ch) &&
35 | ((Character.isLetter(nextChar) || Character.isDigit(nextChar)))) {
36 | stringBuilder.append('_');
37 | stringBuilder.append(Character.toLowerCase(ch));
38 | } else {
39 | stringBuilder.append(ch);
40 | }
41 | }
42 |
43 | return stringBuilder.toString();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/TrackNotificationListenerInterface.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | import com.optimizely.ab.event.LogEvent;
20 |
21 | import javax.annotation.Nonnull;
22 | import java.util.Map;
23 |
24 | /**
25 | * TrackNotificationListenerInterface provides an interface for track event notification.
26 | *
27 | * @deprecated and users should implement NotificationHandler<TrackNotification> directly.
28 | */
29 | @Deprecated
30 | public interface TrackNotificationListenerInterface {
31 | /**
32 | * onTrack is called when a track event is triggered
33 | *
34 | * @param eventKey - The event key that was triggered.
35 | * @param userId - user id passed into track.
36 | * @param attributes - filtered attributes list after passed into track
37 | * @param eventTags - event tags if any were passed in.
38 | * @param event - The event being recorded.
39 | */
40 | public void onTrack(@Nonnull String eventKey,
41 | @Nonnull String userId,
42 | @Nonnull Map attributes,
43 | @Nonnull Map eventTags,
44 | @Nonnull LogEvent event);
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/cmab/service/CmabDecision.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2025, Optimizely
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.cmab.service;
17 |
18 | import java.util.Objects;
19 |
20 | public class CmabDecision {
21 | private final String variationId;
22 | private final String cmabUuid;
23 |
24 | public CmabDecision(String variationId, String cmabUuid) {
25 | this.variationId = variationId;
26 | this.cmabUuid = cmabUuid;
27 | }
28 |
29 | public String getVariationId() {
30 | return variationId;
31 | }
32 |
33 | public String getCmabUuid() {
34 | return cmabUuid;
35 | }
36 |
37 | @Override
38 | public String toString() {
39 | return "CmabDecision{" +
40 | "variationId='" + variationId + '\'' +
41 | ", cmabUuid='" + cmabUuid + '\'' +
42 | '}';
43 | }
44 |
45 | @Override
46 | public boolean equals(Object o) {
47 | if (this == o) return true;
48 | if (o == null || getClass() != o.getClass()) return false;
49 | CmabDecision that = (CmabDecision) o;
50 | return Objects.equals(variationId, that.variationId) &&
51 | Objects.equals(cmabUuid, that.cmabUuid);
52 | }
53 |
54 | @Override
55 | public int hashCode() {
56 | return Objects.hash(variationId, cmabUuid);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/internal/NotificationRegistry.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2023, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import com.optimizely.ab.notification.NotificationCenter;
20 |
21 | import javax.annotation.Nonnull;
22 | import java.util.Map;
23 | import java.util.concurrent.ConcurrentHashMap;
24 |
25 | public class NotificationRegistry {
26 | private final static Map _notificationCenters = new ConcurrentHashMap<>();
27 |
28 | private NotificationRegistry()
29 | {
30 | }
31 |
32 | public static NotificationCenter getInternalNotificationCenter(@Nonnull String sdkKey)
33 | {
34 | NotificationCenter notificationCenter = null;
35 | if (sdkKey != null) {
36 | if (_notificationCenters.containsKey(sdkKey)) {
37 | notificationCenter = _notificationCenters.get(sdkKey);
38 | } else {
39 | notificationCenter = new NotificationCenter();
40 | _notificationCenters.put(sdkKey, notificationCenter);
41 | }
42 | }
43 | return notificationCenter;
44 | }
45 |
46 | public static void clearNotificationCenterRegistry(@Nonnull String sdkKey) {
47 | if (sdkKey != null) {
48 | _notificationCenters.remove(sdkKey);
49 | }
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/ConfigParser.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017,2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 |
21 | import javax.annotation.Nonnull;
22 |
23 | /**
24 | * Config parser wrapper to allow multiple library implementations to be used.
25 | *
26 | * @see GsonConfigParser
27 | * @see JacksonConfigParser
28 | * @see JsonConfigParser
29 | * @see JsonConfigParser
30 | *
31 | * @see Project JSON
32 | */
33 | public interface ConfigParser {
34 |
35 | /**
36 | * @param json The json to parse
37 | * @return The {@code ProjectConfig} configuration from the provided json
38 | * @throws ConfigParseException when there's an issue parsing the provided project config
39 | */
40 | ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException;
41 |
42 | /**
43 | * OptimizelyJSON parsing
44 | *
45 | * @param src The OptimizelyJSON
46 | * @return The serialized String
47 | * @throws JsonParseException when parsing JSON fails
48 | */
49 | String toJson(Object src) throws JsonParseException;
50 | T fromJson(String json, Class clazz) throws JsonParseException;
51 | }
52 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/optimizelyconfig/OptimizelyVariableTest.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2020, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 | package com.optimizely.ab.optimizelyconfig;
17 |
18 | import org.junit.Test;
19 |
20 | import static org.junit.Assert.assertEquals;
21 |
22 | public class OptimizelyVariableTest {
23 |
24 | @Test
25 | public void testOptimizelyVariable() {
26 | OptimizelyVariable optimizelyVariable = new OptimizelyVariable(
27 | "7",
28 | "test_variable_key",
29 | "integer",
30 | "10"
31 | );
32 | assertEquals("7", optimizelyVariable.getId());
33 | assertEquals("test_variable_key", optimizelyVariable.getKey());
34 | assertEquals("integer", optimizelyVariable.getType());
35 | assertEquals("10", optimizelyVariable.getValue());
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/odp/parser/ResponseJsonParserFactoryTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.parser;
17 |
18 | import com.optimizely.ab.internal.PropertyUtils;
19 | import com.optimizely.ab.odp.parser.impl.GsonParser;
20 | import com.optimizely.ab.odp.parser.impl.JsonParser;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import static org.junit.Assert.*;
26 |
27 | public class ResponseJsonParserFactoryTest {
28 | @Before
29 | @After
30 | public void clearParserSystemProperty() {
31 | PropertyUtils.clear("default_parser");
32 | }
33 |
34 | @Test
35 | public void getGsonParserWhenNoDefaultIsSet() {
36 | assertEquals(GsonParser.class, ResponseJsonParserFactory.getParser().getClass());
37 | }
38 |
39 | @Test
40 | public void getCorrectParserWhenValidDefaultIsProvided() {
41 | PropertyUtils.set("default_parser", "JSON_CONFIG_PARSER");
42 | assertEquals(JsonParser.class, ResponseJsonParserFactory.getParser().getClass());
43 | }
44 |
45 | @Test
46 | public void getGsonParserWhenGivenDefaultParserDoesNotExist() {
47 | PropertyUtils.set("default_parser", "GARBAGE_VALUE");
48 | assertEquals(GsonParser.class, ResponseJsonParserFactory.getParser().getClass());
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/.github/workflows/integration_test.yml:
--------------------------------------------------------------------------------
1 | name: Reusable action of running integration of production suite
2 |
3 | on:
4 | workflow_call:
5 | inputs:
6 | FULLSTACK_TEST_REPO:
7 | required: false
8 | type: string
9 | secrets:
10 | CI_USER_TOKEN:
11 | required: true
12 | jobs:
13 | test:
14 | runs-on: ubuntu-latest
15 | steps:
16 | - uses: actions/checkout@v4
17 | with:
18 | # You should create a personal access token and store it in your repository
19 | token: ${{ secrets.CI_USER_TOKEN }}
20 | repository: 'optimizely/ci-helper-tools'
21 | path: 'home/runner/ci-helper-tools'
22 | ref: 'master'
23 | - name: set SDK Branch if PR
24 | env:
25 | HEAD_REF: ${{ github.head_ref }}
26 | if: ${{ github.event_name == 'pull_request' }}
27 | run: |
28 | echo "SDK_BRANCH=$HEAD_REF" >> $GITHUB_ENV
29 | - name: set SDK Branch if not pull request
30 | env:
31 | REF_NAME: ${{ github.ref_name }}
32 | if: ${{ github.event_name != 'pull_request' }}
33 | run: |
34 | echo "SDK_BRANCH=$REF_NAME" >> $GITHUB_ENV
35 | - name: Trigger build
36 | env:
37 | SDK: java
38 | FULLSTACK_TEST_REPO: ${{ inputs.FULLSTACK_TEST_REPO }}
39 | BUILD_NUMBER: ${{ github.run_id }}
40 | TESTAPP_BRANCH: master
41 | GITHUB_TOKEN: ${{ secrets.CI_USER_TOKEN }}
42 | EVENT_TYPE: ${{ github.event_name }}
43 | GITHUB_CONTEXT: ${{ toJson(github) }}
44 | PULL_REQUEST_SLUG: ${{ github.repository }}
45 | UPSTREAM_REPO: ${{ github.repository }}
46 | PULL_REQUEST_SHA: ${{ github.event.pull_request.head.sha }}
47 | PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
48 | UPSTREAM_SHA: ${{ github.sha }}
49 | EVENT_MESSAGE: ${{ github.event.message }}
50 | HOME: 'home/runner'
51 | run: |
52 | echo "$GITHUB_CONTEXT"
53 | home/runner/ci-helper-tools/trigger-script-with-status-update.sh
54 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/internal/SafetyUtilsTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.internal;
18 |
19 | import org.junit.Test;
20 |
21 | import java.io.Closeable;
22 | import java.io.IOException;
23 |
24 | import static org.junit.Assert.*;
25 | import static org.mockito.Mockito.doThrow;
26 | import static org.mockito.Mockito.mock;
27 | import static org.mockito.Mockito.verify;
28 |
29 | public class SafetyUtilsTest {
30 |
31 | @Test
32 | public void tryCloseAutoCloseable() throws Exception {
33 | AutoCloseable autocloseable = mock(AutoCloseable.class);
34 | SafetyUtils.tryClose(autocloseable);
35 |
36 | verify(autocloseable).close();
37 | }
38 |
39 | @Test
40 | public void tryCloseCloseable() throws Exception {
41 | Closeable closeable = mock(Closeable.class);
42 | SafetyUtils.tryClose(closeable);
43 |
44 | verify(closeable).close();
45 | }
46 |
47 | @Test
48 | public void tryCloseNullDoesNotThrow() throws Exception {
49 | SafetyUtils.tryClose(null);
50 | }
51 |
52 | @Test
53 | public void tryCloseExceptionDoesNotThrow() throws Exception {
54 | AutoCloseable autocloseable = mock(AutoCloseable.class);
55 | doThrow(new RuntimeException()).when(autocloseable).close();
56 | SafetyUtils.tryClose(autocloseable);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/TrafficAllocation.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | /**
24 | * Represents the Optimizely Traffic Allocation configuration.
25 | *
26 | * @see Project JSON
27 | */
28 | @JsonIgnoreProperties(ignoreUnknown = true)
29 | public class TrafficAllocation {
30 |
31 | private final String entityId;
32 | private final int endOfRange;
33 |
34 | @JsonCreator
35 | public TrafficAllocation(@JsonProperty("entityId") String entityId,
36 | @JsonProperty("endOfRange") int endOfRange) {
37 | this.entityId = entityId;
38 | this.endOfRange = endOfRange;
39 | }
40 |
41 | public String getEntityId() {
42 | return entityId;
43 | }
44 |
45 | public int getEndOfRange() {
46 | return endOfRange;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "TrafficAllocation{" +
52 | "entityId='" + entityId + '\'' +
53 | ", endOfRange=" + endOfRange +
54 | '}';
55 | }
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/audience/match/ExactMatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2020, 2022, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.audience.match;
18 |
19 | import javax.annotation.Nullable;
20 |
21 | import static com.optimizely.ab.internal.AttributesUtil.isValidNumber;
22 |
23 | /**
24 | * ExactMatch supports matching Numbers, Strings and Booleans. Numbers are first converted to doubles
25 | * before the comparison is evaluated. See {@link NumberComparator} Strings and Booleans are evaulated
26 | * via the Object equals method.
27 | */
28 | class ExactMatch implements Match {
29 | @Nullable
30 | public Boolean eval(Object conditionValue, Object attributeValue) throws UnexpectedValueTypeException {
31 | if (attributeValue == null) return null;
32 |
33 | if (isValidNumber(attributeValue)) {
34 | if (isValidNumber(conditionValue)) {
35 | return NumberComparator.compareUnsafe(attributeValue, conditionValue) == 0;
36 | }
37 | return null;
38 | }
39 |
40 | if (!(conditionValue instanceof String || conditionValue instanceof Boolean)) {
41 | throw new UnexpectedValueTypeException();
42 | }
43 |
44 | if (attributeValue.getClass() != conditionValue.getClass()) {
45 | return null;
46 | }
47 |
48 | return conditionValue.equals(attributeValue);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/Rollout.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | import javax.annotation.concurrent.Immutable;
24 | import java.util.List;
25 |
26 | /**
27 | * Represents a Optimizely Rollout configuration
28 | *
29 | * @see Project JSON
30 | */
31 | @Immutable
32 | @JsonIgnoreProperties(ignoreUnknown = true)
33 | public class Rollout implements IdMapped {
34 |
35 | private final String id;
36 | private final List experiments;
37 |
38 | @JsonCreator
39 | public Rollout(@JsonProperty("id") String id,
40 | @JsonProperty("experiments") List experiments) {
41 | this.id = id;
42 | this.experiments = experiments;
43 | }
44 |
45 | @Override
46 | public String getId() {
47 | return id;
48 | }
49 |
50 | public List getExperiments() {
51 | return experiments;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "Rollout{" +
57 | "id='" + id + '\'' +
58 | ", experiments=" + experiments +
59 | '}';
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/optimizelyconfig/OptimizelyAttributeTest.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2021, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 | package com.optimizely.ab.optimizelyconfig;
17 |
18 | import org.junit.Test;
19 |
20 | import static org.junit.Assert.assertEquals;
21 |
22 | public class OptimizelyAttributeTest {
23 |
24 | @Test
25 | public void testOptimizelyAttribute() {
26 | OptimizelyAttribute optimizelyAttribute1 = new OptimizelyAttribute(
27 | "5",
28 | "test_attribute"
29 | );
30 | OptimizelyAttribute optimizelyAttribute2 = new OptimizelyAttribute(
31 | "5",
32 | "test_attribute"
33 | );
34 | assertEquals("5", optimizelyAttribute1.getId());
35 | assertEquals("test_attribute", optimizelyAttribute1.getKey());
36 | assertEquals(optimizelyAttribute1, optimizelyAttribute2);
37 | assertEquals(optimizelyAttribute1.hashCode(), optimizelyAttribute2.hashCode());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/gradle/publish.gradle:
--------------------------------------------------------------------------------
1 | ext.s3Credentials = {
2 | accessKey System.getenv('AWS_ACCESS_KEY_ID')
3 | secretKey System.getenv('AWS_SECRET_ACCESS_KEY')
4 | }
5 |
6 | subprojects {
7 | // Add a per-project function to include maven artifact publication.
8 | // To enable publication in a sub-project, include 'publishArtifacts()' in its build.
9 | ext.publishArtifacts = {
10 | apply plugin: 'maven-publish'
11 |
12 | publishing {
13 | publications {
14 | mavanJava(MavenPublication) {
15 | from components.java
16 | artifact sourcesJar
17 | artifact javadocJar
18 | pom.withXml {
19 | asNode().children().last() + {
20 | resolveStrategy = Closure.DELEGATE_FIRST
21 | url 'https://github.com/optimizely/java-sdk'
22 | licenses {
23 | license {
24 | name 'The Apache Software License, Version 2.0'
25 | url 'http://www.apache.org/license/LICENSE-2.0.txt'
26 | distribution 'repo'
27 | }
28 | }
29 | developers {
30 | developer {
31 | id 'optimizely'
32 | name 'Optimizely'
33 | email 'developers@optimizely.com'
34 | }
35 | }
36 | }
37 | }
38 | }
39 | }
40 | }
41 |
42 | publishing {
43 | repositories {
44 | maven {
45 | url "s3://$mavenS3Bucket"
46 | credentials(AwsCredentials, s3Credentials)
47 | }
48 | }
49 | }
50 |
51 | // publish artifacts as part of the release process
52 | // afterReleaseBuild.dependsOn publish
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/parser/ResponseJsonParserFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.parser;
17 |
18 | import com.optimizely.ab.internal.JsonParserProvider;
19 | import com.optimizely.ab.odp.parser.impl.GsonParser;
20 | import com.optimizely.ab.odp.parser.impl.JacksonParser;
21 | import com.optimizely.ab.odp.parser.impl.JsonParser;
22 | import com.optimizely.ab.odp.parser.impl.JsonSimpleParser;
23 | import org.slf4j.Logger;
24 | import org.slf4j.LoggerFactory;
25 |
26 | public class ResponseJsonParserFactory {
27 | private static final Logger logger = LoggerFactory.getLogger(ResponseJsonParserFactory.class);
28 |
29 | public static ResponseJsonParser getParser() {
30 | JsonParserProvider parserProvider = JsonParserProvider.getDefaultParser();
31 | ResponseJsonParser jsonParser = null;
32 | switch (parserProvider) {
33 | case GSON_CONFIG_PARSER:
34 | jsonParser = new GsonParser();
35 | break;
36 | case JACKSON_CONFIG_PARSER:
37 | jsonParser = new JacksonParser();
38 | break;
39 | case JSON_CONFIG_PARSER:
40 | jsonParser = new JsonParser();
41 | break;
42 | case JSON_SIMPLE_CONFIG_PARSER:
43 | jsonParser = new JsonSimpleParser();
44 | break;
45 | }
46 | logger.debug("Using " + parserProvider.toString() + " parser");
47 | return jsonParser;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/optimizelyconfig/OptimizelyEventTest.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2020-2021, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 | package com.optimizely.ab.optimizelyconfig;
17 | import org.junit.Test;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static java.util.Arrays.asList;
21 |
22 | public class OptimizelyEventTest {
23 | @Test
24 | public void testOptimizelyEvent() {
25 | OptimizelyEvent optimizelyEvent1 = new OptimizelyEvent(
26 | "5",
27 | "test_event",
28 | asList("123","234","345")
29 | );
30 | OptimizelyEvent optimizelyEvent2 = new OptimizelyEvent(
31 | "5",
32 | "test_event",
33 | asList("123","234","345")
34 | );
35 | assertEquals("5", optimizelyEvent1.getId());
36 | assertEquals("test_event", optimizelyEvent1.getKey());
37 | assertEquals(optimizelyEvent1, optimizelyEvent2);
38 | assertEquals(optimizelyEvent1.hashCode(), optimizelyEvent2.hashCode());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/event/ForwardingEventProcessor.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.event;
18 |
19 | import com.optimizely.ab.event.internal.EventFactory;
20 | import com.optimizely.ab.event.internal.UserEvent;
21 | import com.optimizely.ab.notification.NotificationCenter;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 |
25 | /**
26 | * ForwardingEventProcessor is a basic transformation stage for converting
27 | * the event batch into a LogEvent to be dispatched.
28 | */
29 | public class ForwardingEventProcessor implements EventProcessor {
30 |
31 | private static final Logger logger = LoggerFactory.getLogger(ForwardingEventProcessor.class);
32 |
33 | private final EventHandler eventHandler;
34 | private final NotificationCenter notificationCenter;
35 |
36 | public ForwardingEventProcessor(EventHandler eventHandler, NotificationCenter notificationCenter) {
37 | this.eventHandler = eventHandler;
38 | this.notificationCenter = notificationCenter;
39 | }
40 |
41 | @Override
42 | public void process(UserEvent userEvent) {
43 | LogEvent logEvent = EventFactory.createLogEvent(userEvent);
44 |
45 | if (notificationCenter != null) {
46 | notificationCenter.send(logEvent);
47 | }
48 |
49 | try {
50 | eventHandler.dispatchEvent(logEvent);
51 | } catch(Exception e) {
52 | logger.error("Error dispatching event: {}", logEvent, e);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/ActivateNotificationListenerInterface.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.Map;
20 |
21 | import javax.annotation.Nonnull;
22 |
23 | import com.optimizely.ab.config.Experiment;
24 | import com.optimizely.ab.config.Variation;
25 | import com.optimizely.ab.event.LogEvent;
26 |
27 | /**
28 | * ActivateNotificationListenerInterface provides and interface for activate event notification.
29 | *
30 | * @deprecated along with {@link ActivateNotification} and {@link ActivateNotificationListener}
31 | * and users should implement NotificationHandler<DecisionNotification> directly.
32 | */
33 | @Deprecated
34 | public interface ActivateNotificationListenerInterface {
35 | /**
36 | * onActivate called when an activate was triggered
37 | *
38 | * @param experiment - The experiment object being activated.
39 | * @param userId - The userId passed into activate.
40 | * @param attributes - The filtered attribute list passed into activate
41 | * @param variation - The variation that was returned from activate.
42 | * @param event - The impression event that was triggered.
43 | */
44 | public void onActivate(@Nonnull Experiment experiment,
45 | @Nonnull String userId,
46 | @Nonnull Map attributes,
47 | @Nonnull Variation variation,
48 | @Nonnull LogEvent event);
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/FeatureVariableUsageInstance.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | /**
24 | * Represents the value of a feature variable for a variation
25 | */
26 | @JsonIgnoreProperties(ignoreUnknown = true)
27 | public class FeatureVariableUsageInstance implements IdMapped {
28 |
29 | private final String id;
30 | private final String value;
31 |
32 | @JsonCreator
33 | public FeatureVariableUsageInstance(@JsonProperty("id") String id,
34 | @JsonProperty("value") String value) {
35 | this.id = id;
36 | this.value = value;
37 | }
38 |
39 | public String getId() {
40 | return id;
41 | }
42 |
43 | public String getValue() {
44 | return value;
45 | }
46 |
47 | @Override
48 | public boolean equals(Object o) {
49 | if (this == o) return true;
50 | if (o == null || getClass() != o.getClass()) return false;
51 |
52 | FeatureVariableUsageInstance that = (FeatureVariableUsageInstance) o;
53 |
54 | return id.equals(that.id) && value.equals(that.value);
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | int result = id.hashCode();
60 | result = 31 * result + value.hashCode();
61 | return result;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/core-api/src/test/java/com/optimizely/ab/OptimizelyDecisionContextTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2021, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab;
18 |
19 | import org.junit.Test;
20 | import static junit.framework.TestCase.assertEquals;
21 |
22 | public class OptimizelyDecisionContextTest {
23 |
24 | @Test
25 | public void initializeOptimizelyDecisionContextWithFlagKeyAndRuleKey() {
26 | String flagKey = "test-flag-key";
27 | String ruleKey = "1029384756";
28 | String expectedKey = flagKey + OptimizelyDecisionContext.OPTI_KEY_DIVIDER + ruleKey;
29 | OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
30 | assertEquals(flagKey, optimizelyDecisionContext.getFlagKey());
31 | assertEquals(ruleKey, optimizelyDecisionContext.getRuleKey());
32 | assertEquals(expectedKey, optimizelyDecisionContext.getKey());
33 | }
34 |
35 | @Test
36 | public void initializeOptimizelyDecisionContextWithFlagKey() {
37 | String flagKey = "test-flag-key";
38 | String expectedKey = flagKey + OptimizelyDecisionContext.OPTI_KEY_DIVIDER + OptimizelyDecisionContext.OPTI_NULL_RULE_KEY;
39 | OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
40 | assertEquals(flagKey, optimizelyDecisionContext.getFlagKey());
41 | assertEquals(OptimizelyDecisionContext.OPTI_NULL_RULE_KEY, optimizelyDecisionContext.getRuleKey());
42 | assertEquals(expectedKey, optimizelyDecisionContext.getKey());
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/core-api/src/jmh/java/com/optimizely/ab/config/parser/JacksonConfigParserBenchmark.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2018-2019 Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.optimizely.ab.config.ProjectConfig;
20 | import com.optimizely.ab.config.DatafileProjectConfigTestUtils;
21 | import org.openjdk.jmh.annotations.*;
22 |
23 | import java.io.IOException;
24 | import java.util.concurrent.TimeUnit;
25 |
26 | @BenchmarkMode(Mode.AverageTime)
27 | @OutputTimeUnit(TimeUnit.MICROSECONDS)
28 | @Fork(2)
29 | @Warmup(iterations = 10)
30 | @Measurement(iterations = 20)
31 | @State(Scope.Benchmark)
32 | public class JacksonConfigParserBenchmark {
33 | JacksonConfigParser parser;
34 | String jsonV2;
35 | String jsonV3;
36 | String jsonV4;
37 |
38 | @Setup
39 | public void setUp() throws IOException {
40 | parser = new JacksonConfigParser();
41 | jsonV2 = DatafileProjectConfigTestUtils.validConfigJsonV2();
42 | jsonV3 = DatafileProjectConfigTestUtils.validConfigJsonV3();
43 | jsonV4 = DatafileProjectConfigTestUtils.validConfigJsonV4();
44 | }
45 |
46 | @Benchmark
47 | public ProjectConfig parseV2() throws ConfigParseException {
48 | return parser.parseProjectConfig(jsonV2);
49 | }
50 |
51 | @Benchmark
52 | public ProjectConfig parseV3() throws ConfigParseException {
53 | return parser.parseProjectConfig(jsonV3);
54 | }
55 |
56 | @Benchmark
57 | public ProjectConfig parseV4() throws ConfigParseException {
58 | return parser.parseProjectConfig(jsonV4);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/notification/FeatureTestSourceInfo.java:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | * Copyright 2019, Optimizely, Inc. and contributors *
3 | * *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
5 | * you may not use this file except in compliance with the License. *
6 | * You may obtain a copy of the License at *
7 | * *
8 | * http://www.apache.org/licenses/LICENSE-2.0 *
9 | * *
10 | * Unless required by applicable law or agreed to in writing, software *
11 | * distributed under the License is distributed on an "AS IS" BASIS, *
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 | * See the License for the specific language governing permissions and *
14 | * limitations under the License. *
15 | ***************************************************************************/
16 |
17 | package com.optimizely.ab.notification;
18 |
19 | import java.util.HashMap;
20 | import java.util.Map;
21 |
22 | import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
23 | import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
24 |
25 | public class FeatureTestSourceInfo implements SourceInfo {
26 | private String experimentKey;
27 | private String variationKey;
28 |
29 | public FeatureTestSourceInfo(String experimentKey, String variationKey) {
30 | this.experimentKey = experimentKey;
31 | this.variationKey = variationKey;
32 | }
33 |
34 | @Override
35 | public Map get() {
36 | Map sourceInfo = new HashMap<>();
37 | sourceInfo.put(EXPERIMENT_KEY, experimentKey);
38 | sourceInfo.put(VARIATION_KEY, variationKey);
39 |
40 | return sourceInfo;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/odp/serializer/ODPJsonSerializerFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2022, Optimizely Inc. and contributors
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.odp.serializer;
17 |
18 | import com.optimizely.ab.internal.JsonParserProvider;
19 | import com.optimizely.ab.odp.serializer.impl.GsonSerializer;
20 | import com.optimizely.ab.odp.serializer.impl.JacksonSerializer;
21 | import com.optimizely.ab.odp.serializer.impl.JsonSerializer;
22 | import com.optimizely.ab.odp.serializer.impl.JsonSimpleSerializer;
23 | import org.slf4j.Logger;
24 | import org.slf4j.LoggerFactory;
25 |
26 | public class ODPJsonSerializerFactory {
27 | private static final Logger logger = LoggerFactory.getLogger(ODPJsonSerializerFactory.class);
28 |
29 | public static ODPJsonSerializer getSerializer() {
30 | JsonParserProvider parserProvider = JsonParserProvider.getDefaultParser();
31 | ODPJsonSerializer jsonSerializer = null;
32 | switch (parserProvider) {
33 | case GSON_CONFIG_PARSER:
34 | jsonSerializer = new GsonSerializer();
35 | break;
36 | case JACKSON_CONFIG_PARSER:
37 | jsonSerializer = new JacksonSerializer();
38 | break;
39 | case JSON_CONFIG_PARSER:
40 | jsonSerializer = new JsonSerializer();
41 | break;
42 | case JSON_SIMPLE_CONFIG_PARSER:
43 | jsonSerializer = new JsonSimpleSerializer();
44 | break;
45 | }
46 | logger.info("Using " + parserProvider.toString() + " serializer");
47 | return jsonSerializer;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2020, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.optimizelydecision;
18 |
19 | import javax.annotation.Nonnull;
20 | import javax.annotation.Nullable;
21 |
22 | public class DecisionResponse {
23 | private T result;
24 | private DecisionReasons reasons;
25 | private boolean error;
26 | private String cmabUuid;
27 |
28 | public DecisionResponse(@Nullable T result, @Nonnull DecisionReasons reasons, @Nonnull boolean error, @Nullable String cmabUuid) {
29 | this.result = result;
30 | this.reasons = reasons;
31 | this.error = error;
32 | this.cmabUuid = cmabUuid;
33 | }
34 |
35 | public DecisionResponse(@Nullable T result, @Nonnull DecisionReasons reasons) {
36 | this(result, reasons, false, null);
37 | }
38 |
39 | public static DecisionResponse responseNoReasons(@Nullable E result) {
40 | return new DecisionResponse<>(result, DefaultDecisionReasons.newInstance(), false, null);
41 | }
42 |
43 | public static DecisionResponse nullNoReasons() {
44 | return new DecisionResponse<>(null, DefaultDecisionReasons.newInstance(), false, null);
45 | }
46 |
47 | @Nullable
48 | public T getResult() {
49 | return result;
50 | }
51 |
52 | @Nonnull
53 | public DecisionReasons getReasons() {
54 | return reasons;
55 | }
56 |
57 | @Nonnull
58 | public boolean isError(){
59 | return error;
60 | }
61 |
62 | @Nullable
63 | public String getCmabUuid() {
64 | return cmabUuid;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/parser/AudienceJacksonDeserializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config.parser;
18 |
19 | import com.fasterxml.jackson.core.JsonParser;
20 | import com.fasterxml.jackson.core.ObjectCodec;
21 | import com.fasterxml.jackson.databind.DeserializationContext;
22 | import com.fasterxml.jackson.databind.JsonDeserializer;
23 | import com.fasterxml.jackson.databind.JsonNode;
24 | import com.fasterxml.jackson.databind.ObjectMapper;
25 | import com.optimizely.ab.config.audience.*;
26 |
27 | import java.io.IOException;
28 |
29 | public class AudienceJacksonDeserializer extends JsonDeserializer {
30 | private ObjectMapper objectMapper;
31 |
32 | public AudienceJacksonDeserializer() {
33 | this(new ObjectMapper());
34 | }
35 |
36 | AudienceJacksonDeserializer(ObjectMapper objectMapper) {
37 | this.objectMapper = objectMapper;
38 | }
39 |
40 | @Override
41 | public Audience deserialize(JsonParser parser, DeserializationContext context) throws IOException {
42 | ObjectCodec codec = parser.getCodec();
43 | JsonNode node = codec.readTree(parser);
44 |
45 | String id = node.get("id").textValue();
46 | String name = node.get("name").textValue();
47 |
48 | JsonNode conditionsJson = node.get("conditions");
49 | conditionsJson = objectMapper.readTree(conditionsJson.textValue());
50 |
51 | Condition conditions = ConditionJacksonDeserializer.parseCondition(UserAttribute.class, objectMapper, conditionsJson);
52 |
53 | return new Audience(id, name, conditions);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/cmab/service/CmabCacheValue.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2025, Optimizely
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.optimizely.ab.cmab.service;
17 |
18 | import java.util.Objects;
19 |
20 | public class CmabCacheValue {
21 | private final String attributesHash;
22 | private final String variationId;
23 | private final String cmabUuid;
24 |
25 | public CmabCacheValue(String attributesHash, String variationId, String cmabUuid) {
26 | this.attributesHash = attributesHash;
27 | this.variationId = variationId;
28 | this.cmabUuid = cmabUuid;
29 | }
30 |
31 | public String getAttributesHash() {
32 | return attributesHash;
33 | }
34 |
35 | public String getVariationId() {
36 | return variationId;
37 | }
38 |
39 | public String getCmabUuid() {
40 | return cmabUuid;
41 | }
42 |
43 | @Override
44 | public String toString() {
45 | return "CmabCacheValue{" +
46 | "attributesHash='" + attributesHash + '\'' +
47 | ", variationId='" + variationId + '\'' +
48 | ", cmabUuid='" + cmabUuid + '\'' +
49 | '}';
50 | }
51 |
52 | @Override
53 | public boolean equals(Object o) {
54 | if (this == o) return true;
55 | if (o == null || getClass() != o.getClass()) return false;
56 | CmabCacheValue that = (CmabCacheValue) o;
57 | return Objects.equals(attributesHash, that.attributesHash) &&
58 | Objects.equals(variationId, that.variationId) &&
59 | Objects.equals(cmabUuid, that.cmabUuid);
60 | }
61 |
62 | @Override
63 | public int hashCode() {
64 | return Objects.hash(attributesHash, variationId, cmabUuid);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/core-api/src/main/java/com/optimizely/ab/config/Attribute.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2016-2017, 2019, Optimizely and contributors
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.optimizely.ab.config;
18 |
19 | import com.fasterxml.jackson.annotation.JsonCreator;
20 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | import javax.annotation.concurrent.Immutable;
24 |
25 | /**
26 | * Represents the Optimizely Attribute configuration.
27 | *
28 | * @see Project JSON
29 | */
30 | @Immutable
31 | @JsonIgnoreProperties(ignoreUnknown = true)
32 | public class Attribute implements IdKeyMapped {
33 |
34 | private final String id;
35 | private final String key;
36 | private final String segmentId;
37 |
38 | public Attribute(String id, String key) {
39 | this(id, key, null);
40 | }
41 |
42 | @JsonCreator
43 | public Attribute(@JsonProperty("id") String id,
44 | @JsonProperty("key") String key,
45 | @JsonProperty("segmentId") String segmentId) {
46 | this.id = id;
47 | this.key = key;
48 | this.segmentId = segmentId;
49 | }
50 |
51 | public String getId() {
52 | return id;
53 | }
54 |
55 | public String getKey() {
56 | return key;
57 | }
58 |
59 | public String getSegmentId() {
60 | return segmentId;
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return "Attribute{" +
66 | "id='" + id + '\'' +
67 | ", key='" + key + '\'' +
68 | ", segmentId='" + segmentId + '\'' +
69 | '}';
70 | }
71 | }
72 |
--------------------------------------------------------------------------------