Skip to content

Commit

Permalink
Fixed up formatting and capitalized JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarkowsky committed Sep 22, 2021
1 parent c26aff3 commit 9149315
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 309 deletions.
23 changes: 11 additions & 12 deletions Source/santametricservice/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,38 @@ objc_library(
srcs = [
"SNTMetricService.h",
"SNTMetricService.m",
"main.m",
"main.m",
],
deps = [
"//Source/common:SNTXPCMetricServiceInterface",
"//Source/common:SNTLogging",
"//Source/common:SNTConfigurator",
"//Source/common:SNTLogging",
"//Source/common:SNTMetricSet",
"//Source/common:SNTXPCMetricServiceInterface",
"//Source/santametricservice/Formats:SNTMetricRawJsonFormat",
"//Source/santametricservice/Writers:SNTMetricFileWriter",
"@MOLCodesignChecker",
"@MOLCodesignChecker",
"@MOLXPCConnection",
],
)

santa_unit_test(
name="SNTMetricServiceTest",
name = "SNTMetricServiceTest",
srcs = ["SNTMetricServiceTest.m"],
deps = [
":SNTMetricServiceLib",
"@OCMock",
":SNTMetricServiceLib",
"@OCMock",
],
)

test_suite(
name="unit_tests",
name = "unit_tests",
tests = [
":SNTMetricServiceTest",
"//Source/santametricservice/Formats:SNTMetricRawJsonFormatTest",
"//Source/santametricservice/Writers:SNTMetricFileWriterTest",
":SNTMetricServiceTest",
"//Source/santametricservice/Formats:SNTMetricRawJsonFormatTest",
"//Source/santametricservice/Writers:SNTMetricFileWriterTest",
],
)


