### Question:
PostgresqlCollection extends AbstractSynchronizedMongoCollection<Long> { static String convertOrderByToSql(Document orderBy) { StringBuilder orderBySql = new StringBuilder(); if (orderBy != null && !orderBy.isEmpty()) { orderBySql.append("ORDER BY"); int num = 0; for (String key : orderBy.keySet()) { if (num > 0) { orderBySql.append(","); } int sortValue = getSortValue(orderBy, key); orderBySql.append(" "); if (key.equals("$natural")) { orderBySql.append("id"); } else { orderBySql.append(PostgresqlUtils.toNormalizedDataKey(key)); } if (sortValue == 1) { orderBySql.append(" ASC NULLS FIRST"); } else if (sortValue == -1) { orderBySql.append(" DESC NULLS LAST"); } else { throw new IllegalArgumentException("Illegal sort value: " + sortValue); } num++; } } return orderBySql.toString(); } PostgresqlCollection(PostgresqlDatabase database, String collectionName,
                                CollectionOptions options, CursorRegistry cursorRegistry); @Override int count(); static String getQualifiedTablename(String databaseName, String collectionName); @Override void renameTo(MongoDatabase newDatabase, String newCollectionName);  }### Answer:
@Test void testConvertOrderByToSql() throws Exception { assertThat(PostgresqlCollection.convertOrderByToSql(json(""))).isEqualTo(""); assertThat(PostgresqlCollection.convertOrderByToSql(json("key: 1"))) .isEqualTo("ORDER BY CASE WHEN (data ->> 'key')::numeric = -0.0 THEN '0' ELSE data ->> 'key' END ASC NULLS FIRST"); assertThat(PostgresqlCollection.convertOrderByToSql(json("key1: 1, key2: -1"))) .isEqualTo("ORDER BY" + " CASE WHEN (data ->> 'key1')::numeric = -0.0 THEN '0' ELSE data ->> 'key1' END ASC NULLS FIRST," + " CASE WHEN (data ->> 'key2')::numeric = -0.0 THEN '0' ELSE data ->> 'key2' END DESC NULLS LAST"); assertThat(PostgresqlCollection.convertOrderByToSql(json("$natural: 1"))) .isEqualTo("ORDER BY id ASC NULLS FIRST"); assertThat(PostgresqlCollection.convertOrderByToSql(json("$natural: -1"))) .isEqualTo("ORDER BY id DESC NULLS LAST"); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> PostgresqlCollection.convertOrderByToSql(json("foo: 'bar'"))) .withMessage("Illegal sort value: bar"); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> PostgresqlCollection.convertOrderByToSql(json("$foo: 1"))) .withMessage("Illegal key: $foo"); }