Skip to content

Commit

Permalink
New method to build a valid consumer name for a web client (openhab#3359
Browse files Browse the repository at this point in the history
)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored Feb 19, 2023
1 parent 1b18831 commit cd45dfe
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.util;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.ThingUID;

/**
* {@link ThingWebClientUtil} provides an utility method to create a valid consumer name for web clients.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class ThingWebClientUtil {

private static final int MAX_CONSUMER_NAME_LENGTH = 20;

/**
* Build a valid consumer name for HTTP or WebSocket client.
*
* @param uid thing UID for which to associate HTTP or WebSocket client
* @param prefix a prefix to consider for the name; can be null
* @return a valid consumer name
*/
public static String buildWebClientConsumerName(ThingUID uid, @Nullable String prefix) {
String pref = prefix == null ? "" : prefix;
String name = pref + uid.getAsString().replace(':', '-');
if (name.length() > MAX_CONSUMER_NAME_LENGTH) {
// Try to use only prefix + binding ID + thing ID
name = pref + uid.getBindingId();
if (name.length() > (MAX_CONSUMER_NAME_LENGTH / 2)) {
// Truncate the binding ID to keep enough place for thing ID
name = name.substring(0, MAX_CONSUMER_NAME_LENGTH / 2);
}
// Add the thing ID
String id = uid.getId();
int maxIdLength = MAX_CONSUMER_NAME_LENGTH - 1 - name.length();
if (id.length() > maxIdLength) {
// If thing ID is too big, use a hash code of the thing UID instead of thing id
// and truncate it if necessary
id = buildHashCode(uid, maxIdLength);
}
name += "-" + id;
}
return name;
}

// Make the method public just to be able to call it inside the tests
static String buildHashCode(ThingUID uid, int maxLength) {
String result = Integer.toHexString(uid.hashCode());
if (result.length() > maxLength) {
result = result.substring(result.length() - maxLength);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.util;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.thing.ThingUID;

/**
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class ThingWebClientUtilTest {

private ThingUID uid1 = new ThingUID("mycroft", "mycroft", "yy");
private ThingUID uid2 = new ThingUID("mycroft", "mycroft", "myInstance");
private ThingUID uid3 = new ThingUID("mycroft", "mycroft", "myPersonalInstance");
private ThingUID uid4 = new ThingUID("amazonechocontrol", "account", "myAccount");

@Test
public void testBuildWebClientConsumerNameWhenThingUidSizeIsOk() {
String name = ThingWebClientUtil.buildWebClientConsumerName(uid1, null);
assertThat(name, is("mycroft-mycroft-yy"));
}

@Test
public void testBuildWebClientConsumerNameWhenPrefixAndThingUidSizeIsOk() {
String name = ThingWebClientUtil.buildWebClientConsumerName(uid1, "x-");
assertThat(name, is("x-mycroft-mycroft-yy"));
}

@Test
public void testBuildWebClientConsumerNameWhenPrefixIsTooBig() {
String name = ThingWebClientUtil.buildWebClientConsumerName(uid1, "xxxx-");
assertThat(name, is("xxxx-mycro-yy"));
}

@Test
public void testBuildWebClientConsumerNameWhenThingUidIsTooBig() {
String name = ThingWebClientUtil.buildWebClientConsumerName(uid2, null);
assertThat(name, is("mycroft-myInstance"));
}

@Test
public void testBuildWebClientConsumerNameWhenThingIdIsTooBig() {
String name = ThingWebClientUtil.buildWebClientConsumerName(uid3, null);
String hashCode = ThingWebClientUtil.buildHashCode(uid3, 12);
assertThat(name, is("mycroft-" + hashCode));
}

@Test
public void testBuildWebClientConsumerNameWhenBindingIdIsTooBig() {
String name = ThingWebClientUtil.buildWebClientConsumerName(uid4, null);
assertThat(name, is("amazonecho-myAccount"));
}
}

0 comments on commit cd45dfe

Please sign in to comment.