Example #1
0
  @Test
  public void loadGzippedProfile() throws Exception {
    String expected = readTestUserProfile();

    clientPair.appClient.send("loadProfileGzipped");
    verify(clientPair.appClient.responseMock, timeout(500)).channelRead(any(), any());

    Profile profile = JsonParser.parseProfile(clientPair.appClient.getBody(), 1);
    profile.dashBoards[0].updatedAt = 0;
    assertEquals(expected, profile.toString());
  }
Example #2
0
  @Test
  public void testActive2AndDeactivate1() throws Exception {
    TestHardClient hardClient2 = new TestHardClient("localhost", tcpHardPort);
    hardClient2.start();

    Profile newProfile =
        JsonParser.parseProfile(readTestUserProfile("user_profile_json_3_dashes.txt"), 1);
    clientPair.appClient.send("createDash " + newProfile.dashBoards[1]);
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, OK)));

    clientPair.appClient.send("activate 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, OK)));

    clientPair.appClient.send("activate 2");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(3, DEVICE_NOT_IN_NETWORK)));

    clientPair.appClient.reset();

    clientPair.appClient.send("getToken 2");
    String token2 = clientPair.appClient.getBody();
    hardClient2.send("login " + token2);
    verify(hardClient2.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, OK)));

    clientPair.appClient.reset();

    clientPair.hardwareClient.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(produce(1, HARDWARE, b("1 aw 1 1"))));

    hardClient2.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(produce(2, HARDWARE, b("2 aw 1 1"))));

    clientPair.appClient.send("deactivate 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, OK)));

    clientPair.hardwareClient.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500).times(0))
        .channelRead(any(), eq(new ResponseMessage(2, NO_ACTIVE_DASHBOARD)));

    hardClient2.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(produce(3, HARDWARE, b("2 aw 1 1"))));
    hardClient2.stop().awaitUninterruptibly();
  }
  public void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) {
    String[] split = message.body.split(BODY_SEPARATOR_STRING, 2);

    if (split.length < 2) {
      throw new IllegalCommandException("Wrong income message format.");
    }

    int dashId = ParseUtil.parseInt(split[0]);
    String widgetString = split[1];

    if (widgetString == null || widgetString.equals("")) {
      throw new IllegalCommandException("Income widget message is empty.");
    }

    if (widgetString.length() > MAX_WIDGET_SIZE) {
      throw new NotAllowedException("Widget is larger then limit.");
    }

    DashBoard dash = user.profile.getDashById(dashId);

    Widget newWidget = JsonParser.parseWidget(widgetString);

    log.debug("Updating widget {}.", widgetString);

    if (dash == null || newWidget == null) {
      log.error("Error updating widget {}. Project : {}", widgetString, dash);
      throw new IllegalCommandException("Empty widget or project.");
    }

    int existingWidgetIndex = dash.getWidgetIndex(newWidget.id);

    if (newWidget instanceof Tabs) {
      Tabs newTabs = (Tabs) newWidget;
      DeleteWidgetLogic.deleteTabs(user, dash, newTabs.tabs.length - 1);
    }

    // strange issue https://github.com/blynkkk/blynk-server/issues/227
    // just log error for now
    try {
      dash.widgets[existingWidgetIndex] = newWidget;
      dash.updatedAt = System.currentTimeMillis();
      user.lastModifiedTs = dash.updatedAt;
    } catch (ArrayIndexOutOfBoundsException e) {
      throw new BaseServerException(
          "Error updating widget. " + widgetString, Response.SERVER_EXCEPTION);
    }

    ctx.writeAndFlush(ok(message.id), ctx.voidPromise());
  }
