Skip to content

Commit

Permalink
Merge pull request #1009 from hashmapinc/Tempus-965
Browse files Browse the repository at this point in the history
Tempus 965-Template registry
  • Loading branch information
Shobhit2884 committed Jan 16, 2019
2 parents 4a34180 + 85f848a commit d8d3892
Show file tree
Hide file tree
Showing 32 changed files with 1,528 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright © 2016-2018 The Thingsboard Authors
* Modifications © 2017-2018 Hashmap, 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.hashmapinc.server.controller;

import com.hashmapinc.server.common.data.id.TemplateId;
import com.hashmapinc.server.common.data.page.PaginatedResult;
import com.hashmapinc.server.common.data.page.TextPageData;
import com.hashmapinc.server.common.data.page.TextPageLink;
import com.hashmapinc.server.common.data.template.TemplateMetadata;
import com.hashmapinc.server.dao.template.TemplateService;
import com.hashmapinc.server.common.data.exception.TempusException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class TemplateController extends BaseController {

@Autowired
private TemplateService templateService;

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@PostMapping(value = "/template", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public TemplateMetadata save(@RequestBody TemplateMetadata source) throws TempusException {
try {
return templateService.save(source);
} catch (Exception e) {
throw handleException(e);
}
}

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@DeleteMapping(value = "/template/{id}")
public void delete(@PathVariable String id) throws TempusException {
try {
templateService.delete(new TemplateId(toUUID(id)));
} catch (Exception e) {
throw handleException(e);
}
}

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@GetMapping(value = "/template/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public TemplateMetadata get(@PathVariable String id) throws TempusException {
try {
return templateService.getTemplate(new TemplateId(toUUID(id)));
} catch (Exception e) {
throw handleException(e);
}
}

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@GetMapping(value = "/templates", produces = MediaType.APPLICATION_JSON_VALUE, params = "limit")
@ResponseBody
public TextPageData<TemplateMetadata> getTemplates(@RequestParam int limit,
@RequestParam(required = false) String textSearch,
@RequestParam(required = false) String idOffset,
@RequestParam(required = false) String textOffset) throws TempusException {
try {
TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
return templateService.getTemplate(pageLink);
} catch (Exception e) {
throw handleException(e);
}
}

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@GetMapping(value = "/templates", produces = MediaType.APPLICATION_JSON_VALUE, params = {"limit", "pageNum"})
@ResponseBody
public PaginatedResult<TemplateMetadata> getTemplatesByPage(@RequestParam int limit,
@RequestParam(required = false) int pageNum,
@RequestParam(required = false) String textSearch) throws TempusException {
try {
return templateService.getTemplatesByPage(limit, pageNum, textSearch);
} catch (Exception e) {
throw handleException(e);
}
}

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@GetMapping(value = "/templates", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<TemplateMetadata> getAll() throws TempusException {
try {
return templateService.getAllTemplates();
} catch (Exception e) {
throw handleException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@


public enum EntityType {
TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM, COMPUTATION, COMPUTATION_JOB, NODE_METRIC,THEME, LOGO,
DATA_MODEL_OBJECT, DATA_MODEL, CUSTOMER_GROUP, TEMPUS_GATEWAY_CONFIGURATION
TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM, COMPUTATION, COMPUTATION_JOB, NODE_METRIC, THEME, LOGO,
DATA_MODEL_OBJECT, DATA_MODEL, CUSTOMER_GROUP, TEMPUS_GATEWAY_CONFIGURATION, TEMPLATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static EntityId getByTypeAndUuid(EntityType type, UUID uuid) {
return new CustomerGroupId(uuid);
case TEMPUS_GATEWAY_CONFIGURATION:
return new TempusGatewayConfigurationId(uuid);
case TEMPLATE:
return new TemplateId(uuid);
}
throw new IllegalArgumentException("EntityType " + type + " is not supported!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright © 2016-2018 The Thingsboard Authors
* Modifications © 2017-2018 Hashmap, 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.hashmapinc.server.common.data.id;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hashmapinc.server.common.data.EntityType;

import java.util.UUID;

public class TemplateId extends UUIDBased implements EntityId {

@JsonCreator
public TemplateId(@JsonProperty("id") UUID id) {
super(id);
}

@Override
public EntityType getEntityType() {
return EntityType.TEMPLATE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright © 2016-2018 The Thingsboard Authors
* Modifications © 2017-2018 Hashmap, 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.hashmapinc.server.common.data.template;

import com.hashmapinc.server.common.data.HasName;
import com.hashmapinc.server.common.data.SearchTextBased;
import com.hashmapinc.server.common.data.id.TemplateId;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
@NoArgsConstructor
public class TemplateMetadata extends SearchTextBased<TemplateId> implements HasName {

private String name;
private String body;

public TemplateMetadata(TemplateId id) {
super(id);
}

@Override
public String getName() {
return name;
}

@Override
public String getSearchText() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.hashmapinc.server.dao.relation.RelationService;
import com.hashmapinc.server.dao.rule.RuleService;
import com.hashmapinc.server.dao.settings.UserSettingsService;
import com.hashmapinc.server.dao.template.TemplateService;
import com.hashmapinc.server.dao.tenant.TenantService;
import com.hashmapinc.server.dao.theme.ThemeService;
import com.hashmapinc.server.dao.timeseries.TimeseriesService;
Expand Down Expand Up @@ -147,6 +148,9 @@ public abstract class AbstractServiceTest {
@Autowired
protected PluginService pluginService;

@Autowired
protected TemplateService templateService;

@Autowired
protected RuleService ruleService;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright © 2016-2018 The Thingsboard Authors
* Modifications © 2017-2018 Hashmap, 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.hashmapinc.server.dao.service.template;

import com.hashmapinc.server.common.data.page.PaginatedResult;
import com.hashmapinc.server.common.data.template.TemplateMetadata;
import com.hashmapinc.server.dao.service.AbstractServiceTest;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public abstract class BaseTemplateServiceTest extends AbstractServiceTest {

@Test
public void saveTemplate() {
TemplateMetadata givenTemplateMetadata = getTemplateMetadata("1");
TemplateMetadata savedTemplateMetaData = templateService.save(givenTemplateMetadata);
Assert.assertNotNull(savedTemplateMetaData.getId());

TemplateMetadata fetchedTemplateMetadata = templateService.getTemplate(savedTemplateMetaData.getId());
Assert.assertEquals(givenTemplateMetadata.getName(), fetchedTemplateMetadata.getName());
Assert.assertEquals(givenTemplateMetadata.getBody(), fetchedTemplateMetadata.getBody());

templateService.delete(savedTemplateMetaData.getId());
}

@Test
public void deleteTemplate() {
TemplateMetadata givenTemplateMetadata = getTemplateMetadata("1");
TemplateMetadata savedTemplateMetadata = templateService.save(givenTemplateMetadata);
Assert.assertNotNull(savedTemplateMetadata.getId());

templateService.delete(savedTemplateMetadata.getId());
TemplateMetadata templateMetadata = templateService.getTemplate(savedTemplateMetadata.getId());
Assert.assertNull(templateMetadata);
}

@Test
public void getTemplates() {
TemplateMetadata givenTemplateMetadata1 = getTemplateMetadata("1");
TemplateMetadata savedTemplateMetadata1 = templateService.save(givenTemplateMetadata1);
Assert.assertNotNull(savedTemplateMetadata1.getId());
TemplateMetadata givenTemplateMetadata2 = getTemplateMetadata("2");
TemplateMetadata savedTemplateMetadata2 = templateService.save(givenTemplateMetadata2);
Assert.assertNotNull(savedTemplateMetadata2.getId());
TemplateMetadata givenTemplateMetadata3 = getTemplateMetadata("3");
TemplateMetadata savedTemplateMetadata3 = templateService.save(givenTemplateMetadata3);
Assert.assertNotNull(savedTemplateMetadata3.getId());

List<TemplateMetadata> templateMetadataList = templateService.getAllTemplates();
Assert.assertEquals(3, templateMetadataList.size());

PaginatedResult<TemplateMetadata> templateMetadataPage = templateService.getTemplatesByPage(2, 1, "temp");
Assert.assertEquals(1, templateMetadataPage.getData().size());
}

private TemplateMetadata getTemplateMetadata(String suffix) {
TemplateMetadata templateMetadata = new TemplateMetadata();
templateMetadata.setName("templatename" + suffix);
templateMetadata.setBody("{templatebody}");
return templateMetadata;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright © 2016-2018 The Thingsboard Authors
* Modifications © 2017-2018 Hashmap, 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.hashmapinc.server.dao.service.template.sql;

import com.hashmapinc.server.dao.service.DaoSqlTest;
import com.hashmapinc.server.dao.service.template.BaseTemplateServiceTest;

@DaoSqlTest
public class TemplateServiceSqlTest extends BaseTemplateServiceTest {
}
3 changes: 2 additions & 1 deletion dao/src/it/resources/sql/drop-all-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ DROP TABLE IF EXISTS kubeless_computation_meta_data;
DROP TABLE IF EXISTS metadata_entries;
DROP TABLE IF EXISTS spark_computation_meta_data;
DROP TABLE IF EXISTS tempus_gateway_configuration;
DROP TABLE IF EXISTS tenant_unit_system;
DROP TABLE IF EXISTS tenant_unit_system;
DROP TABLE IF EXISTS templates;
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ private ModelConstants() {
public static final String RULE_PROCESSOR = "processor";
public static final String RULE_ACTION = "action";

/**
* Template metadata constants.
*/
public static final String TEMPLATE_COLUMN_FAMILY_NAME = "templates";
public static final String TEMPLATE_NAME_PROPERTY = "name";
public static final String TEMPLATE_BODY_PROPERTY = "body";

/**
* Event constants.
*/
Expand Down

0 comments on commit d8d3892

Please sign in to comment.