Skip to content

Commit

Permalink
Merge branch 'dev' into tempus-elaboration-37
Browse files Browse the repository at this point in the history
  • Loading branch information
randypitcherii authored Jun 11, 2018
2 parents 28ba00e + 8dc458b commit 1d60bbf
Show file tree
Hide file tree
Showing 38 changed files with 1,106 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hashmapinc.server.common.data.plugin.ComponentDescriptor;
import com.hashmapinc.server.dao.application.ApplicationService;
import com.hashmapinc.server.dao.cluster.NodeMetricService;
import com.hashmapinc.server.dao.datamodel.DataModelService;
import com.hashmapinc.server.dao.device.DeviceCredentialsService;
import com.hashmapinc.server.dao.rule.RuleService;
import com.hashmapinc.server.dao.user.UserService;
Expand Down Expand Up @@ -98,6 +99,9 @@ public abstract class BaseController {
@Autowired
protected AssetService assetService;

@Autowired
protected DataModelService dataModelService;

@Autowired
protected AlarmService alarmService;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright © 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.DataModel;
import com.hashmapinc.server.common.data.EntityType;
import com.hashmapinc.server.common.data.audit.ActionType;
import com.hashmapinc.server.exception.TempusException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

public static final String DATA_MODEL_ID = "dataModelId";

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/data-model", method = RequestMethod.POST)
@ResponseBody
public DataModel saveDataModel(@RequestBody DataModel dataModel) throws TempusException {
dataModel.setTenantId(getCurrentUser().getTenantId());
dataModel.setLastUpdatedTs(System.currentTimeMillis());
try {
DataModel savedDataModel = checkNotNull(dataModelService.saveDataModel(dataModel));
logEntityAction(savedDataModel.getId(), savedDataModel,
null,
dataModel.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
return savedDataModel;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.DATA_MODEL), dataModel,
null, dataModel.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
throw handleException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright © 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.DataModel;
import com.hashmapinc.server.common.data.Tenant;
import com.hashmapinc.server.common.data.User;
import com.hashmapinc.server.common.data.security.Authority;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class BaseDataModelControllerTest extends AbstractControllerTest {

private Tenant savedTenant;
private User tenantAdmin;

@Before
public void beforeTest() throws Exception {
loginSysAdmin();

Tenant tenant = new Tenant();
tenant.setTitle("My tenant");
savedTenant = doPost("/api/tenant", tenant, Tenant.class);
Assert.assertNotNull(savedTenant);

tenantAdmin = new User();
tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
tenantAdmin.setTenantId(savedTenant.getId());
tenantAdmin.setEmail("[email protected]");
tenantAdmin.setFirstName("Joe");
tenantAdmin.setLastName("Downs");

tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
}

@After
public void afterTest() throws Exception {
loginSysAdmin();

doDelete("/api/tenant/"+savedTenant.getId().getId().toString())
.andExpect(status().isOk());
}


@Test
public void testSaveDataModel() throws Exception {
DataModel dataModel = new DataModel();
dataModel.setName("Drilling Data Model1");
dataModel.setLastUpdatedTs(System.currentTimeMillis());

DataModel savedDataModel = doPost("/api/data-model", dataModel, DataModel.class);

Assert.assertNotNull(savedDataModel);
Assert.assertNotNull(savedDataModel.getId());
Assert.assertTrue(savedDataModel.getCreatedTime() > 0);
Assert.assertEquals(savedTenant.getId(), savedDataModel.getTenantId());
Assert.assertEquals(dataModel.getName(), savedDataModel.getName());
Assert.assertTrue(savedDataModel.getLastUpdatedTs() > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private static List<CQLDataSet> getDataSetLists(){
dataSets.add(new ClassPathCQLDataSet("cassandra/system-data.cql", false, false));
dataSets.add(new ClassPathCQLDataSet("cassandra/system-test.cql", false, false));
dataSets.addAll(Arrays.asList(
new ClassPathCQLDataSet("cassandra/upgrade/1.cql", false, false)
new ClassPathCQLDataSet("cassandra/upgrade/1.cql", false, false),
new ClassPathCQLDataSet("cassandra/upgrade/3.cql", false, false)
));
return dataSets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public class ControllerSqlTestSuite {
Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
"sql/drop-all-tables.sql",
"sql-test.properties",
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql")
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql", "sql/upgrade/4.sql")
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright © 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.nosql;

import com.hashmapinc.server.controller.BaseDataModelControllerTest;
import com.hashmapinc.server.dao.service.DaoNoSqlTest;

@DaoNoSqlTest
public class DataModelControllerNoSqlTest extends BaseDataModelControllerTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright © 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.sql;

import com.hashmapinc.server.controller.BaseDataModelControllerTest;
import com.hashmapinc.server.dao.service.DaoSqlTest;

@DaoSqlTest
public class DataModelControllerSqlTest extends BaseDataModelControllerTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public class MqttSqlTestSuite {
Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
"sql/drop-all-tables.sql",
"sql-test.properties",
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql"));
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql", "sql/upgrade/4.sql"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SystemSqlTestSuite {
Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
"sql/drop-all-tables.sql",
"sql-test.properties",
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql")
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql", "sql/upgrade/4.sql")
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.hashmapinc.server.common.data.id.CustomerId;
import com.hashmapinc.server.common.data.id.DataModelId;
import com.hashmapinc.server.common.data.id.TenantId;

public class Customer extends ContactBased<CustomerId> implements HasName {
Expand All @@ -27,6 +28,7 @@ public class Customer extends ContactBased<CustomerId> implements HasName {

private String title;
private TenantId tenantId;
private DataModelId dataModelId;

public Customer() {
super();
Expand All @@ -39,13 +41,22 @@ public Customer(CustomerId id) {
public Customer(Customer customer) {
super(customer);
this.tenantId = customer.getTenantId();
this.dataModelId = customer.getDataModelId();
this.title = customer.getTitle();
}

public TenantId getTenantId() {
return tenantId;
}

public DataModelId getDataModelId() {
return dataModelId;
}

public void setDataModelId(DataModelId dataModelId) {
this.dataModelId = dataModelId;
}

public void setTenantId(TenantId tenantId) {
this.tenantId = tenantId;
}
Expand Down Expand Up @@ -88,6 +99,7 @@ public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((tenantId == null) ? 0 : tenantId.hashCode());
result = prime * result + ((dataModelId == null) ? 0 : dataModelId.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
Expand All @@ -106,6 +118,11 @@ public boolean equals(Object obj) {
return false;
} else if (!tenantId.equals(other.tenantId))
return false;
if (dataModelId == null) {
if (other.dataModelId != null)
return false;
} else if (!dataModelId.equals(other.dataModelId))
return false;
if (title == null) {
if (other.title != null)
return false;
Expand All @@ -121,6 +138,8 @@ public String toString() {
builder.append(title);
builder.append(", tenantId=");
builder.append(tenantId);
builder.append(", dataModelId=");
builder.append(dataModelId);
builder.append(", additionalInfo=");
builder.append(getAdditionalInfo());
builder.append(", country=");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright © 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;

import com.hashmapinc.server.common.data.id.DataModelId;
import com.hashmapinc.server.common.data.id.TenantId;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = true)
public class DataModel extends SearchTextBasedWithAdditionalInfo<DataModelId> implements HasName {
private static final long serialVersionUID = 5100770312089703084L;

private TenantId tenantId;
private String name;
private Long lastUpdatedTs;

public DataModel() {
super();
}

public DataModel(DataModelId id) {
super(id);
}

public DataModel(DataModel dataModel) {
super(dataModel);
this.tenantId = dataModel.getTenantId();
this.name = dataModel.getName();
this.lastUpdatedTs = dataModel.lastUpdatedTs;
}

public TenantId getTenantId() {
return tenantId;
}

public void setTenantId(TenantId tenantId) {
this.tenantId = tenantId;
}

public void setName(String name) {
this.name = name;
}

public Long getLastUpdatedTs() {
return lastUpdatedTs;
}

public void setLastUpdatedTs(Long lastUpdatedTs) {
this.lastUpdatedTs = lastUpdatedTs;
}

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

@Override
public String getSearchText() {
return getName();
}
}
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, APPLICATION, COMPUTATION, COMPUTATION_JOB, NODE_METRIC,THEME
TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM, APPLICATION, COMPUTATION, COMPUTATION_JOB, NODE_METRIC,THEME, DATA_MODEL

}
Loading

0 comments on commit 1d60bbf

Please sign in to comment.