<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.apache.isis.core.runtimeservices.wrapper;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

import org.jmock.Expectations;
import org.jmock.auto.Mock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.apache.isis.applib.services.command.Command;
import org.apache.isis.applib.services.command.CommandContext;
import org.apache.isis.applib.services.factory.FactoryService;
import org.apache.isis.applib.services.message.MessageService;
import org.apache.isis.applib.services.wrapper.DisabledException;
import org.apache.isis.applib.services.wrapper.HiddenException;
import org.apache.isis.applib.services.wrapper.InvalidException;
import org.apache.isis.applib.services.xactn.TransactionService;
import org.apache.isis.core.codegen.bytebuddy.services.ProxyFactoryServiceByteBuddy;
import org.apache.isis.core.commons.internal.plugins.codegen.ProxyFactoryService;
import org.apache.isis.core.config.unittestsupport.internal._Config;
import org.apache.isis.core.metamodel.MetaModelContext_forTesting;
import org.apache.isis.core.metamodel.context.MetaModelContext;
import org.apache.isis.core.metamodel.facetapi.FacetUtil;
import org.apache.isis.core.metamodel.facets.FacetedMethod;
import org.apache.isis.core.metamodel.facets.all.named.NamedFacetInferred;
import org.apache.isis.core.metamodel.facets.members.disabled.method.DisableForContextFacetViaMethod;
import org.apache.isis.core.metamodel.facets.members.hidden.method.HideForContextFacetViaMethod;
import org.apache.isis.core.metamodel.facets.object.entity.EntityFacet;
import org.apache.isis.core.metamodel.facets.properties.accessor.PropertyAccessorFacetViaAccessor;
import org.apache.isis.core.metamodel.facets.properties.update.clear.PropertyClearFacetViaClearMethod;
import org.apache.isis.core.metamodel.facets.properties.update.init.PropertyInitializationFacetViaSetterMethod;
import org.apache.isis.core.metamodel.facets.properties.update.modify.PropertySetterFacetViaModifyMethod;
import org.apache.isis.core.metamodel.facets.properties.validating.method.PropertyValidateFacetViaMethod;
import org.apache.isis.core.metamodel.objectmanager.ObjectManager;
import org.apache.isis.core.metamodel.services.command.CommandDtoServiceInternal;
import org.apache.isis.core.metamodel.spec.ManagedObject;
import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.feature.ObjectMember;
import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;
import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
import org.apache.isis.core.metamodel.specloader.specimpl.OneToOneAssociationDefault;
import org.apache.isis.core.metamodel.specloader.specimpl.dflt.ObjectSpecificationDefault;
import org.apache.isis.core.runtime.session.IsisSessionFactory;
import org.apache.isis.core.runtimeservices.wrapper.dom.employees.Employee;
import org.apache.isis.core.runtimeservices.wrapper.dom.employees.EmployeeRepository;
import org.apache.isis.core.runtimeservices.wrapper.dom.employees.EmployeeRepositoryImpl;
import org.apache.isis.core.security.authentication.AuthenticationSessionProvider;
import org.apache.isis.core.security.authentication.standard.SimpleSession;
import org.apache.isis.core.internaltestsupport.jmocking.JUnitRuleMockery2;
import org.apache.isis.core.internaltestsupport.jmocking.JUnitRuleMockery2.Mode;
import org.apache.isis.schema.cmd.v2.CommandDto;

import lombok.val;

public class WrapperFactoryDefault_wrappedObject_Test {

    @Rule
    public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Mock private AuthenticationSessionProvider mockAuthenticationSessionProvider;
    @Mock private MessageService mockMessageService;
    @Mock private CommandContext mockCommandContext;
    @Mock private Command mockCommand;
    @Mock private CommandDtoServiceInternal mockCommandDtoServiceInternal;
    @Mock private ObjectSpecification mockOnType;
    @Mock private SpecificationLoader mockSpecificationLoader;
    @Mock private IsisSessionFactory mockIsisSessionFactory;
    @Mock private ObjectSpecificationDefault mockEmployeeSpec;
    @Mock private FactoryService mockFactoryService;
    @Mock private TransactionService mockTransactionService;
    @Mock protected ObjectManager mockObjectManager;
    
