@Test
  public void testChatSessionMergeHistoryToRootChatSession() throws Exception {

    // (Arrange) Set up the return value of the generateContent

    VertexAI vertexAi =
        new VertexAI.Builder()
            .setProjectId(PROJECT)
            .setLocation(LOCATION)
            .setPredictionClientSupplier(() -> mockPredictionServiceClient)
            .build();

    GenerativeModel model = new GenerativeModel("gemini-pro", vertexAi);

    when(mockPredictionServiceClient.generateContentCallable()).thenReturn(mockUnaryCallable);
    when(mockUnaryCallable.call(any(GenerateContentRequest.class)))
        .thenReturn(RESPONSE_FROM_UNARY_CALL)
        .thenReturn(RESPONSE_FROM_UNARY_CALL_2);

    // (Act) Send text message in root chat
    ChatSession rootChat = model.startChat();
    GenerateContentResponse response = rootChat.sendMessage(SAMPLE_MESSAGE1);
    // (Act) Create a child chat session and send message again
    ChatSession childChat =
        rootChat
            .withGenerationConfig(GENERATION_CONFIG)
            .withSafetySettings(Arrays.asList(SAFETY_SETTING))
            .withTools(Arrays.asList(TOOL))
            .withToolConfig(TOOL_CONFIG)
            .withSystemInstruction(SYSTEM_INSTRUCTION);
    response = childChat.sendMessage(SAMPLE_MESSAGE_2);

    // (Assert) root chat history should contain all 4 contents
    List<Content> history = rootChat.getHistory();
    assertThat(history.get(0).getParts(0).getText()).isEqualTo(SAMPLE_MESSAGE1);
    assertThat(history.get(1).getParts(0).getText()).isEqualTo(FULL_RESPONSE_TEXT);
    assertThat(history.get(2).getParts(0).getText()).isEqualTo(SAMPLE_MESSAGE_2);
    assertThat(history.get(3).getParts(0).getText()).isEqualTo(RESPONSE_TEXT_2);

    // (Assert) the second request (from child chat) should contained updated configurations
    ArgumentCaptor<GenerateContentRequest> request =
        ArgumentCaptor.forClass(GenerateContentRequest.class);
    verify(mockUnaryCallable, times(2)).call(request.capture());
    Content expectedSystemInstruction = SYSTEM_INSTRUCTION.toBuilder().clearRole().build();
    assertThat(request.getAllValues().get(1).getGenerationConfig()).isEqualTo(GENERATION_CONFIG);
    assertThat(request.getAllValues().get(1).getSafetySettings(0)).isEqualTo(SAFETY_SETTING);
    assertThat(request.getAllValues().get(1).getTools(0)).isEqualTo(TOOL);
    assertThat(request.getAllValues().get(1).getToolConfig()).isEqualTo(TOOL_CONFIG);
    assertThat(request.getAllValues().get(1).getSystemInstruction())
        .isEqualTo(expectedSystemInstruction);
  }
Callable function getCurrentWeather for testing automatic function calling.
java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java