<commit_before>package com.sdl.dxa.modules.context.builder;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.sdl.dxa.api.datamodel.model.EntityModelData;
import com.sdl.dxa.api.datamodel.model.util.ListWrapper;
import com.sdl.dxa.caching.wrapper.EntitiesCache;
import com.sdl.dxa.modules.context.model.Conditions;
import com.sdl.webapp.common.api.model.EntityModel;
import com.sdl.webapp.common.api.model.entity.AbstractEntityModel;
import com.sdl.webapp.common.exceptions.DxaException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Collections;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

public class ContextExpressionModelBuilderTest {

    private ContextExpressionModelBuilder builder;

    @Before
    public void init() {
        EntitiesCache cache = spy(EntitiesCache.class);
        doReturn(null).when(cache).getSpecificKey(any(), anyVararg());
        doReturn(false).when(cache).containsKey(any());
        doReturn(false).when(cache).isCachingEnabled();


        builder = new ContextExpressionModelBuilder(cache);
        ReflectionTestUtils.setField(builder, "entitiesCache", cache);
        ReflectionTestUtils.setField(builder, "cxKeyR2", "CX");
    }

    @Test
    public void shouldNotChangeEntityBecauseExtensionDataIsNullOrHasNoContextExpressions() throws Exception {
        //given
        TestEntity testEntity = new TestEntity();

        EntityModelData entityModelData = new EntityModelData();

        //when
        EntityModel entityR2 = builder.buildEntityModel(testEntity, entityModelData, null);

        //then
        Assert.assertSame(testEntity, entityR2);

        //when
        entityModelData = EntityModelData.builder().extensionData(Collections.emptyMap()).build();
        entityR2 = builder.buildEntityModel(testEntity, entityModelData, null);

        //then
        Assert.assertSame(testEntity, entityR2);
    }

    @Test
    public void shouldParseAllConditions() throws DxaException {
        //given
        ListWrapper<String> includesList = new ListWrapper<>(Lists.newArrayList("include1", "include2"));
        ListWrapper<String> excludesList = new ListWrapper<>(Lists.newArrayList("exclude1", "exclude2"));

        TestEntity testEntity = new TestEntity();
        EntityModelData entityModelData = EntityModelData.builder()
                .extensionData(ImmutableMap.of("CX.Include", includesList, "CX.Exclude", excludesList))
                .build();

        //when
        EntityModel entityR2 = builder.buildEntityModel(testEntity, entityModelData, null);

        //then
        Conditions conditions = (Conditions) entityR2.getExtensionData().get("ContextExpressions");
        org.junit.Assert.assertTrue(conditions.getExcludes().contains("exclude1"));
        org.junit.Assert.assertTrue(conditions.getExcludes().contains("exclude2"));
        org.junit.Assert.assertTrue(conditions.getIncludes().contains("include1"));
        org.junit.Assert.assertTrue(conditions.getIncludes().contains("include2"));

    }

    @Test
    public void shouldNotHaveHighPriority() {
        Assert.assertTrue(builder.getOrder() >= 0);
    }

    private static class TestEntity extends AbstractEntityModel {

    }
}<commit_msg>TSI-2759 > Support of a changed CX ExtensionData in the CX module [Java]. Fixed broken test<commit_after>package com.sdl.dxa.modules.context.builder;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.sdl.dxa.api.datamodel.model.ContentModelData;
import com.sdl.dxa.api.datamodel.model.EntityModelData;
import com.sdl.dxa.api.datamodel.model.util.ListWrapper;
import com.sdl.dxa.caching.wrapper.EntitiesCache;
import com.sdl.dxa.modules.context.model.Conditions;
import com.sdl.webapp.common.api.model.EntityModel;
import com.sdl.webapp.common.api.model.entity.AbstractEntityModel;
import com.sdl.webapp.common.exceptions.DxaException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Collections;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

public class ContextExpressionModelBuilderTest {

    private ContextExpressionModelBuilder builder;

    @Before
    public void init() {
        EntitiesCache cache = spy(EntitiesCache.class);
        doReturn(null).when(cache).getSpecificKey(any(), anyVararg());
        doReturn(false).when(cache).containsKey(any());
        doReturn(false).when(cache).isCachingEnabled();


        builder = new ContextExpressionModelBuilder(cache);
        ReflectionTestUtils.setField(builder, "entitiesCache", cache);
    }

    @Test
    public void shouldNotChangeEntityBecauseExtensionDataIsNullOrHasNoContextExpressions() throws Exception {
        //given
        TestEntity testEntity = new TestEntity();

        EntityModelData entityModelData = new EntityModelData();

        //when
        EntityModel entityR2 = builder.buildEntityModel(testEntity, entityModelData, null);

        //then
        Assert.assertSame(testEntity, entityR2);

        //when
        entityModelData = EntityModelData.builder().extensionData(Collections.emptyMap()).build();
        entityR2 = builder.buildEntityModel(testEntity, entityModelData, null);

        //then
        Assert.assertSame(testEntity, entityR2);
    }

    @Test
    public void shouldParseAllConditions() throws DxaException {
        //given
        ListWrapper<String> includesList = new ListWrapper<>(Lists.newArrayList("include1", "include2"));
        ListWrapper<String> excludesList = new ListWrapper<>(Lists.newArrayList("exclude1", "exclude2"));

        TestEntity testEntity = new TestEntity();
        EntityModelData entityModelData = EntityModelData.builder()
                .extensionData(ImmutableMap.of("ContextExpressions", new ContentModelData(ImmutableMap.of("Include", includesList, "Exclude", excludesList))))
                .build();

        //when
        EntityModel entityR2 = builder.buildEntityModel(testEntity, entityModelData, null);

        //then
        Conditions conditions = (Conditions) entityR2.getExtensionData().get("ContextExpressions");
        org.junit.Assert.assertTrue(conditions.getExcludes().contains("exclude1"));
        org.junit.Assert.assertTrue(conditions.getExcludes().contains("exclude2"));
        org.junit.Assert.assertTrue(conditions.getIncludes().contains("include1"));
        org.junit.Assert.assertTrue(conditions.getIncludes().contains("include2"));

    }

    @Test
    public void shouldNotHaveHighPriority() {
        Assert.assertTrue(builder.getOrder() >= 0);
    }

    private static class TestEntity extends AbstractEntityModel {

    }
}
{'fraction_non_alphanumeric': 0.11224489795918367, 'fraction_numerical': 0.00496031746031746, 'mean_word_length': 3.9453398738612475, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822698', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1915, 'n_words': 331}