Example #4
0
  @Test
  public void testHardwareDeviceWentOffline() throws Exception {
    Profile profile = JsonParser.parseProfile(readTestUserProfile(), 1);
    Notification notification = profile.dashBoards[0].getWidgetByType(Notification.class);
    notification.notifyWhenOffline = false;

    clientPair.appClient.send("saveDash " + profile.dashBoards[0].toString());
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, OK)));

    ChannelFuture channelFuture = clientPair.hardwareClient.stop();
    channelFuture.await();

    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(
            any(), eq(new ResponseWithBodyMessage(0, Command.RESPONSE, DEVICE_WENT_OFFLINE, 1)));
  }
Example #5
0
  @Test
  public void testAppChangeActiveDash() throws Exception {
    clientPair.hardwareClient.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(produce(1, HARDWARE, b("1 aw 1 1"))));

    clientPair.appClient.send("deactivate 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, OK)));

    Profile newProfile =
        JsonParser.parseProfile(readTestUserProfile("user_profile_json_3_dashes.txt"), 1);
    clientPair.appClient.send("createDash " + newProfile.dashBoards[1]);
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, OK)));

    clientPair.hardwareClient.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500).times(0))
        .channelRead(any(), eq(new ResponseMessage(2, NO_ACTIVE_DASHBOARD)));

    clientPair.appClient.send("activate 2");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(3, DEVICE_NOT_IN_NETWORK)));

    clientPair.hardwareClient.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500).times(0))
        .channelRead(any(), eq(new ResponseMessage(3, NO_ACTIVE_DASHBOARD)));

    clientPair.appClient.send("activate 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(4, OK)));

    clientPair.hardwareClient.send("hardware aw 1 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(produce(4, HARDWARE, b("1 aw 1 1"))));
  }
Example #6
0
  @Test
  public void createBasicProfile() throws Exception {
    TestAppClient appClient = new TestAppClient("localhost", tcpAppPort, properties);

    appClient.start();

    appClient.send("register [email protected] 1");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(1, OK)));

    appClient.send("login [email protected] 1 Android RC13");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(2, OK)));

    appClient.send("createDash {\"id\":1, \"name\":\"test board\"}\"");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(3, OK)));

    appClient.send(
        "createWidget 1\0{\"id\":1, \"x\":0, \"y\":0, \"label\":\"Some Text\", \"type\":\"BUTTON\", \"pinType\":\"DIGITAL\", \"pin\":1}");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(4, OK)));

    appClient.reset();

    appClient.send("loadProfileGzipped");
    Profile profile = JsonParser.parseProfile(appClient.getBody(), 1);
    profile.dashBoards[0].updatedAt = 0;

    assertEquals(
        "{\"dashBoards\":[{\"id\":1,\"name\":\"test board\",\"createdAt\":0,\"updatedAt\":0,\"widgets\":[{\"type\":\"BUTTON\",\"id\":1,\"x\":0,\"y\":0,\"label\":\"Some Text\",\"pinType\":\"DIGITAL\",\"pin\":1,\"pwmMode\":false,\"rangeMappingOn\":false,\"min\":0,\"max\":0,\"pushMode\":false}],\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}]}",
        profile.toString());

    appClient.send("createWidget 1\0{\"id\":1,\"type\":\"BUTTON\"}");
    verify(appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, NOT_ALLOWED)));

    appClient.send(
        "createWidget 1\0{\"id\":2, \"x\":2, \"y\":2, \"label\":\"Some Text 2\", \"type\":\"BUTTON\", \"pinType\":\"DIGITAL\", \"pin\":2}");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(3, OK)));

    appClient.reset();

    appClient.send("loadProfileGzipped");
    profile = JsonParser.parseProfile(appClient.getBody(), 1);
    profile.dashBoards[0].updatedAt = 0;
    assertEquals(
        "{\"dashBoards\":[{\"id\":1,\"name\":\"test board\",\"createdAt\":0,\"updatedAt\":0,\"widgets\":[{\"type\":\"BUTTON\",\"id\":1,\"x\":0,\"y\":0,\"label\":\"Some Text\",\"pinType\":\"DIGITAL\",\"pin\":1,\"pwmMode\":false,\"rangeMappingOn\":false,\"min\":0,\"max\":0,\"pushMode\":false},{\"type\":\"BUTTON\",\"id\":2,\"x\":2,\"y\":2,\"label\":\"Some Text 2\",\"pinType\":\"DIGITAL\",\"pin\":2,\"pwmMode\":false,\"rangeMappingOn\":false,\"min\":0,\"max\":0,\"pushMode\":false}],\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}]}",
        profile.toString());

    appClient.send(
        "updateWidget 1\0{\"id\":2, \"x\":2, \"y\":2, \"label\":\"new label\", \"type\":\"BUTTON\", \"pinType\":\"DIGITAL\", \"pin\":3}\"");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(2, OK)));

    appClient.reset();

    appClient.send("loadProfileGzipped");
    profile = JsonParser.parseProfile(appClient.getBody(), 1);
    profile.dashBoards[0].updatedAt = 0;
    assertEquals(
        "{\"dashBoards\":[{\"id\":1,\"name\":\"test board\",\"createdAt\":0,\"updatedAt\":0,\"widgets\":[{\"type\":\"BUTTON\",\"id\":1,\"x\":0,\"y\":0,\"label\":\"Some Text\",\"pinType\":\"DIGITAL\",\"pin\":1,\"pwmMode\":false,\"rangeMappingOn\":false,\"min\":0,\"max\":0,\"pushMode\":false},{\"type\":\"BUTTON\",\"id\":2,\"x\":2,\"y\":2,\"label\":\"new label\",\"pinType\":\"DIGITAL\",\"pin\":3,\"pwmMode\":false,\"rangeMappingOn\":false,\"min\":0,\"max\":0,\"pushMode\":false}],\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}]}",
        profile.toString());

    appClient.send("deleteWidget 1\0" + "3");
    verify(appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, ILLEGAL_COMMAND)));

    appClient.send("deleteWidget 1\0" + "1");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(3, OK)));

    appClient.send("deleteWidget 1\0" + "2");
    verify(appClient.responseMock, timeout(500)).channelRead(any(), eq(new ResponseMessage(4, OK)));

    appClient.reset();

    appClient.send("loadProfileGzipped");
    profile = JsonParser.parseProfile(appClient.getBody(), 1);
    profile.dashBoards[0].updatedAt = 0;
    assertEquals(
        "{\"dashBoards\":[{\"id\":1,\"name\":\"test board\",\"createdAt\":0,\"updatedAt\":0,\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}]}",
        profile.toString());
  }