    private ObjectMember employeeNameMember;

    @Mock private ObjectSpecificationDefault mockStringSpec;
    @Mock private ManagedObject mockEmployeeAdapter;
    @Mock private ManagedObject mockAdapterForStringSmith;
    @Mock private ManagedObject mockAdapterForStringJones;

    private final SimpleSession session = new SimpleSession("tester", Collections.<String>emptyList());

    private EmployeeRepository employeeRepository;

    private Employee employeeDO;
    private Employee employeeWO;

    private WrapperFactoryDefault wrapperFactory;
    
    protected MetaModelContext metaModelContext;

    @SuppressWarnings("unchecked")
    @Before
    public void setUp() {

        // PRODUCTION
        
        val proxyFactoryService = (ProxyFactoryService) new ProxyFactoryServiceByteBuddy();

        metaModelContext = MetaModelContext_forTesting.builder()
                .specificationLoader(mockSpecificationLoader)
                .objectManager(mockObjectManager)
//                .singleton(mockPersistenceSessionServiceInternal)
                .authenticationSessionProvider(mockAuthenticationSessionProvider)
                .singleton(proxyFactoryService)
                .singleton(wrapperFactory = createWrapperFactory(proxyFactoryService))
                .singleton(mockCommandContext)
                .singleton(mockCommandDtoServiceInternal)
                .singleton(mockFactoryService)
                .singleton(mockIsisSessionFactory)
                .singleton(mockTransactionService)
                .build();
        
        metaModelContext.getServiceInjector().injectServicesInto(wrapperFactory);

        employeeRepository = new EmployeeRepositoryImpl();

        employeeDO = new Employee();
        employeeDO.setName("Smith");
        employeeDO.setEmployeeRepository(employeeRepository);

        context.checking(new Expectations() {
            {
                
                allowing(mockObjectManager).adapt(employeeDO);
                will(returnValue(mockEmployeeAdapter));

//                allowing(mockEmployeeAdapter).getOid();
//                will(returnValue(Factory.persistentOf(ObjectSpecId.of("EMP"), "1")));

                allowing(mockEmployeeSpec).isManagedBean();
                will(returnValue(true));
                
                allowing(mockEmployeeSpec).isIdentifiable();
                will(returnValue(true));
                
                allowing(mockEmployeeSpec).getCorrespondingClass();
                will(returnValue(Employee.class));

                allowing(mockStringSpec).getCorrespondingClass();
                will(returnValue(String.class));

                allowing(mockCommandDtoServiceInternal).asCommandDto(with(any(List.class)), with(any(OneToOneAssociation.class)), with(any(ManagedObject.class)));
                will(returnValue(new CommandDto()));
                
                allowing(mockCommandContext).getCommand();
                will(returnValue(mockCommand));

                allowing(mockSpecificationLoader).loadSpecification(String.class);
                will(returnValue(mockStringSpec));

                allowing(mockStringSpec).getShortIdentifier();
                will(returnValue(String.class.getName()));

                allowing(mockAuthenticationSessionProvider).getAuthenticationSession();
                will(returnValue(session));

//                allowing(mockAdapterManager).adapterFor(employeeDO);
//                will(returnValue(mockEmployeeAdapter));

                allowing(mockEmployeeAdapter).titleString(null);
                will(returnValue("titleOf[mockEmployeeAdapter]"));
                
                allowing(mockEmployeeAdapter).getSpecification();
                will(returnValue(mockEmployeeSpec));

                allowing(mockSpecificationLoader).loadSpecification(Employee.class);
                will(returnValue(mockEmployeeSpec));

                allowing(mockEmployeeSpec).getMember(methodOf(Employee.class, "getEmployeeRepository"));
                will(returnValue(null));
                
                allowing(mockEmployeeSpec).getFacet(EntityFacet.class);
                will(returnValue(null));

<commit_msg>ISIS-1970: supposed to fix tests<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.apache.isis.core.runtimeservices.wrapper;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.jmock.Expectations;
import org.jmock.auto.Mock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.apache.isis.applib.services.command.Command;
import org.apache.isis.applib.services.command.Command.Executor;
import org.apache.isis.applib.services.command.CommandContext;
import org.apache.isis.applib.services.factory.FactoryService;
import org.apache.isis.applib.services.message.MessageService;
import org.apache.isis.applib.services.wrapper.DisabledException;
import org.apache.isis.applib.services.wrapper.HiddenException;
import org.apache.isis.applib.services.wrapper.InvalidException;
import org.apache.isis.applib.services.xactn.TransactionService;
import org.apache.isis.core.codegen.bytebuddy.services.ProxyFactoryServiceByteBuddy;
import org.apache.isis.core.commons.internal.plugins.codegen.ProxyFactoryService;
import org.apache.isis.core.config.unittestsupport.internal._Config;
import org.apache.isis.core.metamodel.MetaModelContext_forTesting;
import org.apache.isis.core.metamodel.context.MetaModelContext;
import org.apache.isis.core.metamodel.facetapi.FacetUtil;
import org.apache.isis.core.metamodel.facets.FacetedMethod;
import org.apache.isis.core.metamodel.facets.all.named.NamedFacetInferred;
import org.apache.isis.core.metamodel.facets.members.disabled.method.DisableForContextFacetViaMethod;
import org.apache.isis.core.metamodel.facets.members.hidden.method.HideForContextFacetViaMethod;
import org.apache.isis.core.metamodel.facets.object.entity.EntityFacet;
import org.apache.isis.core.metamodel.facets.properties.accessor.PropertyAccessorFacetViaAccessor;
import org.apache.isis.core.metamodel.facets.properties.update.clear.PropertyClearFacetViaClearMethod;
import org.apache.isis.core.metamodel.facets.properties.update.init.PropertyInitializationFacetViaSetterMethod;
import org.apache.isis.core.metamodel.facets.properties.update.modify.PropertySetterFacetViaModifyMethod;
import org.apache.isis.core.metamodel.facets.properties.validating.method.PropertyValidateFacetViaMethod;
import org.apache.isis.core.metamodel.objectmanager.ObjectManager;
import org.apache.isis.core.metamodel.services.command.CommandDtoServiceInternal;
import org.apache.isis.core.metamodel.spec.ManagedObject;
import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.feature.ObjectMember;
import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;
import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
import org.apache.isis.core.metamodel.specloader.specimpl.OneToOneAssociationDefault;
import org.apache.isis.core.metamodel.specloader.specimpl.dflt.ObjectSpecificationDefault;
import org.apache.isis.core.runtime.session.IsisSessionFactory;
import org.apache.isis.core.runtimeservices.wrapper.dom.employees.Employee;
import org.apache.isis.core.runtimeservices.wrapper.dom.employees.EmployeeRepository;
import org.apache.isis.core.runtimeservices.wrapper.dom.employees.EmployeeRepositoryImpl;
import org.apache.isis.core.security.authentication.AuthenticationSessionProvider;
import org.apache.isis.core.security.authentication.standard.SimpleSession;
import org.apache.isis.core.internaltestsupport.jmocking.JUnitRuleMockery2;
import org.apache.isis.core.internaltestsupport.jmocking.JUnitRuleMockery2.Mode;
import org.apache.isis.schema.cmd.v2.CommandDto;

import lombok.val;

public class WrapperFactoryDefault_wrappedObject_Test {

    @Rule
    public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Mock private AuthenticationSessionProvider mockAuthenticationSessionProvider;
    @Mock private MessageService mockMessageService;
    @Mock private CommandContext mockCommandContext;
    @Mock private Command mockCommand;
    @Mock private CommandDtoServiceInternal mockCommandDtoServiceInternal;
    @Mock private ObjectSpecification mockOnType;
    @Mock private SpecificationLoader mockSpecificationLoader;
    @Mock private IsisSessionFactory mockIsisSessionFactory;
    @Mock private ObjectSpecificationDefault mockEmployeeSpec;
    @Mock private FactoryService mockFactoryService;
    @Mock private TransactionService mockTransactionService;
    @Mock protected ObjectManager mockObjectManager;
    
    private ObjectMember employeeNameMember;

    @Mock private ObjectSpecificationDefault mockStringSpec;
    @Mock private ManagedObject mockEmployeeAdapter;
    @Mock private ManagedObject mockAdapterForStringSmith;
    @Mock private ManagedObject mockAdapterForStringJones;

    private final SimpleSession session = new SimpleSession("tester", Collections.<String>emptyList());

    private EmployeeRepository employeeRepository;

    private Employee employeeDO;
    private Employee employeeWO;

    private WrapperFactoryDefault wrapperFactory;
    
    protected MetaModelContext metaModelContext;

    @SuppressWarnings("unchecked")
    @Before
    public void setUp() {

        // PRODUCTION
        
        val proxyFactoryService = (ProxyFactoryService) new ProxyFactoryServiceByteBuddy();

        metaModelContext = MetaModelContext_forTesting.builder()
                .specificationLoader(mockSpecificationLoader)
                .objectManager(mockObjectManager)
//                .singleton(mockPersistenceSessionServiceInternal)
                .authenticationSessionProvider(mockAuthenticationSessionProvider)
                .singleton(proxyFactoryService)
                .singleton(wrapperFactory = createWrapperFactory(proxyFactoryService))
                .singleton(mockCommandContext)
                .singleton(mockCommandDtoServiceInternal)
                .singleton(mockFactoryService)
                .singleton(mockIsisSessionFactory)
                .singleton(mockTransactionService)
                .build();
        
        metaModelContext.getServiceInjector().injectServicesInto(wrapperFactory);

        employeeRepository = new EmployeeRepositoryImpl();

        employeeDO = new Employee();
        employeeDO.setName("Smith");
        employeeDO.setEmployeeRepository(employeeRepository);

        context.checking(new Expectations() {
            {
                
                allowing(mockObjectManager).adapt(employeeDO);
                will(returnValue(mockEmployeeAdapter));

//                allowing(mockEmployeeAdapter).getOid();
//                will(returnValue(Factory.persistentOf(ObjectSpecId.of("EMP"), "1")));

                allowing(mockEmployeeSpec).isManagedBean();
                will(returnValue(true));
                
                allowing(mockEmployeeSpec).isIdentifiable();
                will(returnValue(true));
                
                allowing(mockEmployeeSpec).getCorrespondingClass();
                will(returnValue(Employee.class));

                allowing(mockStringSpec).getCorrespondingClass();
                will(returnValue(String.class));

                allowing(mockCommandDtoServiceInternal).asCommandDto(with(any(List.class)), with(any(OneToOneAssociation.class)), with(any(ManagedObject.class)));
                will(returnValue(new CommandDto()));
                
                allowing(mockCommandContext).getCommand();
                will(returnValue(mockCommand));
                
                allowing(mockCommandContext).getCurrentExecutor();
                will(returnValue(Optional.of(Executor.USER)));

                allowing(mockSpecificationLoader).loadSpecification(String.class);
                will(returnValue(mockStringSpec));

                allowing(mockStringSpec).getShortIdentifier();
                will(returnValue(String.class.getName()));

                allowing(mockAuthenticationSessionProvider).getAuthenticationSession();
                will(returnValue(session));

//                allowing(mockAdapterManager).adapterFor(employeeDO);
//                will(returnValue(mockEmployeeAdapter));

                allowing(mockEmployeeAdapter).titleString(null);
                will(returnValue("titleOf[mockEmployeeAdapter]"));
                
                allowing(mockEmployeeAdapter).getSpecification();
                will(returnValue(mockEmployeeSpec));

                allowing(mockSpecificationLoader).loadSpecification(Employee.class);
                will(returnValue(mockEmployeeSpec));

                allowing(mockEmployeeSpec).getMember(methodOf(Employee.class, "getEmployeeRepository"));
                will(returnValue(null));
                
                allowing(mockEmployeeSpec).getFacet(EntityFacet.class);
                will(returnValue(null));


{'fraction_non_alphanumeric': 0.08673521327496692, 'fraction_numerical': 0.0012216227221826326, 'mean_word_length': 3.849913601579857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5837871', 'n_tokens_mistral': 5361, 'n_tokens_neox': 5135, 'n_words': 822}