/*
 * Copyright 2018 Red Hat, Inc.
 *
 * Licensed 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 io.quarkus.arc.test.requestcontext;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.ManagedContext;
import io.quarkus.arc.test.ArcTestContainer;
import javax.enterprise.context.ContextNotActiveException;
import org.junit.Rule;
import org.junit.Test;

public class RequestContextTest {

    @Rule
    public ArcTestContainer container = new ArcTestContainer(Controller.class, ControllerClient.class);

    @Test
    public void testRequestContext() {

        ArcContainer arc = Arc.container();
        ManagedContext requestContext = arc.requestContext();

        try {
            arc.instance(Controller.class).get().getId();
            fail();
        } catch (ContextNotActiveException expected) {
        }

        requestContext.activate();
        assertFalse(Controller.DESTROYED.get());
        Controller controller1 = arc.instance(Controller.class).get();
        Controller controller2 = arc.instance(Controller.class).get();
        String controller2Id = controller2.getId();
        assertEquals(controller1.getId(), controller2Id);
        requestContext.terminate();
        assertTrue(Controller.DESTROYED.get());

        try {
            arc.instance(Controller.class).get().getId();
            fail();
        } catch (ContextNotActiveException expected) {
        }

        // Id must be different in a different request
        Controller.DESTROYED.set(false);
        requestContext.activate();
        assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());
        requestContext.terminate();
        assertTrue(Controller.DESTROYED.get());

        Controller.DESTROYED.set(false);
        requestContext.activate();
        assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());
        requestContext.terminate();
        assertTrue(Controller.DESTROYED.get());

        // @ActivateRequestContext
        Controller.DESTROYED.set(false);
        ControllerClient client = arc.instance(ControllerClient.class).get();
        assertNotEquals(controller2Id, client.getControllerId());
        assertTrue(Controller.DESTROYED.get());
    }

}

@@ -1059,16 +1059,83 @@
 eption;%0A
+import javax.enterprise.context.control.RequestContextController;%0A%0A
 import o
@@ -1149,16 +1149,16 @@
 t.Rule;%0A
-
 import o
@@ -1372,17 +1372,57 @@
 ext() %7B%0A
+        Controller.DESTROYED.set(false);
 %0A
-
         
@@ -2713,32 +2713,32 @@
 get().getId());%0A
-
         requestC
@@ -2798,32 +2798,1831 @@
 TROYED.get());%0A%0A
+        // @ActivateRequestContext%0A        Controller.DESTROYED.set(false);%0A        ControllerClient client = arc.instance(ControllerClient.class).get();%0A        assertNotEquals(controller2Id, client.getControllerId());%0A        assertTrue(Controller.DESTROYED.get());%0A    %7D%0A    %0A    @Test%0A    public void testRequestContextController() %7B%0A        Controller.DESTROYED.set(false);%0A        ArcContainer arc = Arc.container();%0A        RequestContextController controller = Arc.container().instance(RequestContextController.class).get();%0A%0A        try %7B%0A            arc.instance(Controller.class).get().getId();%0A            fail();%0A        %7D catch (ContextNotActiveException expected) %7B%0A        %7D%0A%0A        controller.activate();%0A        assertFalse(Controller.DESTROYED.get());%0A        Controller controller1 = arc.instance(Controller.class).get();%0A        Controller controller2 = arc.instance(Controller.class).get();%0A        String controller2Id = controller2.getId();%0A        assertEquals(controller1.getId(), controller2Id);%0A        controller.deactivate();%0A        assertTrue(Controller.DESTROYED.get());%0A%0A        try %7B%0A            arc.instance(Controller.class).get().getId();%0A            fail();%0A        %7D catch (ContextNotActiveException expected) %7B%0A        %7D%0A%0A        // Id must be different in a different request%0A        Controller.DESTROYED.set(false);%0A        controller.activate();%0A        assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());%0A        controller.deactivate();%0A        assertTrue(Controller.DESTROYED.get());%0A%0A        Controller.DESTROYED.set(false);%0A        controller.activate();%0A        assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());%0A        controller.deactivate();%0A        assertTrue(Controller.DESTROYED.get());%0A%0A
         // @Acti

Add test for RequestContextController