Example #7
0
  @Test
  public void testDashCommands() throws Exception {
    clientPair.appClient.send("saveDash {\"id\":10, \"name\":\"test board update\"}");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, ILLEGAL_COMMAND)));

    clientPair.appClient.send("createDash {\"id\":10, \"name\":\"test board\"}");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, OK)));

    clientPair.appClient.send("createDash {\"id\":10, \"name\":\"test board\"}");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(3, NOT_ALLOWED)));

    clientPair.appClient.send("saveDash {\"id\":10, \"name\":\"test board update\"}");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(4, OK)));

    clientPair.hardwareClient.send("ping");

    clientPair.appClient.send("deleteDash 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(5, OK)));

    clientPair.appClient.send("deleteDash 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(6, ILLEGAL_COMMAND)));

    String expectedDash;

    expectedDash =
        "{\"dashBoards\":[{\"id\":10,\"name\":\"test board update\",\"createdAt\":0,\"updatedAt\":0,\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}]}";
    clientPair.appClient.send("loadProfileGzipped");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new LoadProfileGzippedBinaryMessage(7, expectedDash)));

    expectedDash =
        "{\"id\":10,\"name\":\"test board update\",\"createdAt\":0,\"updatedAt\":0,\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}";
    clientPair.appClient.send("loadProfileGzipped 10");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new LoadProfileGzippedBinaryMessage(8, expectedDash)));

    clientPair.appClient.send("loadProfileGzipped 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(9, ILLEGAL_COMMAND)));

    clientPair.appClient.send("activate 10");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(10, DEVICE_NOT_IN_NETWORK)));

    clientPair.appClient.reset();
    clientPair.appClient.send("loadProfileGzipped");
    Profile profile = JsonParser.parseProfile(clientPair.appClient.getBody(), 1);
    expectedDash =
        String.format(
            "{\"dashBoards\":[{\"id\":10,\"name\":\"test board update\",\"createdAt\":0,\"updatedAt\":%d,\"keepScreenOn\":false,\"isShared\":false,\"isActive\":true}]}",
            profile.dashBoards[0].updatedAt);
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new LoadProfileGzippedBinaryMessage(1, expectedDash)));

    clientPair.appClient.send(
        "saveDash {\"id\":10,\"name\":\"test board update\",\"keepScreenOn\":false,\"isShared\":false,\"isActive\":false}");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, OK)));

    expectedDash =
        "{\"dashBoards\":[{\"id\":10,\"name\":\"test board update\",\"createdAt\":0,\"updatedAt\":0,\"keepScreenOn\":false,\"isShared\":false,\"isActive\":true}]}";
    clientPair.appClient.send("loadProfileGzipped");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new LoadProfileGzippedBinaryMessage(3, expectedDash)));
  }
