Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate model if not retrieved from model repo; moved constants around #247

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/app/api/constants.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/api/models/authorizationRuleNotFoundError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
import { ERROR_TYPES } from '../constants';
import { ERROR_TYPES } from './../../constants/apiConstants';

export class AuthorizationRuleNotFoundError extends Error {
public requiredPermissions: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/models/httpError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
import { ERROR_TYPES } from '../constants';
import { ERROR_TYPES } from './../../constants/apiConstants';

export class HttpError extends Error {
public httpCode: number;
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/models/modelDefinitionWithSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import { REPOSITORY_LOCATION_TYPE } from '../../constants/repositoryLocationType

export interface ModelDefinitionWithSource {
modelDefinition: ModelDefinition;
source?: REPOSITORY_LOCATION_TYPE;
source: REPOSITORY_LOCATION_TYPE;
isModelValid: boolean;
}
2 changes: 1 addition & 1 deletion src/app/api/models/portIsInUseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
import { ERROR_TYPES } from '../constants';
import { ERROR_TYPES } from './../../constants/apiConstants';

export class PortIsInUseError extends Error {
constructor() {
Expand Down
3 changes: 1 addition & 2 deletions src/app/api/services/dataplaneServiceHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Licensed under the MIT License
**********************************************************/
import { DataPlaneParameters } from '../parameters/deviceParameters';
import { CONTROLLER_API_ENDPOINT, DATAPLANE, DataPlaneStatusCode } from '../../constants/apiConstants';
import { HTTP_OPERATION_TYPES } from '../constants';
import { CONTROLLER_API_ENDPOINT, DATAPLANE, DataPlaneStatusCode, HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { getConnectionInfoFromConnectionString, generateSasToken } from '../shared/utils';
import { PortIsInUseError } from '../models/portIsInUseError';

Expand Down
3 changes: 1 addition & 2 deletions src/app/api/services/devicesService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import 'jest';
import * as DevicesService from './devicesService';
import * as DataplaneService from './dataplaneServiceHelper';
import { HTTP_OPERATION_TYPES } from '../constants';
import { DIGITAL_TWIN_API_VERSION, CONTROLLER_API_ENDPOINT, CLOUD_TO_DEVICE } from '../../constants/apiConstants';
import { DIGITAL_TWIN_API_VERSION, CONTROLLER_API_ENDPOINT, CLOUD_TO_DEVICE, HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { CONNECTION_TIMEOUT_IN_SECONDS, RESPONSE_TIME_IN_SECONDS } from '../../constants/devices';
import { Twin } from '../models/device';
import { DeviceIdentity } from './../models/deviceIdentity';
Expand Down
3 changes: 1 addition & 2 deletions src/app/api/services/devicesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
PatchDigitalTwinInterfacePropertiesParameters,
CloudToDeviceMessageParameters
} from '../parameters/deviceParameters';
import { CONTROLLER_API_ENDPOINT, EVENTHUB, DIGITAL_TWIN_API_VERSION, MONITOR, STOP, HEADERS, CLOUD_TO_DEVICE } from '../../constants/apiConstants';
import { HTTP_OPERATION_TYPES } from '../constants';
import { CONTROLLER_API_ENDPOINT, EVENTHUB, DIGITAL_TWIN_API_VERSION, MONITOR, STOP, HEADERS, CLOUD_TO_DEVICE, HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { buildQueryString } from '../shared/utils';
import { CONNECTION_TIMEOUT_IN_SECONDS, RESPONSE_TIME_IN_SECONDS } from '../../constants/devices';
import { Message } from '../models/messages';
Expand Down
45 changes: 43 additions & 2 deletions src/app/api/services/digitalTwinsModelService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Licensed under the MIT License
**********************************************************/
import * as DigitalTwinsModelService from './digitalTwinsModelService';
import { API_VERSION, DIGITAL_TWIN_API_VERSION } from '../../constants/apiConstants';
import { HTTP_OPERATION_TYPES } from '../constants';
import { API_VERSION, DIGITAL_TWIN_API_VERSION, MODEL_REPO_API_VERSION, HTTP_OPERATION_TYPES, PUBLIC_REPO_HOSTNAME_TEST } from '../../constants/apiConstants';

describe('digitalTwinsModelService', () => {

Expand Down Expand Up @@ -111,4 +110,46 @@ describe('digitalTwinsModelService', () => {
done();
});
});

context('validateModelDefinitions', () => {
const parameters = JSON.stringify([]);

it('calls fetch with specified parameters and returns true when response is 200', async () => {
// tslint:disable
const response = {
json: () => {},
ok: true
} as any;
// tslint:enable
jest.spyOn(window, 'fetch').mockResolvedValue(response);

const result = await DigitalTwinsModelService.validateModelDefinitions(parameters);

const apiVersionQueryString = `?${API_VERSION}${MODEL_REPO_API_VERSION}`;
const controllerRequest = {
body: parameters,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-ms-client-request-id': 'azure iot explorer: validate model definition'
},
method: HTTP_OPERATION_TYPES.Post,
uri: `https://${PUBLIC_REPO_HOSTNAME_TEST}/models/validate${apiVersionQueryString}`
};

const validateModelParameters = {
body: JSON.stringify(controllerRequest),
cache: 'no-cache',
credentials: 'include',
headers: new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
}),
method: HTTP_OPERATION_TYPES.Post
};

expect(fetch).toBeCalledWith(DigitalTwinsModelService.CONTROLLER_ENDPOINT, validateModelParameters);
expect(result).toEqual(true);
});
});
});
28 changes: 26 additions & 2 deletions src/app/api/services/digitalTwinsModelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
CONTROLLER_API_ENDPOINT,
DIGITAL_TWIN_API_VERSION,
HEADERS,
MODELREPO } from '../../constants/apiConstants';
import { HTTP_OPERATION_TYPES } from '../constants';
MODELREPO,
MODEL_REPO_API_VERSION,
PUBLIC_REPO_HOSTNAME_TEST,
HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { PnPModel } from '../models/metamodelMetadata';

