-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1009 from hashmapinc/Tempus-965
Tempus 965-Template registry
- Loading branch information
Showing
32 changed files
with
1,528 additions
and
17 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
application/src/main/java/com/hashmapinc/server/controller/TemplateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
common/data/src/main/java/com/hashmapinc/server/common/data/id/TemplateId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
common/data/src/main/java/com/hashmapinc/server/common/data/template/TemplateMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
dao/src/it/java/com/hashmapinc/server/dao/service/template/BaseTemplateServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
dao/src/it/java/com/hashmapinc/server/dao/service/template/sql/TemplateServiceSqlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.