/*
 * Copyright 2019 Google 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 com.example.cloud.iot.examples;

import com.google.pubsub.v1.Topic;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for iot "Management" sample. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class ManagerIT {
  private ByteArrayOutputStream bout;
  private PrintStream out;
  private MqttCommandsDemo app;

  @Before
  public void setUp() throws Exception {
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    System.setOut(out);
  }

  @After
  public void tearDown() throws Exception {
    System.setOut(null);
  }

  @Test
  public void testTerminal() throws Exception {
    // Set up
    Screen screen = null;
    DefaultTerminalFactory defaultTerminalFactory = new DefaultTerminalFactory();
    Terminal terminal = defaultTerminalFactory.createTerminal();
    screen = new TerminalScreen(terminal);

    Screen finalScreen = screen;
    Thread deviceThread =
        new Thread(
            () -> {
              try {
                MqttCommandsDemo.startGui(finalScreen, new TextColor.RGB(255, 255, 255));
              } catch (IOException e) {
                e.printStackTrace();
              }
            });

    deviceThread.join(3000);
    System.out.println(terminal.getTerminalSize().toString());
    // Assertions
    Assert.assertTrue(terminal.getTerminalSize().toString().contains("x"));
    Assert.assertTrue(terminal.getTerminalSize().toString().contains("{"));
    Assert.assertTrue(terminal.getTerminalSize().toString().contains("}"));
  }

  @Test
  public void testJsonValid() {
    // Assertions
    Assert.assertTrue(MqttCommandsDemo.isJsonValid("{test:true}"));
    Assert.assertFalse(MqttCommandsDemo.isJsonValid("{test:false"));
  }
}

@@ -951,24 +951,62 @@
 tputStream;%0A
+import java.io.FileNotFoundException;%0A
 import java.
@@ -1876,16 +1876,21 @@
 ();%0A    
+try (
 Terminal
@@ -1940,18 +1940,22 @@
 rminal()
-;%0A
+) %7B%0A  
     scre
@@ -1990,24 +1990,26 @@
 inal);%0A%0A    
+  
 Screen final
@@ -2029,16 +2029,18 @@
 en;%0A    
+ 
+ 
 Thread d
@@ -2061,16 +2061,18 @@
         
+  
 new Thre
@@ -2091,16 +2091,18 @@
     
+ 
+ 
 () -%3E %7B%0A
     
@@ -2097,16 +2097,18 @@
 () -%3E %7B%0A
+  
         
@@ -2135,16 +2135,18 @@
         
+  
 MqttComm
@@ -2225,16 +2225,18 @@
         
+  
 %7D catch 
@@ -2249,24 +2249,26 @@
 eption e) %7B%0A
+  
             
@@ -2306,18 +2306,22 @@
         
-%7D%0A
+  %7D%0A  
         
@@ -2325,24 +2325,26 @@
        %7D);%0A%0A
+  
     deviceTh
@@ -2360,24 +2360,26 @@
 (3000);%0A    
+ 
+ 
 System.out.p
@@ -2425,24 +2425,26 @@
 ing());%0A    
+  
 // Assertion
@@ -2441,32 +2441,34 @@
  Assertions%0A    
+ 
+ 
 Assert.assertTru
@@ -2519,24 +2519,26 @@
 ains(%22x%22));%0A
+  
     Assert.a
@@ -2597,32 +2597,34 @@
 ains(%22%7B%22));%0A    
+  
 Assert.assertTru
@@ -2675,24 +2675,195 @@
 ains(%22%7D%22));%0A
+    %7D catch (FileNotFoundException e) %7B%0A      // Don't fail when GUI not available: %22/dev/tty (No such device or address)%22%0A      System.out.println(e.getMessage());%0A    %7D%0A
   %7D%0A%0A  @Test

handle expected exception when running IoT GUI test (#7349)