Example #8
0
  @Test
  public void testHas1Pin() {
    InputStream is = this.getClass().getResourceAsStream("/json_test/user_profile_json.txt");

    Profile profile = JsonParser.parseProfile(is);
    DashBoard dash = profile.dashBoards[0];
    dash.isActive = true;

    Button button = dash.getWidgetByType(Button.class);
    assertEquals(1, button.pin);
    assertEquals(PinType.DIGITAL, button.pinType);
    assertEquals("1", button.value);

    dash.update("dw 1 0".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));
    assertEquals("0", button.value);

    dash.update("aw 1 1".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));
    assertEquals("0", button.value);

    dash.update("dw 1 1".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));
    assertEquals("1", button.value);

    RGB rgb = new RGB();
    rgb.pins = new Pin[3];
    rgb.pins[0] = new Pin();
    rgb.pins[0].pin = 0;
    rgb.pins[0].pinType = PinType.VIRTUAL;
    rgb.pins[1] = new Pin();
    rgb.pins[1].pin = 1;
    rgb.pins[1].pinType = PinType.VIRTUAL;
    rgb.pins[2] = new Pin();
    rgb.pins[2].pin = 2;
    rgb.pins[2].pinType = PinType.VIRTUAL;

    dash.widgets = ArrayUtils.add(dash.widgets, rgb);

    dash.update("vw 0 100".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));
    dash.update("vw 1 101".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));
    dash.update("vw 2 102".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));

    for (int i = 0; i < rgb.pins.length; i++) {
      assertEquals("10" + i, rgb.pins[i].value);
    }

    rgb = new RGB();
    rgb.pins = new Pin[3];
    rgb.pins[0] = new Pin();
    rgb.pins[0].pin = 4;
    rgb.pins[0].pinType = PinType.VIRTUAL;
    rgb.pins[1] = new Pin();
    rgb.pins[1].pin = 4;
    rgb.pins[1].pinType = PinType.VIRTUAL;
    rgb.pins[2] = new Pin();
    rgb.pins[2].pin = 4;
    rgb.pins[2].pinType = PinType.VIRTUAL;

    dash.widgets = ArrayUtils.add(dash.widgets, rgb);

    dash.update("vw 4 100 101 102".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING));

    assertEquals(
        "100 101 102".replaceAll(" ", StringUtils.BODY_SEPARATOR_STRING), rgb.pins[0].value);
  }
Example #9
0
 @SuppressWarnings("unchecked")
 public List<String> consumeJsonPinValues(CloseableHttpResponse response) throws IOException {
   return JsonParser.readAny(consumeText(response), List.class);
 }