export const fetchModel = async (parameters: FetchModelParameters): Promise<PnPModel> => {
Expand Down Expand Up @@ -69,6 +71,27 @@ export const fetchModelDefinition = async (parameters: FetchModelParameters) =>
}
};

export const validateModelDefinitions = async (modelDefinitions: string) => {
try {
const apiVersionQueryString = `?${API_VERSION}${MODEL_REPO_API_VERSION}`;
const controllerRequest: RequestInitWithUri = {
body: modelDefinitions,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-ms-client-request-id': 'azure iot explorer: validate model definition'
},
method: HTTP_OPERATION_TYPES.Post,
uri: `https://${PUBLIC_REPO_HOSTNAME_TEST}/models/validate${apiVersionQueryString}`
};

return (await request(controllerRequest)).ok;
}
catch (error) {
throw new Error(error);
}
};

export interface RequestInitWithUri extends RequestInit {
uri: string;
headers?: Record<string, string>;
Expand All @@ -82,6 +105,7 @@ export interface RepoConnectionSettings {
}

export const CONTROLLER_ENDPOINT = `${CONTROLLER_API_ENDPOINT}${MODELREPO}`;

const request = async (requestInit: RequestInitWithUri) => {
return fetch(
CONTROLLER_ENDPOINT,
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/services/moduleService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import 'jest';
import * as ModuleService from './moduleService';
import * as DataplaneService from './dataplaneServiceHelper';
import { HTTP_OPERATION_TYPES } from '../constants';
import { getConnectionInfoFromConnectionString } from '../shared/utils';
import { DataPlaneParameters } from '../parameters/deviceParameters';
import { ModuleIdentity } from '../models/moduleIdentity';
import { ModuleTwin } from '../models/moduleTwin';
import { HTTP_OPERATION_TYPES } from '../../constants/apiConstants';

const deviceId = 'deviceId';
const moduleId = 'moduleId';
Expand Down
3 changes: 1 addition & 2 deletions src/app/api/services/moduleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
ModuleIdentityTwinParameters,
FetchModuleIdentityParameters
} from '../parameters/moduleParameters';
import { HTTP_OPERATION_TYPES } from '../constants';
import { DataPlaneResponse } from '../models/device';
import { ModuleIdentity } from '../models/moduleIdentity';
import { ModuleTwin } from '../models/moduleTwin';
import { dataPlaneConnectionHelper, dataPlaneResponseHelper, request, DATAPLANE_CONTROLLER_ENDPOINT, DataPlaneRequest } from './dataplaneServiceHelper';
import { HEADERS } from '../../constants/apiConstants';
import { HEADERS, HTTP_OPERATION_TYPES } from '../../constants/apiConstants';

export interface IoTHubConnectionSettings {
hostName?: string;
Expand Down
3 changes: 1 addition & 2 deletions src/app/api/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { IoTHubConnectionSettings } from '../services/devicesService';
import { LIST_PLUG_AND_PLAY_DEVICES, SAS_EXPIRES_MINUTES } from '../../constants/devices';
import DeviceQuery, { QueryClause, ParameterType, OperationType } from '../models/deviceQuery';
import { RepoConnectionSettings } from '../services/digitalTwinsModelService';
import { AppEnvironment } from '../../constants/shared';
import { MILLISECONDS_PER_SECOND, SECONDS_PER_MINUTE } from '../constants';
import { AppEnvironment, MILLISECONDS_PER_SECOND, SECONDS_PER_MINUTE } from '../../constants/shared';

export const enum PnPQueryPrefix {
HAS_CAPABILITY_MODEL = 'HAS_CAPABILITYMODEL',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { getAzureResourceIdentifiers, getAzureResourceIdentifier } from './azureResourceIdentifierService';
import { AzureResourceIdentifierType } from '../models/azureResourceIdentifierType';
import { HttpError } from '../../api/models/httpError';
import { APPLICATION_JSON, HTTP_OPERATION_TYPES } from '../../api/constants';
import { HTTP_OPERATION_TYPES, APPLICATION_JSON } from '../../constants/apiConstants';

describe('getAzureResourceIdentifiers', () => {
it('calls fetch with specificed parameters', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AzureResourceIdentifier } from '../models/azureResourceIdentifier';
import { AzureResourceIdentifierType } from '../models/azureResourceIdentifierType';
import { AzureResourceIdentifierQuery } from '../models/azureResourceIdentifierQuery';
import { AzureResourceIdentifierQueryResult } from '../models/azureResourceIdentifierQueryResult';
import { APPLICATION_JSON, HTTP_OPERATION_TYPES } from '../../api/constants';
import { APPLICATION_JSON, HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { AzureResourceManagementEndpoint } from '../models/azureResourceManagementEndpoint';
import { HttpError } from '../../api/models/httpError';
import { mapPropertyArrayToObject } from '../../api/shared/mapUtils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HTTP_OPERATION_TYPES } from './../../constants/apiConstants';
/***********************************************************
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
import { getAzureSubscriptions } from './azureSubscriptionService';
import { HttpError } from '../../api/models/httpError';
import { APPLICATION_JSON, HTTP_OPERATION_TYPES } from '../../api/constants';
import { APPLICATION_JSON } from '../../constants/apiConstants';

describe('getAzureSubscriptions', () => {
it('calls fetch with expected parameters', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License
**********************************************************/
import { AzureResourceManagementEndpoint } from '../models/azureResourceManagementEndpoint';
import { APPLICATION_JSON, HTTP_OPERATION_TYPES } from '../../api/constants';
import { APPLICATION_JSON, HTTP_OPERATION_TYPES } from '../../constants/apiConstants';
import { HttpError } from '../../api/models/httpError';
import { AzureSubscription } from '../models/azureSubscription';

Expand Down
18 changes: 18 additions & 0 deletions src/app/constants/apiConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ export const MODEL_ID_REF = '/ref:modelId?';
export const MODEL_ID = 'modelId=';
export const API_VERSION = 'api-version=';
export const AND = '&';
export const PUBLIC_REPO_HOSTNAME = 'repo.azureiotrepository.com';
export const PUBLIC_REPO_HOSTNAME_TEST = 'repo.azureiotrepository-test.com';

// event hub controller
export const MONITOR = '/monitor';
export const STOP = '/stop';

// digital twin api version
export const DIGITAL_TWIN_API_VERSION = '2019-07-01-preview';
export const MODEL_REPO_API_VERSION = '2020-05-01-preview';

export const HEADERS = {
CONTINUATION_TOKEN: 'x-ms-continuation',
Expand Down Expand Up @@ -56,3 +59,18 @@ export const CONTROLLER_API_ENDPOINT =
appConfig.hostMode === HostMode.Browser ?
`${localIp}:${appConfig.controllerPort}${apiPath}` :
`${localIp}:${localStorage.getItem(CUSTOM_CONTROLLER_PORT) || appConfig.controllerPort}${apiPath}`;

export enum HTTP_OPERATION_TYPES {
Delete = 'DELETE',
Get = 'GET',
Patch = 'PATCH',
Post = 'POST',
Put = 'PUT'
}

export const APPLICATION_JSON = 'application/json';
export const ERROR_TYPES = {
AUTHORIZATION_RULE_NOT_FOUND: 'authorizationRuleNotFound',
HTTP: 'http',
PORT_IS_IN_USE: 'portIsInUse'
};
17 changes: 3 additions & 14 deletions src/app/constants/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
export enum SYNC_STATUS {
Failed,
None,
Synced,
Syncing
}

export enum DesiredStateStatus{
Success = 200,
Synching = 202,
Error = 500
}

export const OFFSET_IN_MINUTES = 15;
export const MILLISECONDS_IN_MINUTE = 60000;
export const PUBLIC_REPO_HOSTNAME = 'repo.azureiotrepository.com';
export const MILLISECONDS_PER_SECOND = 1000;
export const SECONDS_PER_MINUTE = 60;

export enum AppEnvironment {
ProdElectron = 'prodElectron',
Expand Down
10 changes: 2 additions & 8 deletions src/app/devices/deviceContent/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
import actionCreatorFactory from 'typescript-fsa';
import * as actionPrefixes from '../../constants/actionPrefixes';
import * as actionTypes from '../../constants/actionTypes';
import { ModelDefinition } from '../../api/models/modelDefinition';
import { Twin } from '../../api/models/device';
import { DeviceIdentity } from '../../api/models/deviceIdentity';
import { DigitalTwinInterfaces } from './../../api/models/digitalTwinModels';
import { REPOSITORY_LOCATION_TYPE } from './../../constants/repositoryLocationTypes';
import { ModelDefinitionWithSource } from './../../api/models/modelDefinitionWithSource';

const deviceContentCreator = actionCreatorFactory(actionPrefixes.DEVICECONTENT);
const clearModelDefinitionsAction = deviceContentCreator(actionTypes.CLEAR_MODEL_DEFINITIONS);
const cloudToDeviceMessageAction = deviceContentCreator.async<CloudToDeviceMessageActionParameters, string>(actionTypes.CLOUD_TO_DEVICE_MESSAGE);
const getDeviceIdentityAction = deviceContentCreator.async<string, DeviceIdentity> (actionTypes.GET_DEVICE_IDENTITY);
const getDigitalTwinInterfacePropertiesAction = deviceContentCreator.async<string, DigitalTwinInterfaces>(actionTypes.GET_DIGITAL_TWIN_INTERFACE_PROPERTIES);
const getTwinAction = deviceContentCreator.async<string, Twin>(actionTypes.GET_TWIN);
const getModelDefinitionAction = deviceContentCreator.async<GetModelDefinitionActionParameters, ModelDefinitionActionResult>(actionTypes.FETCH_MODEL_DEFINITION);
const getModelDefinitionAction = deviceContentCreator.async<GetModelDefinitionActionParameters, ModelDefinitionWithSource>(actionTypes.FETCH_MODEL_DEFINITION);
const invokeDirectMethodAction = deviceContentCreator.async<InvokeMethodActionParameters, string>(actionTypes.INVOKE_DEVICE_METHOD);
const invokeDigitalTwinInterfaceCommandAction = deviceContentCreator.async<InvokeDigitalTwinInterfaceCommandActionParameters, string>(actionTypes.INVOKE_DIGITAL_TWIN_INTERFACE_COMMAND);
const patchDigitalTwinInterfacePropertiesAction = deviceContentCreator.async<PatchDigitalTwinInterfacePropertiesActionParameters, DigitalTwinInterfaces>(actionTypes.PATCH_DIGITAL_TWIN_INTERFACE_PROPERTIES);
Expand Down Expand Up @@ -71,11 +70,6 @@ export interface UpdateTwinActionParameters {
twin: Twin;
}

export interface ModelDefinitionActionResult {
modelDefinition: ModelDefinition;
source: REPOSITORY_LOCATION_TYPE;
}

export interface GetModelDefinitionActionParameters {
digitalTwinId: string;
interfaceId: string;
Expand Down
Loading