@Test
  public void do_switch_role_when_user_has_no_selected_role() throws Exception {
    user =
        new RichUserDetailsFactory()
            .addBodRoles(BodRole.createNewUser())
            .addUserGroup(new UserGroupFactory().setId("urn:user-group").create())
            .create();
    Security.setUserDetails(user);

    PhysicalResourceGroup pGroup = new PhysicalResourceGroupFactory().setActive(true).create();

    when(virtualResourceGroupServiceMock.findByAdminGroup("urn:user-group"))
        .thenReturn(Optional.empty());
    when(physicalResourceGroupServiceMock.find(2L)).thenReturn(Optional.of(pGroup));

    mockMvc
        .perform(
            post("/request")
                .param("physicalResourceGroupId", "2")
                .param("userGroupId", "urn:user-group")
                .param("bandwidth", "1111")
                .param("userLabel", "port")
                .param("message", "I want!"))
        .andExpect(view().name("redirect:/user"));

    assertThat(
        "Context should not be cleared",
        SecurityContextHolder.getContext().getAuthentication(),
        nullValue());

    verify(virtualResourceGroupServiceMock).save(any(VirtualResourceGroup.class));
    verify(virtualPortServiceMock)
        .requestCreateVirtualPort(eq(user), any(VirtualPortCreateRequestLink.class));
  }
  @Test
  public void should_not_create_form_if_physical_resource_group_does_not_exist() throws Exception {
    when(physicalResourceGroupServiceMock.find(1L)).thenReturn(Optional.empty());

    mockMvc
        .perform(get("/request").param("id", "1").param("teamUrn", "urn:group"))
        .andExpect(view().name("redirect:/virtualports/request"));
  }
  @Test
  public void should_not_create_form_if_physical_resource_group_is_not_active() throws Exception {
    PhysicalResourceGroup group = new PhysicalResourceGroupFactory().setActive(false).create();

    when(physicalResourceGroupServiceMock.find(1L)).thenReturn(Optional.of(group));

    mockMvc
        .perform(get("/request").param("id", "1").param("teamUrn", "urn:group"))
        .andExpect(view().name("redirect:/virtualports/request"));
  }
  @Test
  public void should_create_form_if_physical_resource_group_is_active() throws Exception {
    PhysicalResourceGroup group = new PhysicalResourceGroupFactory().setActive(true).create();

    when(physicalResourceGroupServiceMock.find(1L)).thenReturn(Optional.of(group));

    mockMvc
        .perform(get("/request").param("id", "1").param("teamUrn", "urn:user-group"))
        .andExpect(view().name("virtualports/requestform"))
        .andExpect(model().attributeExists("requestCommand", "user", "physicalResourceGroup"));
  }
  @Test
  public void when_the_physical_group_has_changed_dont_mail_and_redirect() throws Exception {
    when(physicalResourceGroupServiceMock.find(2L)).thenReturn(Optional.empty());

    mockMvc
        .perform(
            post("/request")
                .param("physicalResourceGroupId", "2")
                .param("userGroupId", "urn:user-group-wrong")
                .param("bandwidth", "1000")
                .param("userLabel", "new port")
                .param("message", "message"))
        .andExpect(view().name("redirect:/virtualports/request"));

    verifyNeverRequestNewVirtualPort();
  }
  @Test
  public void when_the_physical_resource_group_is_not_active_dont_mail_and_redirect()
      throws Exception {
    PhysicalResourceGroup group = new PhysicalResourceGroupFactory().setActive(false).create();

    when(physicalResourceGroupServiceMock.find(2L)).thenReturn(Optional.of(group));

    mockMvc
        .perform(
            post("/request")
                .param("physicalResourceGroupId", "2")
                .param("userGroupId", "urn:user-group")
                .param("bandwidth", "1000")
                .param("userLabel", "new port")
                .param("message", "message"))
        .andExpect(view().name("redirect:/virtualports/request"));

    verifyNeverRequestNewVirtualPort();
  }
  @Test
  public void when_virtual_resource_group_does_not_exists_it_should_be_created() throws Exception {
    PhysicalResourceGroup pGroup = new PhysicalResourceGroupFactory().setActive(true).create();

    when(virtualResourceGroupServiceMock.findByAdminGroup("urn:user-group"))
        .thenReturn(Optional.empty());
    when(physicalResourceGroupServiceMock.find(2L)).thenReturn(Optional.of(pGroup));

    mockMvc
        .perform(
            post("/request")
                .param("physicalResourceGroupId", "2")
                .param("userGroupId", "urn:user-group")
                .param("bandwidth", "1111")
                .param("userLabel", "new port")
                .param("message", "I want!"))
        .andExpect(view().name("redirect:/user"));

    verify(virtualResourceGroupServiceMock).save(any(VirtualResourceGroup.class));
    verify(virtualPortServiceMock)
        .requestCreateVirtualPort(eq(user), any(VirtualPortCreateRequestLink.class));
  }