macos_command_line_application(
name = "santametricservice",
bundle_id = "com.google.santa.metricservice",
Expand Down
14 changes: 6 additions & 8 deletions Source/santametricservice/Formats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ objc_library(
objc_library(
name = "SNTMetricRawJsonFormat",
srcs = [
"SNTMetricFormat.h",
"SNTMetricRawJsonFormat.h",
"SNTMetricRawJsonFormat.m",
"SNTMetricFormat.h",

],
deps = [
":SNTMetricFormat",
Expand All @@ -32,14 +31,13 @@ santa_unit_test(
structured_resources = glob(["testdata/**"]),
deps = [
":SNTMetricRawJsonFormat",
"//Source/common:SNTMetricSet",
"//Source/common:SNTMetricSet",
],
)


test_suite(
name = "format_tests",
tests = [
":SNTMetricRawJsonFormatTest",
],
name = "format_tests",
tests = [
":SNTMetricRawJsonFormatTest",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

#import "Source/santametricservice/Formats/SNTMetricFormat.h"

@interface SNTMetricRawJsonFormat : NSObject <SNTMetricFormat>
- (NSArray <NSData *> *) convert:(NSDictionary *)metrics error:(NSError **)err;
@interface SNTMetricRawJSONFormat : NSObject <SNTMetricFormat>
- (NSArray<NSData *> *)convert:(NSDictionary *)metrics error:(NSError **)err;
@end
94 changes: 94 additions & 0 deletions Source/santametricservice/Formats/SNTMetricRawJSONFormat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// Copyright 2021 Google Inc. All rights reserved.
///
/// 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.
#import "Source/common/SNTLogging.h"

#import "Source/santametricservice/Formats/SNTMetricRawJSONFormat.h"

@implementation SNTMetricRawJSONFormat {
NSDateFormatter *dateFormatter;
}

- (instancetype)init {
self = [super init];
if (self) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
}
return self;
}

- (NSArray *)normalizeArray:(NSArray *)arr {
NSMutableArray *normalized = [NSMutableArray arrayWithArray:arr];

for (int i = 0; i < [arr count]; i++) {
if ([arr[i] isKindOfClass:[NSArray class]]) {
normalized[i] = [self normalizeArray:(NSArray *)arr[i]];
} else if ([arr[i] isKindOfClass:[NSDictionary class]]) {
normalized[i] = [self normalize:(NSDictionary *)arr[i]];
}
}

return normalized;
}

/**
* Normalizes the metrics dictionary for exporting to JSON
**/
- (NSDictionary *)normalize:(NSDictionary *)metrics {
// Convert NSDate's to RFC3339 in strings as NSDate's cannot be serialized
// to JSON.
NSMutableDictionary *normalizedMetrics = [NSMutableDictionary dictionaryWithDictionary:metrics];

for (NSString *key in metrics) {
const id object = [metrics objectForKey:key];
if ([object isKindOfClass:[NSDate class]]) {
normalizedMetrics[key] = [self->dateFormatter stringFromDate:(NSDate *)object];
} else if ([object isKindOfClass:[NSDictionary class]]) {
normalizedMetrics[key] = [self normalize:metrics[key]];
} else if ([object isKindOfClass:[NSArray class]]) {
normalizedMetrics[key] = [self normalizeArray:(NSArray *)object];
}
}

return (NSDictionary *)normalizedMetrics;
}

/*
* Convert normalies and converts the metrics dictionary to a single JSON
* object.
*
* @param metrics an NSDictionary exported by the SNTMetricSet
* @param error a pointer to an NSError to allow errors to bubble up.
*
* Returns an NSArray containing one entry of all metrics serialized to JSON or
* nil on error.
*/
- (NSArray<NSData *> *)convert:(NSDictionary *)metrics error:(NSError **)err {
NSDictionary *normalizedMetrics = [self normalize:metrics];

if (![NSJSONSerialization isValidJSONObject:normalizedMetrics]) {
LOGE(@"unable to convert metrics to JSON: invalid metrics");
return nil;
}

NSData *json = [NSJSONSerialization dataWithJSONObject:normalizedMetrics
options:NSJSONWritingPrettyPrinted
error:err];
if (json == nil && *err != nil) {
return nil;
}

return @[ json ];
}
@end
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#import <XCTest/XCTest.h>

#import "Source/common/SNTMetricSet.h"
#import "Source/santametricservice/Formats/SNTMetricRawJsonFormat.h"
#import "Source/santametricservice/Formats/SNTMetricRawJSONFormat.h"

NSDictionary *validMetricsDict = nil;

@interface SNTMetricRawJsonFormatTest : XCTestCase
@interface SNTMetricRawJSONFormatTest : XCTestCase
@end

@implementation SNTMetricRawJsonFormatTest
@implementation SNTMetricRawJSONFormatTest

- (void)initializeValidMetricsDict
{
- (void)initializeValidMetricsDict {
NSDateFormatter *formatter = NSDateFormatter.new;
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *fixedDate = [formatter dateFromString:@"2021-09-16T21:07:34.826Z"];
Expand Down Expand Up @@ -116,40 +115,39 @@ - (void)initializeValidMetricsDict
};
}

- (void)setUp
{
[self initializeValidMetricsDict];
- (void)setUp {
[self initializeValidMetricsDict];
}

- (void)testMetricsConversionToJSON
{
SNTMetricRawJsonFormat *formatter = [[SNTMetricRawJsonFormat alloc] init];
NSError *err = nil;
NSArray<NSData *> *output = [formatter convert:validMetricsDict error: &err];

XCTAssertEqual(1, [output count]);
XCTAssertNotNil(output[0]);
XCTAssertNil(err);

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:output[0]
options:NSJSONReadingAllowFragments
error:&err];
XCTAssertNotNil(jsonDict);

NSString *path = [[NSBundle bundleForClass:[self class]] resourcePath];
path = [path stringByAppendingPathComponent:@"testdata/json/test.json"];

NSFileManager *filemgr = [NSFileManager defaultManager];
NSData *goldenFileData = [filemgr contentsAtPath: path];
XCTAssertNotNil(goldenFileData, @"unable to open / read golden file");

NSDictionary *expectedJsonDict = [NSJSONSerialization JSONObjectWithData:goldenFileData
options:NSJSONReadingAllowFragments
error: &err];
XCTAssertNotNil(expectedJsonDict);
XCTAssertEqualObjects(expectedJsonDict, jsonDict, @"generated JSON does not match golden file.");
- (void)testMetricsConversionToJSON {
SNTMetricRawJSONFormat *formatter = [[SNTMetricRawJSONFormat alloc] init];
NSError *err = nil;
NSArray<NSData *> *output = [formatter convert:validMetricsDict error:&err];

XCTAssertEqual(1, [output count]);
XCTAssertNotNil(output[0]);
XCTAssertNil(err);

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:output[0]
options:NSJSONReadingAllowFragments
error:&err];
XCTAssertNotNil(jsonDict);

NSString *path = [[NSBundle bundleForClass:[self class]] resourcePath];
path = [path stringByAppendingPathComponent:@"testdata/json/test.json"];

NSFileManager *filemgr = [NSFileManager defaultManager];
NSData *goldenFileData = [filemgr contentsAtPath:path];

XCTAssertNotNil(goldenFileData, @"unable to open / read golden file");

NSDictionary *expectedJSONDict =
[NSJSONSerialization JSONObjectWithData:goldenFileData
options:NSJSONReadingAllowFragments
error:&err];

XCTAssertNotNil(expectedJSONDict);
XCTAssertEqualObjects(expectedJSONDict, jsonDict, @"generated JSON does not match golden file.");
}

@end
@end
98 changes: 0 additions & 98 deletions Source/santametricservice/Formats/SNTMetricRawJsonFormat.m

This file was deleted.

0 comments on commit 9149315

Please sign in to comment.